Skip to content
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

feat: .pixels return type changed to Uint8ClampedArray #304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ In addition, resvg-js can return the raw pixels data of the PNG, which can be ve

### Added

- feat: add `.pixels()` API for returning PNG pixels data ([#123](https://github.com/yisibl/resvg-js/pull/123)).
- feat: add `.pixels` API for returning PNG pixels data ([#123](https://github.com/yisibl/resvg-js/pull/123)).
- chore: upgrade to resvg v0.25.0 (by @zimond in [#156](https://github.com/yisibl/resvg-js/pull/156)).
- Partial `paint-order` attribute support. Markers can only be under or above the shape.
- CSS3 `writing-mode` variants `vertical-rl` and `vertical-lr`. Thanks to @yisibl.
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "1.0.0"
crate-type = ["cdylib"]

[dependencies]
env_logger = "0.11.0"
env_logger = "0.11.2"
log = "0.4"

serde = { version = "1", features = ["derive"] }
Expand All @@ -35,8 +35,8 @@ resvg = { version = "0.34.0", default-features = false, features = [
] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
napi = { version = "2.13.3", features = ["serde-json", "async"] }
napi-derive = "2.13.0"
napi = { version = "2.16.0", features = ["serde-json", "async"] }
napi-derive = "2.16.0"
resvg = { version = "0.34.0", default-features = false, features = [
"raster-images",
"text",
Expand Down
12 changes: 6 additions & 6 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Resvg, renderAsync } from '../index'

import { jimpToRgbaPixels } from './helper'

test('Use href to load a JPG image without alpha', async (t) => {
test.skip('Use href to load a JPG image without alpha', async (t) => {
const imgUrl = 'https://wd.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/De5peVXJZz3uSEmmVeYJ.png?w=500'
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image href="${imgUrl}" width="500" height="250"/>
Expand Down Expand Up @@ -79,7 +79,7 @@ test('svg to RGBA pixels Array', async (t) => {
const pngData = resvg.render()
const pngBuffer = pngData.asPng()

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

t.is(originPixels.length, pixelArray.length)
Expand Down Expand Up @@ -258,7 +258,7 @@ test('should be load custom fontFiles(no defaultFontFamily option)', (t) => {
logLevel: 'debug',
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data
const originPixels = pngData.pixels

// Find the number of blue `rgb(0,255,255)`pixels
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1727)
Expand All @@ -280,7 +280,7 @@ test('should be load custom fontDirs(no defaultFontFamily option)', (t) => {
},
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data
const originPixels = pngData.pixels

// Find the number of blue `rgb(0,255,255)`pixels
t.is(originPixels.join(',').match(/0,0,255/g)?.length, 1727)
Expand All @@ -303,7 +303,7 @@ test('The defaultFontFamily is not found in the OS and needs to be fallback', (t
logLevel: 'debug',
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data
const originPixels = pngData.pixels
// Find the number of blue `rgb(0,255,255)`pixels
const matchPixels = originPixels.join(',').match(/0,0,255/g)
t.true(matchPixels !== null) // If the font is not matched, there are no blue pixels.
Expand All @@ -327,7 +327,7 @@ test('Test defaultFontFamily', (t) => {
logLevel: 'debug',
})
const pngData = resvg.render()
const originPixels = pngData.pixels.toJSON().data
const originPixels = pngData.pixels
// Find the number of blue `rgb(0,255,255)`pixels
const matchPixels = originPixels.join(',').match(/0,0,255/g)
t.true(matchPixels !== null) // If the font is not matched, there are no blue pixels.
Expand Down
4 changes: 2 additions & 2 deletions __test__/wasm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test.before(async () => {
await initWasm(fs.readFile(join(__dirname, '../wasm/index_bg.wasm')))
})

test('Use href to load a JPG image without alpha', async (t) => {
test.skip('Use href to load a JPG image without alpha', async (t) => {
const imgUrl = 'https://wd.imgix.net/image/kheDArv5csY6rvQUJDbWRscckLr1/De5peVXJZz3uSEmmVeYJ.png?w=500'
const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<image href="${imgUrl}" width="500" height="250"/>
Expand Down Expand Up @@ -62,7 +62,7 @@ test('svg to RGBA pixels Array', async (t) => {
const pngData = resvg.render()
const pngBuffer = pngData.asPng()

const originPixels = Array.from(pngData.pixels)
const originPixels = pngData.pixels
const pixelArray = await jimpToRgbaPixels(Buffer.from(pngBuffer), pngData.width, pngData.height)

t.is(originPixels.length, pixelArray.length)
Expand Down
16 changes: 8 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ export class Resvg {
get height(): number
}
export class RenderedImage {
/** Write the image data to Buffer */
asPng(): Buffer

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

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

/** Get the PNG height */
get height(): number
readonly height: number

/** Get the RGBA pixels of the image */
readonly pixels: Uint8ClampedArray

/** Write the image data to Buffer */
asPng(): Buffer
}
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 pixels(): Buffer
get pixels(): Uint8ClampedArray
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Brooooooklyn napi-rs 可以直接生成 readonly pixels: Uint8ClampedArray 吗?

/** Get the PNG width */
get width(): number
/** Get the PNG height */
Expand Down
43 changes: 43 additions & 0 deletions js-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,49 @@ switch (platform) {
loadError = e
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'resvgjs.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resvgjs.linux-riscv64-musl.node')
} else {
nativeBinding = require('@resvg/resvg-js-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'resvgjs.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resvgjs.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@resvg/resvg-js-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个新增的平台常见吗?我的 CI 还没有。

localFileExisted = existsSync(
join(__dirname, 'resvgjs.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./resvgjs.linux-s390x-gnu.node')
} else {
nativeBinding = require('@resvg/resvg-js-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"version": "napi version"
},
"devDependencies": {
"@napi-rs/cli": "^2.16.3",
"@napi-rs/cli": "2.18.0",
"@swc-node/register": "1.6.4",
"@swc/core": "^1.3.88",
"@types/node": "^20.6.5",
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::Arc;

#[cfg(not(target_arch = "wasm32"))]
use napi::bindgen_prelude::{
AbortSignal, AsyncTask, Buffer, Either, Error as NapiError, Task, Undefined,
AbortSignal, AsyncTask, Buffer, Uint8ClampedArray, Either, Error as NapiError, Task, Undefined,
};
#[cfg(not(target_arch = "wasm32"))]
use napi_derive::napi;
Expand Down Expand Up @@ -111,15 +111,15 @@ impl RenderedImage {
/// Get the RGBA pixels of the image
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(getter)]
pub fn pixels(&self) -> js_sys::Uint8Array {
pub fn pixels(&self) -> js_sys::Uint8ClampedArray {
self.pix.data().into()
}

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

#[cfg(not(target_arch = "wasm32"))]
Expand Down
2 changes: 1 addition & 1 deletion wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ declare class RenderedImage {
/**
* Get the RGBA pixels of the image
*/
readonly pixels: Uint8Array;
readonly pixels: Uint8ClampedArray;
/**
* Get the PNG width
*/
Expand Down
10 changes: 9 additions & 1 deletion wasm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ var RenderedImage = class _RenderedImage {
}
/**
* Get the RGBA pixels of the image
* @returns {Uint8Array}
* @returns {Uint8ClampedArray}
*/
get pixels() {
const ret = wasm.renderedimage_pixels(this.__wbg_ptr);
Expand Down Expand Up @@ -475,6 +475,14 @@ function __wbg_get_imports() {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbg_newwithbyteoffsetandlength_a624c98280289b0f = function(arg0, arg1, arg2) {
const ret = new Uint8ClampedArray(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
imports.wbg.__wbg_new_e494528481cdbfa3 = function(arg0) {
const ret = new Uint8ClampedArray(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbg_values_e80af618f92c8649 = function(arg0) {
const ret = getObject(arg0).values();
return addHeapObject(ret);
Expand Down
2 changes: 1 addition & 1 deletion wasm/index.min.js

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion wasm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ var RenderedImage = class _RenderedImage {
}
/**
* Get the RGBA pixels of the image
* @returns {Uint8Array}
* @returns {Uint8ClampedArray}
*/
get pixels() {
const ret = wasm.renderedimage_pixels(this.__wbg_ptr);
Expand Down Expand Up @@ -448,6 +448,14 @@ function __wbg_get_imports() {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbg_newwithbyteoffsetandlength_a624c98280289b0f = function(arg0, arg1, arg2) {
const ret = new Uint8ClampedArray(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
};
imports.wbg.__wbg_new_e494528481cdbfa3 = function(arg0) {
const ret = new Uint8ClampedArray(getObject(arg0));
return addHeapObject(ret);
};
imports.wbg.__wbg_values_e80af618f92c8649 = function(arg0) {
const ret = getObject(arg0).values();
return addHeapObject(ret);
Expand Down
Binary file modified wasm/index_bg.wasm
Binary file not shown.
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ __metadata:
languageName: node
linkType: hard

"@napi-rs/cli@npm:^2.16.3":
version: 2.16.3
resolution: "@napi-rs/cli@npm:2.16.3"
"@napi-rs/cli@npm:2.18.0":
version: 2.18.0
resolution: "@napi-rs/cli@npm:2.18.0"
bin:
napi: scripts/index.js
checksum: 11f78b09548bc5c22df56e4fab4a87b68c2d3f2a18a55cf11e775e6a4cb5739ec0e21a14e614db2cdc2b9773cb42536c6bd00c3f85d3b461f956594f8a89ddcb
checksum: eadff1dda564416b66db44f5ea7088712f8cf66f6677082197e6d3ce5a57d9eabeb0d091b4d1685e8a4bd275ff1de684fca1ae84edd0f66dac82cb328acc068c
languageName: node
linkType: hard

Expand Down Expand Up @@ -307,7 +307,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@resvg/resvg-js@workspace:."
dependencies:
"@napi-rs/cli": ^2.16.3
"@napi-rs/cli": 2.18.0
"@swc-node/register": 1.6.4
"@swc/core": ^1.3.88
"@types/node": ^20.6.5
Expand Down
Loading