Skip to content

Commit

Permalink
Purge BitmapBgra from existence
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Jan 29, 2025
1 parent a4f9f5c commit 8daa483
Show file tree
Hide file tree
Showing 27 changed files with 21 additions and 279 deletions.
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/color_transform_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use lcms2::*;
use lcms2;
use crate::ffi;
use crate::errors::{FlowError, ErrorKind, ErrorCategory, Result};
use crate::ffi::{BitmapBgra, DecoderColorInfo};
use crate::ffi::DecoderColorInfo;
use std::cell::RefCell;
use std::thread;

Expand Down
1 change: 0 additions & 1 deletion imageflow_core/src/codecs/gif/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std;
use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse};
use crate::ffi::BitmapBgra;
use imageflow_types::collections::AddRemoveSet;
use crate::io::IoProxy;
use uuid::Uuid;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/jpeg_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std;
use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse};
use crate::ffi::BitmapBgra;

use imageflow_types::collections::AddRemoveSet;
use crate::io::IoProxy;
use uuid::Uuid;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/libpng_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse};
use crate::ffi::{wrap_jpeg_get_custom_state, WrapJpegSourceManager};
use crate::ffi::BitmapBgra;

use imageflow_types::collections::AddRemoveSet;
use crate::io::IoProxy;
use uuid::Uuid;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/libpng_encoder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Encoder;
use super::s::{EncoderPreset, EncodeResult};
use crate::io::IoProxy;
use crate::ffi::BitmapBgra;

use imageflow_types::PixelFormat;
use crate::{Context, Result, ErrorKind, FlowError};
use crate::io::IoProxyRef;
Expand Down
4 changes: 2 additions & 2 deletions imageflow_core/src/codecs/lode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Encoder;
use super::s::{EncoderPreset, EncodeResult};
use crate::io::IoProxy;
use crate::ffi::BitmapBgra;

use imageflow_types::PixelFormat;
use crate::{Context, Result, ErrorKind, FlowError};
use crate::io::IoProxyRef;
Expand Down Expand Up @@ -88,7 +88,7 @@ impl LodepngEncoder {

pub fn write_png_auto<W: Write>(writer: W, window: &mut crate::graphics::bitmaps::BitmapWindowMut<u8>, use_highest_compression: Option<bool>) -> Result<()> {

let bytes_per_pixel = window.info().channels() as usize;
let bytes_per_pixel = window.items_per_pixel() as usize;
let w = window.w() as usize;
let h = window.h() as usize;
let proper_len = w * h * bytes_per_pixel;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::*;
use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse, ErrorKind, FlowError, ErrorCategory};
use crate::ffi::BitmapBgra;

use crate::ffi::DecoderColorInfo;
use crate::ffi::ColorProfileSource;
use imageflow_types::collections::AddRemoveSet;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/mozjpeg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Encoder;
use super::s::{EncoderPreset, EncodeResult};
use crate::io::IoProxy;
use crate::ffi::BitmapBgra;

use imageflow_types::{PixelFormat, Color};
use imageflow_types::PixelBuffer;
use crate::{Context, Result, ErrorKind, FlowError};
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/mozjpeg_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse};
use crate::ffi::{wrap_jpeg_get_custom_state, WrapJpegSourceManager};
use crate::ffi::BitmapBgra;

use imageflow_types::collections::AddRemoveSet;
use crate::io::IoProxy;
use uuid::Uuid;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/mozjpeg_decoder_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std;
use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi::BitmapBgra;

use super::*;
extern crate mozjpeg_sys;
extern crate byteorder;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/pngquant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::Encoder;
use super::s::{EncoderPreset, EncodeResult};
use crate::io::IoProxy;
use crate::ffi::BitmapBgra;

use imageflow_types::PixelFormat;
use crate::{Context, Result, ErrorKind};
use std::result::Result as StdResult;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/codecs/webp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std;
use crate::for_other_imageflow_crates::preludes::external_without_std::*;
use crate::ffi;
use crate::{Context, Result, JsonResponse};
use crate::ffi::BitmapBgra;

use imageflow_types::collections::AddRemoveSet;
use crate::io::IoProxy;
use uuid::Uuid;
Expand Down
156 changes: 0 additions & 156 deletions imageflow_core/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,162 +45,6 @@ pub struct ImageflowJsonResponse {
}


/// Not for external use
#[repr(C)]
#[derive(Clone,Debug,PartialEq)]
pub struct BitmapBgra {
/// bitmap width in pixels
pub w: u32,
/// bitmap height in pixels
pub h: u32,
/// byte length of each row (may include any amount of padding)
pub stride: u32,
/// pointer to pixel 0,0; should be of length > h * stride
pub pixels: *mut u8,

pub fmt: PixelFormat,
/// When using compositing mode blend_with_matte, this color will be used. We should probably define this as
/// always being sRGBA, 4 bytes.
pub matte_color: [u8; 4],

pub compositing_mode: BitmapCompositingMode,
}


impl BitmapBgra {
#[inline]
pub fn width(&self) -> usize {
self.w as usize
}

#[inline]
pub fn stride(&self) -> usize {
self.stride as usize
}

#[inline]
pub fn height(&self) -> usize {
self.h as usize
}

#[inline]
pub unsafe fn pixels_slice(&self) -> Option<&[u8]> {
if self.pixels.is_null() {
None
} else {
let stride = self.stride();
let width_bytes = self.width() * self.fmt.bytes();
// Subimages in bottom right corner may not have pixels left for full stride
Some(slice::from_raw_parts(self.pixels, stride * self.height() + width_bytes - stride))
}
}

/// Unsafe, because it depends on the raw pixels pointer being alive
pub unsafe fn pixels_buffer(&self) -> Option<PixelBuffer> {
if self.pixels.is_null() {
return None;
}
let stride_px = self.stride() / self.fmt.bytes();
let buffer_size_px = stride_px * self.height() + self.width() - stride_px;
Some(match self.fmt {
PixelFormat::Bgra32 => {
let buf = slice::from_raw_parts(self.pixels as *const _, buffer_size_px);
PixelBuffer::Bgra32(ImgRef::new_stride(buf, self.width(), self.height(), stride_px))
},
PixelFormat::Bgr32 => {
let buf = slice::from_raw_parts(self.pixels as *const _, buffer_size_px);
PixelBuffer::Bgr32(ImgRef::new_stride(buf, self.width(), self.height(), stride_px))
},
PixelFormat::Bgr24 => {
let buf = slice::from_raw_parts(self.pixels as *const _, buffer_size_px);
PixelBuffer::Bgr24(ImgRef::new_stride(buf, self.width(), self.height(), stride_px))
},
PixelFormat::Gray8 => {
let buf = slice::from_raw_parts(self.pixels as *const _, buffer_size_px);
PixelBuffer::Gray8(ImgRef::new_stride(buf, self.width(), self.height(), stride_px))
},
})
}

pub unsafe fn pixels_slice_mut(&mut self) -> Option<&mut [u8]>{
if self.pixels.is_null() {
None
}else{
Some(slice::from_raw_parts_mut(self.pixels, (self.stride * self.h) as usize))
}
}

pub fn frame_info(&self) -> crate::flow::definitions::FrameInfo {
crate::flow::definitions::FrameInfo {
w: self.w as i32,
h: self.h as i32,
fmt: self.fmt
}
}
/// If the format is Bgr32, set each alpha byte to 0xff
pub fn normalize_alpha(&mut self) -> Result<()>{
if self.fmt == PixelFormat::Bgr32 {
let width_bytes = self.w as usize * self.fmt.bytes();
for h in 0isize..self.h as isize{
let s = unsafe { slice::from_raw_parts_mut(self.pixels.offset(h * self.stride as isize), width_bytes) };
for pix in s.chunks_mut(4) {
pix[3] = 0xff;
}
}
}
Ok(())
}

pub fn is_pointer_null(&self) -> bool{
self.pixels.is_null()
}

/// Call normalize_alpha first; this function does not skip unused alpha bytes, only unused whole pixels.
/// Otherwise Bgr32 may be non-deterministic
pub unsafe fn short_hash_pixels(&self) -> u64{
use std::hash::Hasher;

if self.is_pointer_null(){
panic!("BitmapBgra::short_hash_pixels called on invalid pointer");
}

let width_bytes = self.w as usize * self.fmt.bytes();

let mut hash = ::twox_hash::XxHash64::with_seed(0x8ed1_2ad9_483d_28a0);
for h in 0isize..(self.h as isize){
let row_slice = unsafe{ slice::from_raw_parts(self.pixels.offset(h * self.stride as isize), width_bytes) };
hash.write(row_slice)
}
hash.finish()
}


pub fn fill_rect(&mut self, x1: u32, y1: u32, x2: u32, y2: u32, color: &s::Color) -> Result<()> {
if self.compositing_mode == BitmapCompositingMode::BlendWithMatte{
if (0,0,self.w,self.h) != (x1,y1,x2,y2){
return Err(nerror!(ErrorKind::InvalidArgument, "Cannot draw on a sub-rectangle of a bitmap in BlendWithMatte mode"));
}
}

let color_srgb_argb = color.clone().to_u32_bgra().unwrap();
unsafe {
crate::graphics::fill::flow_bitmap_bgra_fill_rect(self, x1,y1,x2,y2, color_srgb_argb)
.map_err(|e| e.at(here!()))?;

}
Ok(())
}

pub fn get_row_pointers(&self) -> Result<Vec<*mut u8>>{
let mut vec = Vec::with_capacity(self.h as usize);
for y in 0..self.h{
vec.push(unsafe{ self.pixels.offset(self.stride as isize * y as isize) } )
}
Ok(vec)
}

}


#[repr(C)]
#[derive(Copy,Clone,Debug,PartialEq)]
Expand Down
1 change: 0 additions & 1 deletion imageflow_core/src/flow/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::ffi::{BitmapBgra};
pub use crate::ffi::EdgeKind;
pub use crate::ffi::PixelFormat;
use crate::Context;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/flow/nodes/create_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl CreateCanvasNodeDef{
Err(nerror!(crate::ErrorKind::InvalidNodeParams, "canvas format cannot be grayscale; single-channel grayscale bitmaps are not yet supported in Imageflow"))
}else if format == ffi::PixelFormat::Bgr24{
Err(nerror!(crate::ErrorKind::InvalidNodeParams, "canvas format {:?} not permitted. Use Bgr32 instead", format))
}else if format != ffi::PixelFormat::Bgr24 && format != ffi::PixelFormat::Bgr32 && format != ffi::PixelFormat::Bgra32 {
}else if format != ffi::PixelFormat::Bgr32 && format != ffi::PixelFormat::Bgra32 {
Err(nerror!(crate::ErrorKind::InvalidNodeParams, "canvas format {:?} not recognized", format))
} else {
Ok((w,h,format,color.clone()))
Expand Down
3 changes: 1 addition & 2 deletions imageflow_core/src/flow/nodes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use daggy::{Dag, EdgeIndex, NodeIndex, Walker};
use crate::ffi::BitmapBgra;

use libc::size_t;
use petgraph::EdgeDirection;
use petgraph::visit::EdgeRef;
Expand All @@ -23,7 +23,6 @@ mod round_corners;

mod internal_prelude {
pub use crate::ffi;
pub use crate::ffi::BitmapBgra;
pub use crate::flow::definitions::*;
pub use crate::internal_prelude::works_everywhere::*;
pub use petgraph::EdgeDirection;
Expand Down
2 changes: 1 addition & 1 deletion imageflow_core/src/flow/nodes/scale_render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl NodeDefOneInputOneCanvas for DrawImageDef {

let compose = blend.unwrap_or(::imageflow_types::CompositingMode::Compose) == s::CompositingMode::Compose;

// We can write to the temporary BitmapBgra field because we set it on the real bitmap later after we're done

if canvas_bitmap.info().compose() == &BitmapCompositing::ReplaceSelf && compose {
canvas_bitmap.set_compositing(BitmapCompositing::BlendWithSelf);
}
Expand Down
5 changes: 1 addition & 4 deletions imageflow_core/src/flow/visualize.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Graph;
use crate::ffi::BitmapBgra;

use crate::internal_prelude::works_everywhere::*;
use super::definitions::{FrameEstimate, Node, PixelFormat, EdgeKind, NodeResult};

Expand Down Expand Up @@ -86,9 +86,6 @@ pub fn render_dotfile_to_png(dotfile_path: &str) {

static INDENT: &'static str = " ";

fn get_pixel_format_name_for(bitmap: *const BitmapBgra) -> &'static str {
unsafe { get_pixel_format_name((*bitmap).fmt) }
}

fn get_pixel_format_name(fmt: PixelFormat) -> &'static str {
match fmt {
Expand Down
Loading

0 comments on commit 8daa483

Please sign in to comment.