Skip to content

Commit

Permalink
Fix bug in drawing squares, implement find_differences
Browse files Browse the repository at this point in the history
  • Loading branch information
Vici37 committed Oct 10, 2023
1 parent 132ae05 commit 60d41ac
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
test:
crystal spec --profile --rand --error-trace

test-no-rand:
crystal spec --profile --error-trace

benchmark:
crystal run scripts/benchmark.cr --release

Expand Down
16 changes: 8 additions & 8 deletions spec/cr-image/operation/draw_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ Spectator.describe CrImage::Operation::Draw do
alias Color = CrImage::Color

specs_for_operator(draw_square(image.to_gray.threshold(8).region, Color.of("#00f")),
gray_hash: "23be9687013d598d9416fa3cf428d699f5fe9572",
rgba_hash: "1235b84c0debd42b71b5d20786386f7451928321"
gray_hash: "4d8323c4cfce2cae736185b0b714252ac4547f79",
rgba_hash: "ba0d7b953555121c252843f3db01d0a1c2c9dd89"
)

specs_for_operator(draw_square!(image.to_gray.threshold(8).region, Color.of("#00f")),
gray_hash: "23be9687013d598d9416fa3cf428d699f5fe9572",
rgba_hash: "1235b84c0debd42b71b5d20786386f7451928321",
gray_hash: "4d8323c4cfce2cae736185b0b714252ac4547f79",
rgba_hash: "ba0d7b953555121c252843f3db01d0a1c2c9dd89",
)

specs_for_operator(draw_square(20, 20, 100, 100, Color.of("#00f")),
gray_hash: "1ed4dcb15c6bc97af31eb8dadd05717eea09c082",
rgba_hash: "6aa0ca38d5e245291632f1d6f2861bad5e05d4be"
gray_hash: "48acaf112aa49d6b7d8073b37883f03fb4c2b2e5",
rgba_hash: "ad5686a6597cdf660077d9457ddd6b3a1733a2b3"
)

specs_for_operator(draw_square!(20, 20, 100, 100, Color.of("#00f")),
gray_hash: "1ed4dcb15c6bc97af31eb8dadd05717eea09c082",
rgba_hash: "6aa0ca38d5e245291632f1d6f2861bad5e05d4be",
gray_hash: "48acaf112aa49d6b7d8073b37883f03fb4c2b2e5",
rgba_hash: "ad5686a6597cdf660077d9457ddd6b3a1733a2b3",
)

specs_for_operator(draw_square(20, 20, 100, 100, Color.of("#0f0"), fill: true),
Expand Down
8 changes: 8 additions & 0 deletions src/cr-image/map.cr
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module CrImage
abstract def sum : T
abstract def max : T
abstract def min : T

abstract def -(other : NumericMap(T)) : NumericMap(T)
abstract def +(other : NumericMap(T)) : NumericMap(T)
end

# A collection of implementations of `NumericMap(T)`, intended to be included in base classes.
Expand Down Expand Up @@ -190,6 +193,11 @@ module CrImage
@raw.sum
end

def -(other : NumericMap(T)) : self
raise Exception.new "Unable to subtract a map of different dimensions: #{width}x#{height} vs #{other.width}x#{other.height}" unless width == other.width && height == other.height
self.class.new(width, raw.map_with_index { |v, i| v - other.raw[i] })
end

# Construct a `Mask` from this `GrayscaleImage` using the passed in block to determine if a given pixel should be true or not
#
# ```
Expand Down
4 changes: 4 additions & 0 deletions src/cr-image/mask.cr
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,8 @@ class CrImage::Mask
def closing!(*, diagonal : Bool = true) : Mask
dilate!(diagonal: diagonal).erode!(diagonal: diagonal)
end

def |(other : Mask) : Mask
Mask.new(width, BitArray.new(size) { |i| bits[i] | other.bits[i] })
end
end
6 changes: 3 additions & 3 deletions src/cr-image/operation/draw.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ module CrImage::Operation::Draw
end

def draw_square!(x : Int, y : Int, box_width : Int, box_height : Int, color : Color, *, fill : Bool = false) : self
raise "Box dimensions extend #{x + box_width - width} pixels beyond width of the image (#{width})" if (x + box_width) > width
raise "Box dimensions extend #{y + box_height - height} pixels beyond height of the image (#{height})" if (y + box_height) > height
raise "Box dimensions extend #{x + box_width - width + 1} pixels beyond width of the image (#{width})" if (x + box_width) > width
raise "Box dimensions extend #{y + box_height - height + 1} pixels beyond height of the image (#{height})" if (y + box_height) > height

x_i = x.to_i
y_i = y.to_i
Expand All @@ -42,7 +42,7 @@ module CrImage::Operation::Draw
channel[((y_i + i) * width + x_i), box_width] = Array(UInt8).new(box_width) { color[channel_type] }
else
channel.unsafe_put((y_i + i) * width + x_i, color[channel_type])
channel.unsafe_put((y_i + i) * width + x_i + box_width, color[channel_type])
channel.unsafe_put((y_i + i) * width + x_i + box_width - 1, color[channel_type])
end
end
end
Expand Down
1 change: 1 addition & 0 deletions src/tutorials/tutorial.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
# See:
# * `EdgeDetection`
# * `ChromaKey`
# * `FindDifferences`
module Tutorial
end

0 comments on commit 60d41ac

Please sign in to comment.