pdf.js
Version:
A PDF generation library for Node.js
59 lines (46 loc) • 1.8 kB
text/coffeescript
fs = require 'fs'
Data = '../data'
class JPEG
: (filename) ->
contents = fs.readFileSync filename
data = new Data(contents)
new JPEG(data)
constructor: ( ) ->
len = data.length
if data.readUInt16() isnt 0xFFD8
throw "SOI not found in JPEG"
markers = [0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7,
0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF]
while data.pos < len
marker = data.readUInt16()
break if marker in markers
data.pos += data.readUInt16()
throw "Invalid JPEG." unless marker in markers
data.pos += 2
= data.readByte()
= data.readShort()
= data.readShort()
channels = data.readByte()
= switch channels
when 1 then 'DeviceGray'
when 3 then 'DeviceRGB'
when 4 then 'DeviceCMYK'
=
object: (document) ->
obj = document.ref
Type: 'XObject'
Subtype: 'Image'
BitsPerComponent:
Width:
Height:
Length: .length
ColorSpace:
Filter: 'DCTDecode'
# add extra decode params for CMYK images. By swapping the
# min and max values from the default, we invert the colors. See
# section 4.8.4 of the spec.
if is 'DeviceCMYK'
obj.data['Decode'] = [1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0]
obj.add .data
return obj
module.exports = JPEG