add redact color

This commit is contained in:
Pete Matsyburka
2026-06-15 14:45:06 +03:00
parent 73ce10b26a
commit d1535c4cd8
6 changed files with 68 additions and 14 deletions
+7 -4
View File
@@ -737,7 +737,7 @@ class Pdfium
left = rect['x'].to_f * width
top = height - (rect['y'].to_f * height)
[left, top - (rect['h'].to_f * height), left + (rect['w'].to_f * width), top]
[left, top - (rect['h'].to_f * height), left + (rect['w'].to_f * width), top, rect['color']]
end
unwrap_form_objects(rect_bounds)
@@ -982,7 +982,9 @@ class Pdfium
end
def draw_redaction_rects(rect_bounds)
rect_bounds.each do |left, bottom, right, top|
rect_bounds.each do |left, bottom, right, top, color|
next if color == 'white'
rect_object = Pdfium.FPDFPageObj_CreateNewRect(left, bottom, right - left, top - bottom)
raise PdfiumError, 'Failed to create redaction rect' if rect_object.null?
@@ -1057,7 +1059,7 @@ class Pdfium
a, b, c, d, e, f = matrix
det = (a * d) - (b * c)
rect_bounds.filter_map do |left, bottom, right, top|
rect_bounds.filter_map do |left, bottom, right, top, color|
corners = [[left, bottom], [right, bottom], [left, top], [right, top]].map do |x, y|
u = ((d * (x - e)) - (c * (y - f))) / det
v = ((a * (y - f)) - (b * (x - e))) / det
@@ -1075,7 +1077,8 @@ class Pdfium
[px_left, px_top,
(xs.max.ceil - px_left).clamp(1, image_width - px_left),
(ys.max.ceil - px_top).clamp(1, image_height - px_top)]
(ys.max.ceil - px_top).clamp(1, image_height - px_top),
color]
end
end
+10 -6
View File
@@ -299,19 +299,17 @@ module Templates
else image
end
ink = Array.new(image.bands, 0.0)
pixel_rects.each do |left, top, rect_width, rect_height|
image = image.draw_rect(ink, left, top, rect_width, rect_height, fill: true)
pixel_rects.each do |left, top, rect_width, rect_height, color|
image = image.draw_rect(redaction_ink(image.bands, color), left, top, rect_width, rect_height, fill: true)
end
image.write_to_buffer('.jpg', Q: 50, strip: true)
end
def draw_image_redaction(image, rects)
ink = Array.new(image.bands) { |band| band == 3 ? 255.0 : 0.0 }
rects.each do |rect|
ink = redaction_ink(image.bands, rect['color'])
left = (rect['x'].to_f * image.width).floor.clamp(0, image.width - 1)
top = (rect['y'].to_f * image.height).floor.clamp(0, image.height - 1)
rect_width = (rect['w'].to_f * image.width).ceil.clamp(1, image.width - left)
@@ -323,6 +321,12 @@ module Templates
image
end
def redaction_ink(bands, color)
value = color == 'white' ? 255.0 : 0.0
Array.new(bands) { |band| band == 3 ? 255.0 : value }
end
def open_or_build_pdf(attachment, redact: nil, rotate: nil, pdf_size: nil, default_size: nil)
data =
if attachment.image?