### Home Classes Methods

### In Files

- lib/prawn/images/jpg.rb

### Parent

Prawn::Images::Image

### Methods

- ::can_render?
- ::new
- #build_pdf_object

### Class/Module Index

Quicksearch

- Prawn
- Prawn::Configurable
- Prawn::Document
- Prawn::Document::BoundingBox
- Prawn::Document::ColumnBox
- Prawn::Document::GraphicsState
- Prawn::Document::Grid
- Prawn::Document::GridBox
- Prawn::Document::Internals
- Prawn::Document::Security
- Prawn::Document::Snapshot
- Prawn::Encoding
- Prawn::Errors
- Prawn::Font
- Prawn::Font::AFM
- Prawn::Font::DFont
- Prawn::Font::TTF
- Prawn::FontMetricCache
- Prawn::Graphics
- Prawn::Graphics::CapStyle
- Prawn::Graphics::Color
- Prawn::Graphics::Dash
- Prawn::Graphics::JoinStyle
- Prawn::Graphics::Patterns
- Prawn::Graphics::Transformation
- Prawn::Graphics::Transparency
- Prawn::ImageHandler
- Prawn::Images
- Prawn::Images::Image
- Prawn::Images::JPG
- Prawn::Images::PNG
- Prawn::Measurements
- Prawn::SoftMask
- Prawn::Stamp
- Prawn::SynchronizedCache
- Prawn::Table
- Prawn::Table::Cell
- Prawn::Table::Cell::Image
- Prawn::Table::Cell::InTable
- Prawn::Table::Cell::SpanDummy
- Prawn::Table::Cell::Subtable
- Prawn::Table::Cell::Text
- Prawn::Table::Cells
- Prawn::Table::ColumnWidthCalculator
- Prawn::Text
- Prawn::Text::Box
- Prawn::Text::Formatted
- Prawn::Text::Formatted::Box
- Prawn::Text::Formatted::Fragment
- Prawn::Text::Formatted::Parser
- Prawn::ThreadLocalCache
- PDF
- PDF::Core
- PDF::Core::Reference
- PDF::Core::Stream
- Arcfour
- Numeric

No matching classes.

# Prawn::Images::JPG

A convenience class that wraps the logic for extracting the parts of a JPG image that we need to embed them in a PDF

### Constants

JPEG_SOF_BLOCKS

### Attributes

bits[R]

@group Extension API

channels[R]

@group Extension API

height[R]

@group Extension API

scaled_height[RW]
scaled_width[RW]
width[R]

@group Extension API

### Public Class Methods

can_render?(image_blob) click to toggle source

    # File lib/prawn/images/jpg.rb, line 24
    def self.can_render?(image_blob)
      image_blob[0, 3].unpack("C*") == [255, 216, 255]
    end

new(data) click to toggle source

Process a new JPG image

`:data` | A binary string of JPEG data

    # File lib/prawn/images/jpg.rb, line 32
    def initialize(data)
      @data = data
      d = StringIO.new(@data)
      d.binmode

      c_marker = 0xff # Section marker.
      d.seek(2)    # Skip the first two bytes of JPEG identifier.
      loop do
        marker, code, length = d.read(4).unpack('CCn')
        raise "JPEG marker not found!" if marker != c_marker

        if JPEG_SOF_BLOCKS.include?(code)
          @bits, @height, @width, @channels = d.read(6).unpack("CnnC")
          break
        end

        d.seek(length - 2, IO::SEEK_CUR)
      end
    end

### Public Instance Methods

build_pdf_object(document) click to toggle source

Build a PDF object representing this image in `document`, and return a Reference to it.

    # File lib/prawn/images/jpg.rb, line 55
    def build_pdf_object(document)
      color_space = case channels
      when 1
        :DeviceGray
      when 3
        :DeviceRGB
      when 4
        :DeviceCMYK
      else
        raise ArgumentError, 'JPG uses an unsupported number of channels'
      end

      obj = document.ref!(
        :Type             => :XObject,
        :Subtype          => :Image,
        :ColorSpace       => color_space,
        :BitsPerComponent => bits,
        :Width            => width,
        :Height           => height
      )

      # 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.stream << @data
      obj.stream.filters << :DCTDecode
      obj
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.