Skip to content

Commit bd045b6

Browse files
committed
add Image#get_gainmap
Add get_gainmap, the `gainmap` flag for keep, and a test.
1 parent 9c9c10f commit bd045b6

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
* move library_name out of the global namespace and into FFI [jcupitt]
66
* update docs for libvips 8.18 [jcupitt]
7-
* version bump to 2.3 ready for upcoming gainmap API
7+
* add Image#get_gainmap [jcupitt]
8+
* version bump to 2.3
89

910
## Version 2.2.5 (2025-08-20)
1011

lib/vips/foreign_keep.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Vips
66
# * `:xmp` keep XMP metadata
77
# * `:iptc` keep IPTC metadata
88
# * `:icc` keep ICC profiles
9+
# * `:gainmap` keep the gainmap metadata
910
# * `:other` keep other metadata
1011

1112
class ForeignKeep < Symbol

lib/vips/image.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ module Vips
5050
attach_function :vips_addalpha, [:pointer, :pointer, :varargs], :int
5151
end
5252

53+
if Vips.at_least_libvips?(8, 18)
54+
attach_function :vips_image_get_gainmap, [:pointer], :pointer
55+
end
56+
5357
# move these three lines to mutableimage when we finally remove set and
5458
# remove in this class
5559
attach_function :vips_image_set,
@@ -797,8 +801,36 @@ def get_fields
797801
names
798802
end
799803

804+
# Get the gainmap (if any) from an image.
805+
#
806+
# After modifying the gainmap, you should write it back to the image in a
807+
# mutable block, see [#mutate], in the field "gainmap".
808+
#
809+
# For example:
810+
#
811+
# ```ruby
812+
# gainmap = image.get_gainmap
813+
# unless gainmap.nil?
814+
# new_gainmap = gainmap.crop left, top, width, height
815+
# image = image.mutate do |x|
816+
# x.set_type! Vips::IMAGE_TYPE, "gainmap", new_gainmap
817+
# end
818+
# end
819+
# ```
820+
#
821+
# @return [Image] the gainmap image, or nil
822+
def get_gainmap
823+
if Vips.at_least_libvips?(8, 18)
824+
vi = Vips.vips_image_get_gainmap self
825+
return nil if vi.null?
826+
Image.new(vi)
827+
else
828+
nil
829+
end
830+
end
831+
800832
# Mutate an image with a block. Inside the block, you can call methods
801-
# which modify the image, such as setting or removing metadata, or
833+
# which modify the image, such as setting or removing metadata or
802834
# modifying pixels.
803835
#
804836
# For example:

spec/image_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,27 @@
726726
end
727727
end
728728

729+
# added in 8.18
730+
if Vips.respond_to? :vips_image_get_gainmap
731+
it "can modify gainmaps" do
732+
image = Vips::Image.new_from_file simg "ultra-hdr.jpg"
733+
# test the example code in image.rb doc comment for get_gainmap
734+
gainmap = image.get_gainmap
735+
unless gainmap.nil?
736+
new_gainmap = gainmap.crop 0, 0, 10, 10
737+
image = image.mutate do |x|
738+
x.set_type! Vips::IMAGE_TYPE, "gainmap", new_gainmap
739+
end
740+
741+
buffer = image.write_to_buffer ".jpg"
742+
image = Vips::Image.new_from_buffer buffer, ""
743+
new_gainmap2 = image.get_gainmap
744+
745+
expect(new_gainmap2.width).to eq(10)
746+
end
747+
end
748+
end
749+
729750
it "can has_alpha?" do
730751
x = Vips::Image.new_from_file "./spec/samples/alpha.png"
731752
expect(x.has_alpha?).to be true

spec/samples/ultra-hdr.jpg

1.14 MB
Loading

0 commit comments

Comments
 (0)