Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created August 28, 2009 12:35
Show Gist options
  • Select an option

  • Save practicingruby/176967 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/176967 to your computer and use it in GitHub Desktop.
def build_jpg_object(data, jpg)
color_space = case jpg.channels
when 1
:DeviceGray
when 3
:DeviceRGB
when 4
:DeviceCMYK
else
raise ArgumentError, 'JPG uses an unsupported number of channels'
end
obj = ref(
:Type => :XObject,
:Subtype => :Image,
:Filter => :DCTDecode,
:ColorSpace => color_space,
:BitsPerComponent => jpg.bits,
:Width => jpg.width,
:Height => jpg.height,
:Length => data.size )
# add extra decode params for CMYK images. By swapping the
# min and max values from the default, we invert the colours. See
# section 4.8.4 of the spec.
if color_space == :DeviceCMYK
obj.data[:Decode] = [ 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0 ]
end
obj << data
return obj
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment