Automattic / Automattic/node-canvas

optimize canvas.toBuffer("raw") for BGR24 to improve performance for non-transparent canvas

Open
#2,182 7 comments 3 reactions 0 assignees View on GitHub
Feature
Dominant language
JavaScript
Stars
10.7k
Forks
1.2k
Avg merge
4d 8h
Merged PRs (30d)
1

Description

## Issue or Feature
When creating a canvas and using a 2dContext in the pixelFormat "RGB24" `toBuffer('raw')` still returns "BGRA" on my system instead of "BGR24". It seems like under the hood still the BGRA is used. Changing this here should have a performance improvement. It doesn't make sense to always handle the alpha channel if its not used.

This would be especially useful in the case that node-canvas is used to generate many many images to form a video via ffmpeg. ffmpeg can handle "BGRA" and "RGB24" as well, but the latter uses just a third of the data and should processed faster on canvas and ffmpeg side.

## Steps to Reproduce
```ts
import canvas from "canvas";

const myCanvas = canvas.createCanvas(1920, 1080)
const ctx = myCanvas.getContext('2d', {alpha: false, pixelFormat: "RGB24"});
ctx.rotate(0.1)
ctx.fillText('Awesome!', 0, 0);
const buffer = myCanvas.toBuffer("raw");
console.log(buffer.length);
```

Log output:
8294400

The output means 1920 * 1080 * 4 = 8294400. The resulting buffer still uses 4 bytes (BGRA) per pixel.
The output I would prefer here would be 1920 * 1080 * 3 = 6220800

## Your Environment
* Version of node-canvas 2.9.0
* Environment (node 14.18.1 on Mac OS 12.5.1 Apple Silicon):

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.