mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Refactor jpg.js and include forceRGBoutput, correct style of image.js
This refactors getData to be more readable and extracts all the color conversion algorithms to their own functions. The resulting code was then cleaned up. This also introduces a flag `forceRGBoutput` to getData, that allows to always get the data as a `width * height * 3` bytes long RGB buffer
This commit is contained in:
parent
26bea20c82
commit
e7fe45a5c4
2 changed files with 122 additions and 88 deletions
|
@ -44,6 +44,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
return Promise.resolve(image);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode and clamp a value. The formula is different from the spec because we
|
||||
* don't decode to float range [0,1], we decode it in the [0,max] range.
|
||||
|
@ -53,6 +54,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
// Clamp the value to the range
|
||||
return (value < 0 ? 0 : (value > max ? max : value));
|
||||
}
|
||||
|
||||
function PDFImage(xref, res, image, inline, smask, mask, isMask) {
|
||||
this.image = image;
|
||||
var dict = image.dict;
|
||||
|
@ -279,11 +281,13 @@ var PDFImage = (function PDFImageClosure() {
|
|||
this.smask && this.smask.width || 0,
|
||||
this.mask && this.mask.width || 0);
|
||||
},
|
||||
|
||||
get drawHeight() {
|
||||
return Math.max(this.height,
|
||||
this.smask && this.smask.height || 0,
|
||||
this.mask && this.mask.height || 0);
|
||||
},
|
||||
|
||||
decodeBuffer: function PDFImage_decodeBuffer(buffer) {
|
||||
var bpc = this.bpc;
|
||||
var numComps = this.numComps;
|
||||
|
@ -309,6 +313,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
}
|
||||
}
|
||||
},
|
||||
|
||||
getComponents: function PDFImage_getComponents(buffer) {
|
||||
var bpc = this.bpc;
|
||||
|
||||
|
@ -385,6 +390,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
}
|
||||
return output;
|
||||
},
|
||||
|
||||
fillOpacity: function PDFImage_fillOpacity(rgbaBuf, width, height,
|
||||
actualHeight, image) {
|
||||
var smask = this.smask;
|
||||
|
@ -451,6 +457,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
}
|
||||
}
|
||||
},
|
||||
|
||||
undoPreblend: function PDFImage_undoPreblend(buffer, width, height) {
|
||||
var matte = this.smask && this.smask.matte;
|
||||
if (!matte) {
|
||||
|
@ -467,7 +474,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
var alpha = buffer[i + 3];
|
||||
if (alpha === 0) {
|
||||
// according formula we have to get Infinity in all components
|
||||
// making it as white (tipical paper color) should be okay
|
||||
// making it white (tipical paper color) should be okay
|
||||
buffer[i] = 255;
|
||||
buffer[i + 1] = 255;
|
||||
buffer[i + 2] = 255;
|
||||
|
@ -479,6 +486,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
buffer[i + 2] = clamp((buffer[i + 2] - matteRgb[2]) * k + matteRgb[2]);
|
||||
}
|
||||
},
|
||||
|
||||
createImageData: function PDFImage_createImageData(forceRGBA) {
|
||||
var drawWidth = this.drawWidth;
|
||||
var drawHeight = this.drawHeight;
|
||||
|
@ -529,7 +537,6 @@ var PDFImage = (function PDFImageClosure() {
|
|||
return imgData;
|
||||
}
|
||||
}
|
||||
|
||||
// imgArray can be incomplete (e.g. after CCITT fax encoding).
|
||||
var actualHeight = 0 | (imgArray.length / rowBytes *
|
||||
drawHeight / originalHeight);
|
||||
|
@ -567,6 +574,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
|
||||
return imgData;
|
||||
},
|
||||
|
||||
fillGrayBuffer: function PDFImage_fillGrayBuffer(buffer) {
|
||||
var numComps = this.numComps;
|
||||
if (numComps != 1) {
|
||||
|
@ -589,7 +597,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
length = width * height;
|
||||
if (this.needsDecode) {
|
||||
// invert and scale to {0, 255}
|
||||
for (i = 0; i < length; ++i) {
|
||||
for (var i = 0; i < length; ++i) {
|
||||
buffer[i] = (comps[i] - 1) & 255;
|
||||
}
|
||||
} else {
|
||||
|
@ -611,6 +619,7 @@ var PDFImage = (function PDFImageClosure() {
|
|||
buffer[i] = (scale * comps[i]) | 0;
|
||||
}
|
||||
},
|
||||
|
||||
getImageBytes: function PDFImage_getImageBytes(length) {
|
||||
this.image.reset();
|
||||
return this.image.getBytes(length);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue