Skip to content

Commit

Permalink
Fix missing alpha channel for resizing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Vici37 committed Jul 15, 2023
1 parent f153d23 commit 94a7505
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/cr-image/grayscale_image.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/cr-image/image.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 1 addition & 2 deletions src/cr-image/operation/bilinear_resize.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion src/cr-image/operation/crop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 5 additions & 15 deletions src/cr-image/operation/pad.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/cr-image/rgba_image.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 94a7505

Please sign in to comment.