-
Notifications
You must be signed in to change notification settings - Fork 539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bitmap support [WIP] #483
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Jimp from "jimp"; | ||
import { ImageHandlerError, StatusCodes } from "./lib"; | ||
|
||
export class BitmapRequest { | ||
public convertToPng = async (imageBuffer: Buffer): Promise<Buffer> => { | ||
try { | ||
const image = await Jimp.read(imageBuffer); | ||
const buffer = await image.getBufferAsync(Jimp.MIME_PNG); | ||
return buffer; | ||
} catch (err) { | ||
console.error("Error getting image buffer:", err); | ||
throw new ImageHandlerError( | ||
StatusCodes.BAD_REQUEST, | ||
"BitmapRequest::convertToPng", | ||
"It was not possible to convert buffer to png buffer" | ||
); | ||
} | ||
}; | ||
|
||
public isBmp(imageBuffer: Buffer): boolean { | ||
const imageSignature = imageBuffer.subarray(0, 4).toString("hex").toUpperCase(); | ||
if ( | ||
imageSignature === "89504E47" || | ||
imageSignature === "FFD8FFDB" || | ||
imageSignature === "FFD8FFE0" || | ||
imageSignature === "FFD8FFEE" || | ||
imageSignature === "FFD8FFE1" || | ||
imageSignature === "52494646" || | ||
imageSignature === "49492A00" || | ||
imageSignature === "4D4D002A" | ||
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These constants are already defined, in the |
||
) { | ||
return false; | ||
} else { | ||
const bmpMagicNumber = "424d"; | ||
const fileSignature = imageBuffer.subarray(0, 2).toString("hex"); | ||
return fileSignature === bmpMagicNumber; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ | |
"color": "4.2.3", | ||
"color-name": "1.1.4", | ||
"sharp": "^0.31.3", | ||
"aws-sdk": "^2.1356.0" | ||
"aws-sdk": "^2.1356.0", | ||
"jimp": "^0.22.8" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package brings a lot of dependencies, for BMP it uses https://github.com/shaozilee/bmp-js, please refer to https://github.com/jimp-dev/jimp/blob/245f1cf60e0167d4d733ef90e638428d065856b2/packages/type-bmp/package.json#L25 I think you could use |
||
}, | ||
"devDependencies": { | ||
"@types/color": "^3.0.3", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import Jimp from 'jimp'; | ||
import { BitmapRequest } from '../../bitmap-handler'; | ||
import fs from 'fs'; | ||
import { ImageHandlerError, StatusCodes } from '../../lib'; | ||
|
||
// Mock the Jimp module to prevent actual image processing during tests | ||
jest.mock('jimp', () => ({ | ||
read: jest.fn() | ||
})); | ||
|
||
describe('BitmapRequest', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my point of view, you should not consider it as a
|
||
describe('convertToPng', () => { | ||
const mockedRead = Jimp.read as jest.MockedFunction<typeof Jimp.read>; | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should convert the image buffer to PNG format', async () => { | ||
const imageBuffer = Buffer.from('mocked image buffer'); | ||
const pngBuffer = Buffer.from('mocked PNG buffer'); | ||
|
||
mockedRead.mockResolvedValueOnce({ | ||
getBufferAsync: jest.fn().mockResolvedValueOnce(pngBuffer) | ||
} as unknown as Jimp); | ||
|
||
const bitmapRequest = new BitmapRequest(); | ||
const result = await bitmapRequest.convertToPng(imageBuffer); | ||
|
||
expect(mockedRead).toHaveBeenCalledWith(imageBuffer); | ||
expect(result).toEqual(pngBuffer); | ||
}); | ||
|
||
it('should be able to identify bitmap format', async () => { | ||
const bmpImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.bmp'); | ||
const pngImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.png'); | ||
const jpgImage = fs.readFileSync('./test/image/1x1.jpg'); | ||
|
||
const bmpBuffer = Buffer.from(bmpImage); | ||
const pngBuffer = Buffer.from(pngImage); | ||
const jpgBuffer = Buffer.from(jpgImage); | ||
|
||
const bitmapRequest = new BitmapRequest(); | ||
|
||
expect(true).toEqual(bitmapRequest.isBmp(bmpBuffer)); | ||
expect(false).toEqual(bitmapRequest.isBmp(pngBuffer)); | ||
expect(false).toEqual(bitmapRequest.isBmp(jpgBuffer)); | ||
|
||
}); | ||
|
||
it('should convert the image file to PNG format', async () => { | ||
const originalImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.bmp'); | ||
const pngImage = fs.readFileSync('./test/image/4bd7eaa44283d58f4603a240d6ead553.png'); | ||
|
||
const imageBuffer = Buffer.from(originalImage); | ||
const pngBuffer = Buffer.from(pngImage); | ||
|
||
mockedRead.mockResolvedValueOnce({ | ||
getBufferAsync: jest.fn().mockResolvedValueOnce(pngBuffer) | ||
} as unknown as Jimp); | ||
|
||
const bitmapRequest = new BitmapRequest(); | ||
const result = await bitmapRequest.convertToPng(imageBuffer); | ||
|
||
expect(mockedRead).toHaveBeenCalledWith(imageBuffer); | ||
expect(result).toEqual(pngBuffer); | ||
}); | ||
|
||
it('should throw an error if an error occurs during conversion', async () => { | ||
const imageBuffer = Buffer.from('mocked image buffer'); | ||
const error = new ImageHandlerError(StatusCodes.BAD_REQUEST, 'BitmapRequest::convertToPng', 'It was not possible to convert buffer to png buffer'); | ||
|
||
mockedRead.mockRejectedValueOnce(error); | ||
|
||
const bitmapRequest = new BitmapRequest(); | ||
|
||
await expect(bitmapRequest.convertToPng(imageBuffer)).rejects.toThrowError(error); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class mostly converter from BMP to PNG, IMO
ImageConverter
is more appropriate name, or you can get rid of it at all and move intoimage-request.ts
, like