-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Optimize _clipImageData #4200
Optimize _clipImageData #4200
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -770,20 +770,24 @@ export class TextureAtlas implements ITextureAtlas { | |
} | ||
|
||
private _clipImageData(imageData: ImageData, boundingBox: IBoundingBox): ImageData { | ||
// Operate on pixels instead of channels to reduce the amount of work | ||
const originalData = new Uint32Array(imageData.data.buffer); | ||
|
||
// Create a new view on the same buffer for the clipped data. The clipping operation is done in | ||
// place to avoid allocating another buffer | ||
const width = boundingBox.right - boundingBox.left + 1; | ||
const height = boundingBox.bottom - boundingBox.top + 1; | ||
const clippedData = new Uint8ClampedArray(width * height * 4); | ||
for (let y = boundingBox.top; y <= boundingBox.bottom; y++) { | ||
for (let x = boundingBox.left; x <= boundingBox.right; x++) { | ||
const oldOffset = y * this._tmpCanvas.width * 4 + x * 4; | ||
const newOffset = (y - boundingBox.top) * width * 4 + (x - boundingBox.left) * 4; | ||
clippedData[newOffset] = imageData.data[oldOffset]; | ||
clippedData[newOffset + 1] = imageData.data[oldOffset + 1]; | ||
clippedData[newOffset + 2] = imageData.data[oldOffset + 2]; | ||
clippedData[newOffset + 3] = imageData.data[oldOffset + 3]; | ||
const clippedData = new Uint32Array(imageData.data.buffer, 0, width * height); | ||
|
||
// Perform clipping and return the result | ||
let x = 0; | ||
let y = 0; | ||
for (y = boundingBox.top; y <= boundingBox.bottom; y++) { | ||
for (x = boundingBox.left; x <= boundingBox.right; x++) { | ||
clippedData[(y - boundingBox.top) * width + (x - boundingBox.left)] = originalData[y * imageData.width + x]; | ||
} | ||
} | ||
return new ImageData(clippedData, width, height); | ||
return new ImageData(new Uint8ClampedArray(clippedData.buffer, clippedData.byteOffset, clippedData.byteLength), width, height); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if it is worth the trouble - since you already do inplace overwriting above, the new ImageData maybe can also be avoided? Which raises the question - is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right! We can remove this outright and just use boundingBox in drawImage. I'm guessing the reason it's as it is currently is because I just overlooked that or wasn't thinking drawImage had a this signature source+dest signature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was just doing this, we can't actually use drawImage as it doesn't accept ImageData, looks like we can still use putImageData though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 #4201 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, was mixing the methods up (I use putImageData myself, lol) 😊 Though the question remains, if thats actually faster, or just creates runtime noise at different ends... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely it's faster, it's essentially doing what was being done manually before natively |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh well - inplace overwriting is abit dangerous, if the overlapping from rewrites is not safe (guess its ok here?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's safe as the data is read from low to high and the writing is always the same as writing (rare) or lower