Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1528)

Unified Diff: Source/core/platform/image-decoders/png/PNGImageDecoder.cpp

Issue 17448010: Speed up png decoding by eliminating branches from the core loop that fills ImageFrame with pixels (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Speed up png decoding by eliminating branches from the core loop that fills ImageFrame with pixels Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
diff --git a/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp b/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
index 828328b956c2bb0c8d8d8de534228c2abee60223..6b763214837e48e5168610ed3f9c8410896b419f 100644
--- a/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
+++ b/Source/core/platform/image-decoders/png/PNGImageDecoder.cpp
@@ -489,18 +489,31 @@ void PNGImageDecoder::rowAvailable(unsigned char* rowBuffer, unsigned rowIndex,
// Write the decoded row pixels to the frame buffer.
ImageFrame::PixelData* address = buffer.getAddr(0, y);
- bool nonTrivialAlpha = false;
+ unsigned alphaMask = 255;
int width = size().width();
+ // Do not merge the loops below. They are hand rolled for each case for maximal performance.
png_bytep pixel = row;
- for (int x = 0; x < width; ++x, pixel += colorChannels) {
- unsigned alpha = hasAlpha ? pixel[3] : 255;
- buffer.setRGBA(address++, pixel[0], pixel[1], pixel[2], alpha);
- nonTrivialAlpha |= alpha < 255;
+ if (hasAlpha) {
+ if (buffer.premultiplyAlpha()) {
+ for (int x = 0; x < width; ++x, pixel += 4) {
+ buffer.setRGBAPremultiply(address + x, pixel[0], pixel[1], pixel[2], pixel[3]);
Noel Gordon 2013/06/26 05:15:26 address++ not working for you? Why address + x or
+ alphaMask &= pixel[3];
+ }
+ } else {
+ for (int x = 0; x < width; ++x, pixel += 4) {
+ buffer.setRGBARaw(address + x, pixel[0], pixel[1], pixel[2], pixel[3]);
+ alphaMask &= pixel[3];
+ }
+ }
+ } else {
+ for (int x = 0; x < width; ++x, pixel += 3)
+ buffer.setRGB(address + x, pixel[0], pixel[1], pixel[2]);
}
- if (nonTrivialAlpha && !buffer.hasAlpha())
- buffer.setHasAlpha(nonTrivialAlpha);
+ bool hasNonTrivialAlpha = alphaMask != 255;
+ if (!buffer.hasAlpha())
+ buffer.setHasAlpha(hasNonTrivialAlpha);
}
void PNGImageDecoder::pngComplete()

Powered by Google App Engine
This is Rietveld 408576698