diff --git a/src/cr-image/grayscale_image.cr b/src/cr-image/grayscale_image.cr index def0488..f3fe36a 100644 --- a/src/cr-image/grayscale_image.cr +++ b/src/cr-image/grayscale_image.cr @@ -71,6 +71,13 @@ class CrImage::GrayscaleImage < CrImage::Image nil end + # Run provided block with the `ChannelType::Gray` channels and channel types. + def each_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil + yield @gray, ChannelType::Gray + yield @alpha, ChannelType::Alpha + nil + end + record Pixel, gray : UInt8, alpha : UInt8 diff --git a/src/cr-image/image.cr b/src/cr-image/image.cr index 7d088d5..f806259 100644 --- a/src/cr-image/image.cr +++ b/src/cr-image/image.cr @@ -48,6 +48,9 @@ abstract class CrImage::Image # Run provided block on each channel supported by this image. abstract def each_color_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil + # Run provided block on each channel supported by this image. + abstract def each_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil + # Get the `Array(UInt8)` corresponding to `channel_type`) abstract def [](channel_type : ChannelType) : Array(UInt8) # Set the `Array(UInt8)` corresponding to `channel_type`) to `channel` diff --git a/src/cr-image/operation/bilinear_resize.cr b/src/cr-image/operation/bilinear_resize.cr index f711510..cd6f646 100644 --- a/src/cr-image/operation/bilinear_resize.cr +++ b/src/cr-image/operation/bilinear_resize.cr @@ -22,8 +22,7 @@ module CrImage::Operation::BilinearResize x_ratio = width > 1 ? (@width - 1) / (width - 1) : 0 y_ratio = height > 1 ? (@height - 1) / (height - 1) : 0 - # TODO: resize alpha channel - each_color_channel do |channel, channel_type| + each_channel do |channel, channel_type| resized_channel = Array.new(width * height) { 0u8 } height.times do |h| diff --git a/src/cr-image/operation/crop.cr b/src/cr-image/operation/crop.cr index 0cfdda6..79f6535 100644 --- a/src/cr-image/operation/crop.cr +++ b/src/cr-image/operation/crop.cr @@ -30,7 +30,7 @@ module CrImage::Operation::Crop end def crop!(x : Int32, y : Int32, new_width : Int32, new_height : Int32) : self - each_color_channel do |channel, channel_type| + each_channel do |channel, channel_type| self[channel_type] = UInt8Map.new(width, channel)[x, new_width, y, new_height].raw end diff --git a/src/cr-image/operation/pad.cr b/src/cr-image/operation/pad.cr index 0a7a1e1..b008975 100644 --- a/src/cr-image/operation/pad.cr +++ b/src/cr-image/operation/pad.cr @@ -16,24 +16,14 @@ module CrImage::Operation::Pad end def pad!(all : Int32 = 0, *, top : Int32 = 0, bottom : Int32 = 0, left : Int32 = 0, right : Int32 = 0, pad_type : EdgePolicy = EdgePolicy::Black) : self - # mem_start = GC.stats.total_bytes - new_width = width - new_height = height - # puts "Memory start: #{GC.stats.total_bytes - mem_start}" - # TODO: resize alpha channel - each_color_channel do |channel, channel_type| - padded = UInt8Map.new(width, channel).pad(all, top: top, bottom: bottom, left: left, right: right, pad_type: pad_type) + orig_width = width + each_channel do |channel, channel_type| + padded = UInt8Map.new(orig_width, channel).pad(all, top: top, bottom: bottom, left: left, right: right, pad_type: pad_type) self[channel_type] = padded.raw - new_width = padded.width - new_height = padded.height - # puts "Memory after #{channel_type}: #{GC.stats.total_bytes - mem_start}" + @width = padded.width + @height = padded.height end - # puts "Memory final: #{GC.stats.total_bytes - mem_start}" - - @width = new_width - @height = new_height - self end end diff --git a/src/cr-image/rgba_image.cr b/src/cr-image/rgba_image.cr index 1cbd3d0..4639ce9 100644 --- a/src/cr-image/rgba_image.cr +++ b/src/cr-image/rgba_image.cr @@ -30,6 +30,15 @@ class CrImage::RGBAImage < CrImage::Image nil end + # Run provided block with the `ChannelType::Red`, `ChannelType::Green`, and `ChannelType::Blue` channels. + def each_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil + yield @red, ChannelType::Red + yield @green, ChannelType::Green + yield @blue, ChannelType::Blue + yield @alpha, ChannelType::Alpha + nil + end + record Pixel, red : UInt8, green : UInt8,