Skip to content

Commit

Permalink
refactor: rename pixel() to pixels()
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Nov 15, 2022
1 parent 6d03ebe commit e3061b4
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 9 deletions.
9 changes: 4 additions & 5 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import { Resvg, renderAsync } from '../index'
* @param {Number} height image height
* @returns {Array}, e.g. [255, 0, 0, 255, 255, 0, 0, 255]
*/
async function imgToRgbaPixel(imgBuffer, width, height) {
async function imgToRgbaPixels(imgBuffer: Buffer, width: number, height: number) {
const result = await jimp.read(imgBuffer)

const pixels = []
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const pixel = jimp.intToRGBA(result.getPixelColor(x, y))
// const rgba = `${pixel.r}, ${pixel.g}, ${pixel.b}, ${pixel.a}`
pixels.push(pixel.r)
pixels.push(pixel.g)
pixels.push(pixel.b)
Expand All @@ -32,7 +31,7 @@ async function imgToRgbaPixel(imgBuffer, width, height) {
return pixels
}

test.only('svg to RGBA pixels Array', async (t) => {
test('svg to RGBA pixels Array', async (t) => {
const svg = `<svg width="10px" height="5px" viewBox="0 0 10 5" version="1.1" xmlns="http://www.w3.org/2000/svg">
<rect fill="red" x="0" y="0" width="5" height="5"></rect>
<rect fill="green" x="5" y="0" width="5" height="5"></rect>
Expand All @@ -41,8 +40,8 @@ test.only('svg to RGBA pixels Array', async (t) => {
const pngData = resvg.render()
const pngBuffer = pngData.asPng()

const originPixels = pngData.pixel.toJSON().data
const pixelArray = await imgToRgbaPixel(pngBuffer, pngData.width, pngData.height)
const originPixels = pngData.pixels.toJSON().data
const pixelArray = await imgToRgbaPixels(pngBuffer, pngData.width, pngData.height)

t.is(originPixels.length, pixelArray.length)
t.is(originPixels.toString(), pixelArray.toString())
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class RenderedImage {
asPng(): Buffer

/** Get the RGBA pixels of the image */
get pixel(): Buffer
get pixels(): Buffer

/** Get the PNG width */
get width(): number
Expand Down
2 changes: 1 addition & 1 deletion js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class RenderedImage {
/** Write the image data to Buffer */
asPng(): Buffer
/** Get the RGBA pixels of the image */
get pixel(): Buffer
get pixels(): Buffer
/** Get the PNG width */
get width(): number
/** Get the PNG height */
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ impl RenderedImage {

/// Get the RGBA pixels of the image
#[cfg(target_arch = "wasm32")]
pub fn pixel(&self) -> js_sys::Uint8Array {
pub fn pixels(&self) -> js_sys::Uint8Array {
self.pix.data().into()
}

/// Get the RGBA pixels of the image
#[cfg(not(target_arch = "wasm32"))]
#[napi(getter)]
pub fn pixel(&self) -> Buffer {
pub fn pixels(&self) -> Buffer {
self.pix.data().into()
}

Expand Down

0 comments on commit e3061b4

Please sign in to comment.