Skip to content

Commit

Permalink
Fix image height
Browse files Browse the repository at this point in the history
  • Loading branch information
fiahfy committed Nov 15, 2019
1 parent e57e77c commit 7ae56ed
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 27 deletions.
13 changes: 6 additions & 7 deletions src/ico-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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[] = []
Expand All @@ -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)
Expand All @@ -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))
}

Expand All @@ -103,7 +102,7 @@ export class IcoImage {
const header = new BitmapInfoHeader(
40,
width,
height,
height * 2, // image + mask
planes,
bitCount,
0,
Expand Down
2 changes: 1 addition & 1 deletion src/ico.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions test/bitmap-info-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions test/ico-file-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
19 changes: 12 additions & 7 deletions test/ico-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
4 changes: 2 additions & 2 deletions test/ico-info-header.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
28 changes: 22 additions & 6 deletions test/ico.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand All @@ -12,15 +12,15 @@ 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)
})
})

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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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')

Expand Down

0 comments on commit 7ae56ed

Please sign in to comment.