Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
Extracting PNG splitter strategy from IIIF Print
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyf committed Mar 27, 2023
1 parent 156ec24 commit cafb399
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/space_stone/pdf_splitter/strategies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module Strategies

require 'space_stone/pdf_splitter/strategies/base'
require 'space_stone/pdf_splitter/strategies/jpg'
require 'space_stone/pdf_splitter/strategies/png'
require 'space_stone/pdf_splitter/strategies/tiff'
26 changes: 26 additions & 0 deletions lib/space_stone/pdf_splitter/strategies/png.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module SpaceStone
module PdfSplitter
module Strategies
# The purpose of this class is to split the PDF into constituent png files.
class Png < Strategies::Base
self.image_extension = 'png'

def gsdevice
return @gsdevice if defined?(@gsdevice)

color = pdf_pages_summary.color_description
bits_per_channel = pdf_pages_summary.bits_per_channel
if color == 'gray'
# 1 Bit Grayscale, if applicable:
return @gsdevice = 'pngmonod' if bits_per_channel == 1
return @gsdevice = 'pnggray' if bits_per_channel > 1
end

@gsdevice = 'png16m'
end
end
end
end
end
44 changes: 44 additions & 0 deletions spec/space_stone/pdf_splitter/strategies/png_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'spec_helper'

RSpec.describe SpaceStone::PdfSplitter::Strategies::Png do
let(:path) { __FILE__ }
let(:pdf_pages_summary) { double(SpaceStone::PdfSplitter::PdfPagesSummary) }

let(:splitter) { described_class.new(path, pdf_pages_summary: pdf_pages_summary) }

describe '.compression' do
subject { described_class.compression }
it { is_expected.to be_nil }
end

describe '.compression?' do
subject { described_class.compression? }
it { is_expected.to be_falsey }
end

describe '.image_extension' do
subject { described_class.image_extension }
it { is_expected.to eq('png') }
end

describe '#gsdevice' do
DEFAULT_SUMMARY_ATTRIBUTES = {
color_description: 'rgb',
bits_per_channel: 0
}.freeze

[
[{ color_description: 'gray', bits_per_channel: 2 }, 'pnggray'],
[{ color_description: 'gray', bits_per_channel: 1 }, 'pngmonod'],
[{ color_description: 'rgb', bits_per_channel: 1 }, 'png16m']
].each do |attributes, expected_value|
context 'with #{attributes.inspect}' do
it "is expected to be #{expected_value.inspect}" do
summary = SpaceStone::PdfSplitter::PdfPagesSummary.new(**DEFAULT_SUMMARY_ATTRIBUTES.merge(attributes))
expect(described_class.new(__FILE__, pdf_pages_summary: summary).gsdevice).to eq(expected_value)
end
end
end
end
end

0 comments on commit cafb399

Please sign in to comment.