Skip to content

Commit

Permalink
started working on issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ShyykoSerhiy committed Dec 17, 2015
1 parent 7a3bc25 commit 6429f8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,18 @@ var FILTER_TYPES: {
}
];

export function filter(imageData: ImageData): Uint8Array {
const {width, height, data} = imageData;
const bpp = 4; //bits per pixel r,g,b,a
export function filter(imageData: ImageData, colorType: number): Uint8Array {
var {width, height} = imageData;
var data:number[] | Uint8Array = imageData.data;
if (colorType !== 6) {
var temp = new Uint8Array(width * 3 * height);
for (var fromI=0, toI=0; fromI < data.length; fromI+= 4, toI+=3){
PngWriter.copy(data, temp, toI, 3, fromI);
}
data = temp;
}

const bpp = colorType === 6 ? 4 : 3; //bits per pixel r,g,b,a or r,g,b
const byteWidth = width * bpp; //r,g,b,a
var filtered = new Uint8Array((byteWidth + 1) * height);
var filterTypePos = 0;
Expand Down
11 changes: 7 additions & 4 deletions src/PngWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ export class PngWriter {

write(imageData: ImageData, options?: PakoOptions) {
options = options || {};
var {colorType} = options;
colorType = colorType || 6;
var parts: Uint8Array[] = [];
parts.push(new Uint8Array(PngWriter.PNG_SIGNATURE));
parts.push(this.writeIHDRChunk(imageData.width, imageData.height));
var filtered = filter(imageData);//this._filterData(imageData);
parts.push(this.writeIHDRChunk(imageData.width, imageData.height, colorType));
var filtered = filter(imageData, colorType);//this._filterData(imageData);
var compressed = pako.deflate(filtered, Object.assign({
/**
* //compression level 0-9
Expand Down Expand Up @@ -59,13 +61,14 @@ export class PngWriter {
* Creates IHDR chunk (image dimensions, color depth, compression method, etc.)
* @param width of png image
* @param height of png image
* @param color type of image ()
*/
private writeIHDRChunk(width: number, height: number): Uint8Array {
private writeIHDRChunk(width: number, height: number, colorType: number): Uint8Array {
var ihdr = new Uint8Array(13);
PngWriter._writeAsBigEndian(ihdr, width, 0);
PngWriter._writeAsBigEndian(ihdr, height, 4);
ihdr[8] = 8; // Bit depth: 8 bits per sample //todo add this as option maybe (need to recalculate bpp for this)
ihdr[9] = 6; // Color type: 6 = RGBA // todo add this as option maybe (need to recalculate bpp for this)
ihdr[9] = colorType; // Color type: 6 = RGBA // todo add this as option maybe (need to recalculate bpp for this)
ihdr[10] = 0; // Compression method: DEFLATE (pako comes handy)
ihdr[11] = 0; // Filter method: Adaptive
ihdr[12] = 0; // Interlace method: None
Expand Down
3 changes: 2 additions & 1 deletion typings/tsd.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ interface PakoOptions {
level?: number,
windowBits?: number,
chunkSize?: number,
strategy?: number
strategy?: number,
colorType?: number
}

0 comments on commit 6429f8a

Please sign in to comment.