-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from gemini-testing/HERMIONE-451.migrate_to_sharp
feat: add different files format support
- Loading branch information
Showing
24 changed files
with
1,058 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
const ImageBase = require('../image-base'); | ||
|
||
module.exports = class Image extends ImageBase { | ||
constructor(img) { | ||
super(); | ||
|
||
this._img = img; | ||
} | ||
|
||
async init() { | ||
const {data, info} = await this._img.raw().toBuffer({resolveWithObject: true}); | ||
|
||
this._buffer = data; | ||
this._width = info.width; | ||
this._height = info.height; | ||
this._channels = info.channels; | ||
} | ||
|
||
getPixel(x, y) { | ||
const idx = this._getIdx(x, y); | ||
return { | ||
R: this._buffer[idx], | ||
G: this._buffer[idx + 1], | ||
B: this._buffer[idx + 2] | ||
}; | ||
} | ||
|
||
_getIdx(x, y) { | ||
return (this._width * y + x) * this._channels; | ||
} | ||
|
||
async save(path) { | ||
return this._img.toFile(path); | ||
} | ||
|
||
async createBuffer(extension) { | ||
return this._img.toFormat(extension).toBuffer(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs-extra'); | ||
const NestedError = require('nested-error-stacks'); | ||
const sharp = require('sharp'); | ||
const OriginalIMG = require('./original-image'); | ||
const BoundedIMG = require('./bounded-image'); | ||
|
||
const createimage = async (img, {boundingBox} = {}) => { | ||
return boundingBox | ||
? BoundedIMG.create(img, boundingBox) | ||
: OriginalIMG.create(img); | ||
}; | ||
|
||
exports.fromBuffer = async (buffer, opts) => { | ||
const img = sharp(buffer, opts); | ||
return createimage(img, opts); | ||
}; | ||
|
||
exports.fromFile = async (filePath, opts = {}) => { | ||
try { | ||
const buffer = await fs.readFile(filePath); | ||
return exports.fromBuffer(buffer, opts); | ||
} catch (err) { | ||
throw new NestedError(`Can't load img file ${filePath}`, err); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const Image = require('./image'); | ||
|
||
module.exports = class OriginalImage extends Image { | ||
getActualCoord(x, y) { | ||
return {x, y}; | ||
} | ||
|
||
get width() { | ||
return this._width; | ||
} | ||
|
||
get height() { | ||
return this._height; | ||
} | ||
}; |
4 changes: 2 additions & 2 deletions
4
lib/png-buffer/bounded-buffer.js → lib/img-buffer/bounded-buffer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const IMGBuffer = require('./buffer'); | ||
|
||
const IMG_WIDTH_OFFSET = 16; | ||
const IMG_HEIGHT_OFFSET = 20; | ||
|
||
module.exports = class OriginalIMGBuffer extends IMGBuffer { | ||
getActualCoord(x, y) { | ||
return {x, y}; | ||
} | ||
|
||
get width() { | ||
return this._buffer.readUInt32BE(IMG_WIDTH_OFFSET); | ||
} | ||
|
||
get height() { | ||
return this._buffer.readUInt32BE(IMG_HEIGHT_OFFSET); | ||
} | ||
}; |
Oops, something went wrong.