Hi! First of all, sorry if I’m making a stupid question, but I’ve trying to solve this for a while and I’m still trying to figure this out.
What I’m trying to do is to create a new pixel layer and then change in it the pixels of the selected area of the RGB image to red, using putPixels
.
The problem is that I can’t seem to make it work with a custom image buffer:
async function updateSelection()
{
try
{
await window.require('photoshop').core.executeAsModal(
async () =>
{
const selection = await imaging.getSelection({});
if (selection.imageData !== undefined)
{
let doc = app.activeDocument;
let newLayer = await doc.createPixelLayer({
"name": "Dummy pixel layer",
"opacity": 100
});
await createPixelData(selection, newLayer.id);
}
}
);
}
catch (e)
{
console.error("ERROR in updateSelection: " + e);
}
}
async function createPixelData(selection, layerId)
{
const pixelResult = await imaging.getPixels(
{
"sourceBounds": selection.sourceBounds,
"colorSpace": "RGB",
"targetSize": { "height": selection.sourceBounds.height, "width": selection.sourceBounds.width }
}
);
const imageData = pixelResult.imageData;
const components = imageData.components;
const componentSize = imageData.componentSize;
// const buffer = await imageData.getData(); // THIS WORKS
const buffer = new Uint8Array(dataSize); // THIS DOESN'T
let newValue = 0;
if (componentSize == 8)
{
newValue = 255;
}
else { throw "Only interested in components of size 8." }
const width = imageData.width;
const height = imageData.height;
const pixelCount = width * height;
const dataSize = pixelCount * components;
for (let index = 0; index < dataSize; index += components)
{
buffer[index] = newValue;
buffer[index + 1] = 0;
buffer[index + 2] = 0;
buffer[index + 3] = newValue;
}
let options = {
"width": width,
"height": height,
"components": components,
"chunky": true,
"colorProfile": imageData.colorProfile,
"colorSpace": "RGB"
};
let image = await imaging.createImageDataFromBuffer(buffer, options);
await imaging.putPixels({
"layerID": layerId,
"imageData": image,
"replace": false,
"targetBounds": { "left": selection.sourceBounds.left, "top": selection.sourceBounds.top }
});
image.dispose();
}
The code creates a new layer but the selection remains unchanged:
However, if in createPixelData
I declare the buffer as const buffer = await imageData.getData();
instead of const buffer = new Uint8Array(dataSize);
, it works: