Skip to content

Commit

Permalink
feat: add .pixel() API
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Jun 24, 2022
1 parent 6240299 commit ce7bbe0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
42 changes: 42 additions & 0 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,48 @@ import jimp from 'jimp'

import { Resvg, renderAsync } from '../index'

/**
* Convert image to RGBA pixels Array
* Traverse the pixels in the order from left to right and top to bottom.
*
* @param {Buffer} imgBuffer
* @param {Number} width image width
* @param {Number} height image height
* @returns {Array}, e.g. [255, 0, 0, 255, 255, 0, 0, 255]
*/
async function imgToRgbaPixel(imgBuffer, width, height) {
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)
pixels.push(pixel.a)
}
}
return pixels
}

test.only('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>
</svg>`
const resvg = new Resvg(svg)
const pngData = resvg.render()
const pngBuffer = pngData.asPng()

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

t.is(originPixels.length, pixelArray.length)
t.is(originPixels.toString(), pixelArray.toString())
})

test('fit to width', async (t) => {
const filePath = '../example/text.svg'
const svg = await fs.readFile(join(__dirname, filePath))
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export class RenderedImage {
/** Write the image data to Buffer */
asPng(): Buffer

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

/** Get the PNG width */
get width(): number

Expand Down
2 changes: 2 additions & 0 deletions js-binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ export class Resvg {
export class RenderedImage {
/** Write the image data to Buffer */
asPng(): Buffer
/** Get the RGBA pixels of the image */
get pixel(): Buffer
/** Get the PNG width */
get width(): number
/** Get the PNG height */
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ impl RenderedImage {
Ok(buffer.as_slice().into())
}

/// Get the RGBA pixels of the image
#[cfg(target_arch = "wasm32")]
pub fn pixel(&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 {
self.pix.data().into()
}

#[cfg(not(target_arch = "wasm32"))]
#[napi(getter)]
/// Get the PNG width
Expand Down

0 comments on commit ce7bbe0

Please sign in to comment.