diff --git a/src/ico-image.ts b/src/ico-image.ts index eda20a3..c1f4138 100644 --- a/src/ico-image.ts +++ b/src/ico-image.ts @@ -53,7 +53,7 @@ export class IcoImage { } const width = png.width - let height = png.height + const height = png.height if (width !== height) { throw new TypeError('Image must be squre') } @@ -62,9 +62,9 @@ export class IcoImage { throw new TypeError('No supported size') } - height *= 2 // image + mask + const bpp = 4 const planes = 1 - const bitCount = (png as any).bpp * 8 // byte per pixel * 8 + const bitCount = bpp * 8 // byte per pixel * 8 const xors = [] let andBits: number[] = [] @@ -73,7 +73,7 @@ export class IcoImage { for (let y = height - 1; y >= 0; y--) { for (let x = 0; x < width; x++) { // RGBA to BGRA - const pos = (y * width + x) * (png as any).bpp + const pos = (y * width + x) * bpp const red = png.data.slice(pos, pos + 1) const green = png.data.slice(pos + 1, pos + 2) const blue = png.data.slice(pos + 2, pos + 3) @@ -84,8 +84,7 @@ export class IcoImage { xors.push(alpha) andBits.push(alpha.readUInt8(0) === 0 ? 1 : 0) } - const padding: number = - andBits.length % 32 ? 32 - (andBits.length % 32) : 0 + const padding = andBits.length % 32 ? 32 - (andBits.length % 32) : 0 andBits = andBits.concat(Array(padding).fill(0)) } @@ -103,7 +102,7 @@ export class IcoImage { const header = new BitmapInfoHeader( 40, width, - height, + height * 2, // image + mask planes, bitCount, 0, diff --git a/src/ico.ts b/src/ico.ts index 67161da..b65a201 100644 --- a/src/ico.ts +++ b/src/ico.ts @@ -68,7 +68,7 @@ export class Ico { const infoHeaders = this._images.map((image) => { return new IcoInfoHeader( image.header.width < 256 ? image.header.width : 0, - image.header.height < 256 ? image.header.height : 0, + image.header.height / 2 < 256 ? image.header.height / 2 : 0, 0, 0, image.header.planes, diff --git a/test/bitmap-info-header.test.ts b/test/bitmap-info-header.test.ts index 8212103..54168a9 100644 --- a/test/bitmap-info-header.test.ts +++ b/test/bitmap-info-header.test.ts @@ -2,7 +2,7 @@ import { BitmapInfoHeader } from '../src' describe('BitmapInfoHeader', () => { describe('constructor', () => { - test('should work', () => { + it('should work', () => { const header = new BitmapInfoHeader() expect(header.size).toBe(40) expect(header.width).toBe(0) @@ -19,7 +19,7 @@ describe('BitmapInfoHeader', () => { }) describe('from', () => { - test('should work', () => { + it('should work', () => { const buffer = Buffer.alloc(40) buffer.writeUInt32LE(40, 0) buffer.writeInt32LE(32, 4) diff --git a/test/ico-file-header.test.ts b/test/ico-file-header.test.ts index 27d8916..5f552fd 100644 --- a/test/ico-file-header.test.ts +++ b/test/ico-file-header.test.ts @@ -2,7 +2,7 @@ import { IcoFileHeader } from '../src' describe('IcoFileHeader', () => { describe('constructor', () => { - test('should work', () => { + it('should work', () => { const header = new IcoFileHeader() expect(header.reserved).toBe(0) expect(header.type).toBe(1) @@ -11,7 +11,7 @@ describe('IcoFileHeader', () => { }) describe('from', () => { - test('should work', () => { + it('should work', () => { const buffer = Buffer.alloc(6) buffer.writeUInt16LE(1, 0) buffer.writeUInt16LE(2, 2) diff --git a/test/ico-image.test.ts b/test/ico-image.test.ts index 8952793..f47c349 100644 --- a/test/ico-image.test.ts +++ b/test/ico-image.test.ts @@ -3,7 +3,7 @@ import { IcoImage } from '../src' describe('IcoImage', () => { describe('constructor', () => { - test('should work', () => { + it('should work', () => { const image = new IcoImage() expect(image.header.data.length).toBe(40) expect(image.xor.length).toBe(0) @@ -12,19 +12,24 @@ describe('IcoImage', () => { }) describe('fromPNG', () => { - test('should work', () => { - const buffer = fs.readFileSync('./test/256x256.png') - expect(() => IcoImage.fromPNG(buffer)).not.toThrowError() + it('should work', () => { + const buffer = fs.readFileSync('./test/16x16.png') + const image = IcoImage.fromPNG(buffer) + expect(image.header.size).toBe(40) + expect(image.header.width).toBe(16) + expect(image.header.height).toBe(32) + expect(image.header.planes).toBe(1) + expect(image.header.compression).toBe(0) }) - test('should throw error if buffer is not PNG format', () => { + it('should throw error if buffer is not PNG format', () => { const buffer = fs.readFileSync('./test/256x256.jpg') expect(() => IcoImage.fromPNG(buffer)).toThrowError(TypeError) }) - test('should throw error if buffer is not square', () => { + it('should throw error if buffer is not square', () => { const buffer = fs.readFileSync('./test/256x128.png') expect(() => IcoImage.fromPNG(buffer)).toThrowError(TypeError) }) - test('should throw error if buffer is not supported size', () => { + it('should throw error if buffer is not supported size', () => { const buffer = fs.readFileSync('./test/100x100.png') expect(() => IcoImage.fromPNG(buffer)).toThrowError(TypeError) }) diff --git a/test/ico-info-header.test.ts b/test/ico-info-header.test.ts index 7bc5d5d..0d1398f 100644 --- a/test/ico-info-header.test.ts +++ b/test/ico-info-header.test.ts @@ -2,7 +2,7 @@ import { IcoInfoHeader } from '../src' describe('IcoInfoHeader', () => { describe('constructor', () => { - test('should work', () => { + it('should work', () => { const header = new IcoInfoHeader() expect(header.width).toBe(0) expect(header.height).toBe(0) @@ -16,7 +16,7 @@ describe('IcoInfoHeader', () => { }) describe('from', () => { - test('should work', () => { + it('should work', () => { const buffer = Buffer.alloc(16) buffer.writeUInt8(32, 0) buffer.writeUInt8(32, 1) diff --git a/test/ico.test.ts b/test/ico.test.ts index fd819b7..6e7c990 100644 --- a/test/ico.test.ts +++ b/test/ico.test.ts @@ -3,7 +3,7 @@ import { Ico, IcoImage, IcoFileHeader } from '../src' describe('Ico', () => { describe('constructor', () => { - test('should work', () => { + it('should work', () => { const ico = new Ico() expect(ico.fileHeader).toEqual(new IcoFileHeader()) expect(ico.infoHeaders).toEqual([]) @@ -12,7 +12,7 @@ describe('Ico', () => { }) describe('from', () => { - test('should work', () => { + it('should work', () => { const buffer = fs.readFileSync('./test/icon.ico') const ico = Ico.from(buffer) expect(ico.images.length).toBe(7) @@ -20,7 +20,7 @@ describe('Ico', () => { }) describe('set images', () => { - test('should work', () => { + it('should work', () => { const ico = new Ico() const buffer = fs.readFileSync('./test/16x16.png') const firstBytes = ico.data.length @@ -94,10 +94,26 @@ describe('Ico', () => { expect(ico.infoHeaders.length).toBe(0) expect(ico.data.length).toBe(firstBytes) }) + it('should create valid info header', () => { + const ico = new Ico() + const buffer = fs.readFileSync('./test/16x16.png') + const image = IcoImage.fromPNG(buffer) + ico.append(image) + expect(ico.infoHeaders[0].width).toBe(16) + expect(ico.infoHeaders[0].height).toBe(16) + expect(ico.infoHeaders[0].colorCount).toBe(0) + expect(ico.infoHeaders[0].reserved).toBe(0) + expect(ico.infoHeaders[0].planes).toBe(1) + expect(ico.infoHeaders[0].bitCount).toBe(32) + expect(ico.infoHeaders[0].bytesInRes).toBe(image.data.length) + expect(ico.infoHeaders[0].imageOffset).toBe( + ico.fileHeader.data.length + ico.infoHeaders[0].data.length + ) + }) }) describe('append', () => { - test('should work', () => { + it('should work', () => { const ico = new Ico() const buffer = fs.readFileSync('./test/16x16.png') let image: IcoImage @@ -113,7 +129,7 @@ describe('Ico', () => { }) describe('insert', () => { - test('should work', () => { + it('should work', () => { const ico = new Ico() const buffer = fs.readFileSync('./test/16x16.png') let image: IcoImage @@ -133,7 +149,7 @@ describe('Ico', () => { }) describe('remove', () => { - test('should work', () => { + it('should work', () => { const ico = new Ico() const buffer = fs.readFileSync('./test/16x16.png')