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

Unified Diff: src/codec/SkSwizzler.cpp

Issue 947283002: Bmp Image Decoding (Closed) Base URL: https://skia.googlesource.com/skia.git@decode-leon-3
Patch Set: Tested bmp and swizzler design Created 5 years, 10 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
« src/codec/SkSwizzler.h ('K') | « src/codec/SkSwizzler.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/codec/SkSwizzler.cpp
diff --git a/src/codec/SkSwizzler.cpp b/src/codec/SkSwizzler.cpp
index 85f447a843d35d17dd04bab397b80c26a82a3181..3be330f65a6c4033a9865c98b855e452cfbe87d5 100644
--- a/src/codec/SkSwizzler.cpp
+++ b/src/codec/SkSwizzler.cpp
@@ -15,23 +15,37 @@
static bool swizzle_index_to_n32(void* SK_RESTRICT dstRow,
const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int, const SkPMColor ctable[]) {
+ int width, int deltaSrc, int bPP, int,
+ const SkPMColor ctable[], const uint32_t*,
+ const bool, bool*, bool*) {
SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
SkPMColor cc = A32_MASK_IN_PLACE;
- for (int x = 0; x < width; x++) {
- SkPMColor c = ctable[*src];
- cc &= c;
- dst[x] = c;
- src += deltaSrc;
+ const uint32_t pixelsPerByte = 8 / bPP;
+ const uint32_t rowBytes = (width + pixelsPerByte - 1) / pixelsPerByte;
+ const uint8_t mask = (1 << bPP) - 1;
+
+ uint32_t x = 0;
+ for (uint32_t byte = 0; byte < rowBytes; byte++) {
+ uint8_t pixelData = src[byte];
+ for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) {
+ uint8_t index = (pixelData >> (8 - bPP)) & mask;
+ dst[x] = ctable[index];
+ cc &= ctable[index];
+ pixelData <<= bPP;
+ x++;
+ }
}
return cc != A32_MASK_IN_PLACE;
}
+// TODO: this is only valid for kIndex8
static bool swizzle_index_to_n32_skipZ(void* SK_RESTRICT dstRow,
- const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int,
- const SkPMColor ctable[]) {
+ const uint8_t* SK_RESTRICT src,
+ int width, int deltaSrc, int bPP, int,
+ const SkPMColor ctable[],
+ const uint32_t*, const bool, bool*,
+ bool*) {
SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
SkPMColor cc = A32_MASK_IN_PLACE;
@@ -48,10 +62,217 @@ static bool swizzle_index_to_n32_skipZ(void* SK_RESTRICT dstRow,
#undef A32_MASK_IN_PLACE
+// mask
+
+/**
+ *
+ * Used to convert 1-7 bit color components into 8-bit color components
+ *
+ */
+const uint8_t nBitTo8BitlookupTable[] = {
+ // 1 bit
+ 0, 255,
+ // 2 bits
+ 0, 85, 170, 255,
+ // 3 bits
+ 0, 36, 73, 109, 146, 182, 219, 255,
+ // 4 bits
+ 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255,
+ // 5 bits
+ 0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140,
+ 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255,
+ // 6 bits
+ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 45, 49, 53, 57, 61, 65, 69, 73,
+ 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138,
+ 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198,
+ 202, 206, 210, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255,
+ // 7 bits
+ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38,
+ 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76,
+ 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110,
+ 112, 114, 116, 118, 120, 122, 124, 126, 129, 131, 133, 135, 137, 139, 141,
+ 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171,
+ 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201,
+ 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231,
+ 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255
+};
+
+/*
+ *
+ * Convert an n bit component to an 8-bit component
+ *
+ */
+static uint8_t convertNTo8(uint32_t component, uint32_t n) {
+ if (n == 0) {
+ return 0;
+ } else if (n < 8) {
+ return nBitTo8BitlookupTable[(1 << n) - 2 + component];
+ } else if (n == 8) {
+ return component;
+ } else {
+ SkDebugf("Error: too many bits for lookup table.\n");
+ return 0;
+ }
+}
+
+/*
+ *
+ * For a continuous bit mask (ex: 0011100), retrieves the size of the mask and
+ * the trailing zeros
+ *
+ */
+static void getMaskInfo(uint32_t mask, uint32_t bPP, uint32_t* size,
+ uint32_t* shift) {
+ // Trim mask based on pixel size
+ if (bPP < 32) {
+ mask &= (1 << bPP) - 1;
+ }
+
+ // For empty masks, set zeros and return
+ uint32_t tempMask = mask;
+ if (!tempMask) {
+ *size = 0;
+ *shift = 0;
+ return;
+ }
+
+ // Count trailing zeros
+ int zeros = 0;
+ for (; !(tempMask & 1); tempMask >>= 1) {
+ zeros++;
+ }
+
+ // Count mask size
+ int count = 0;
+ for (; tempMask & 1; tempMask >>= 1) {
+ count++;
+ }
+
+ // We will use a maximum of 8 bits for the size, truncate some of the mask
+ // bits if necessary
+ if (count > 8) {
+ *shift = count - 8 + zeros;
+ *size = 8;
+ } else {
+ *shift = zeros;
+ *size = count;
+ }
+ return;
+}
+
+static bool swizzle_mask_to_n32(void* SK_RESTRICT dstRow,
+ const uint8_t* SK_RESTRICT src,
+ int width, int deltaSrc, int bPP, int,
+ const SkPMColor ctable[],
+ const uint32_t* masks, const bool,
+ bool* fSeenNonZeroAlphaPtr,
+ bool* fZeroPrevRowsPtr) {
+ // Load the bit masks
+ uint32_t redMask = masks[0];
+ uint32_t greenMask = masks[1];
+ uint32_t blueMask = masks[2];
+ uint32_t alphaMask = masks[3];
+ uint32_t rBits, rShift, gBits, gShift, bBits, bShift, aBits, aShift;
+ getMaskInfo(redMask, bPP, &rBits, &rShift);
+ getMaskInfo(greenMask, bPP, &gBits, &gShift);
+ getMaskInfo(blueMask, bPP, &bBits, &bShift);
+ getMaskInfo(alphaMask, bPP, &aBits, &aShift);
+
+ // Use the masks to decode to the destination
+ SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
+ int x = 0;
+ for (uint32_t p = 0; p < width * deltaSrc; p += deltaSrc) {
+ uint32_t pixel;
+ switch (deltaSrc) {
scroggo 2015/02/25 17:22:43 these static functions are intended to be specific
msarett 2015/02/26 23:58:18 Yes we can. My concern is an explosion in the num
scroggo 2015/02/27 17:04:00 In general, my preference is to avoid duplicated c
+ case 2:
+ pixel = src[p] | (src[p + 1] << 8);
+ break;
+ case 3:
+ pixel = src[p] | (src[p + 1] << 8) | (src[p + 2] << 16);
+ break;
+ case 4:
+ pixel = src[p] | (src[p + 1] << 8) | (src[p + 2] << 16) |
+ (src[p + 3] << 24);
+ break;
+ default:
+ SkDebugf("Error: invalid number of bytes per pixel.\n");
+ return false;
+ }
+ uint8_t red = convertNTo8((pixel & redMask) >> rShift, rBits);
+ uint8_t green = convertNTo8((pixel & greenMask) >> gShift, gBits);
+ uint8_t blue = convertNTo8((pixel & blueMask) >> bShift, bBits);
+ uint8_t alpha = convertNTo8((pixel & alphaMask) >> aShift, aBits);
+
+ // We must respect the alpha channel for V4 and V5. However, if it is
+ // all zeros, we will display the image as opaque rather than
+ // transparent. This may require redoing some of the processing.
+ if (*fSeenNonZeroAlphaPtr) {
+ dst[x] = SkPackARGB32(alpha, red, blue, green);
+ x++;
+ } else if (!alpha) {
+ dst[x] = SkPackARGB32(0xFF, red, blue, green);
+ x++;
+ } else {
+ *fZeroPrevRowsPtr = true;
+ *fSeenNonZeroAlphaPtr = true;
+ x = 0;
+ p = -1;
+ }
+ }
+ return false;
+}
+
+// bgrx and bgra
+
+static bool swizzle_bgrx_to_n32(void* SK_RESTRICT dstRow,
+ const uint8_t* SK_RESTRICT src,
+ int width, int deltaSrc, int,
+ int, const SkPMColor[], const uint32_t*,
+ const bool, bool*, bool*) {
+ SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
+ for (int x = 0; x < width; x++) {
+ dst[x] = SkPackARGB32(0xFF, src[2], src[1], src[0]);
+ src += deltaSrc;
+ }
+ return false;
+}
+
+static bool swizzle_bgra_to_n32(void* SK_RESTRICT dstRow,
+ const uint8_t* SK_RESTRICT src,
+ int width, int deltaSrc, int, int,
+ const SkPMColor[], const uint32_t* masks,
+ const bool fixAlpha, bool* fSeenNonZeroAlphaPtr,
+ bool* fZeroPrevRowsPtr) {
+ SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
+ uint32_t alphaMask = masks[3];
+ const uint8_t* srcStart = src;
+ for (int x = 0; x < width; x++) {
+ uint8_t alpha = alphaMask & src[3];
+ // We must respect the alpha channel for V4 and V5. However, if it is
+ // all zeros, we will display the image as opaque rather than
+ // transparent. This may require redoing some of the processing.
+ if (*fSeenNonZeroAlphaPtr) {
+ dst[x] = SkPackARGB32(alpha, src[2], src[1], src[0]);
+ src += deltaSrc;
+ } else if (!alpha) {
+ dst[x] = SkPackARGB32(0xFF, src[2], src[1], src[0]);
+ src += deltaSrc;
+ } else {
+ *fZeroPrevRowsPtr = true;
+ *fSeenNonZeroAlphaPtr = true;
+ int x = -1;
+ src = srcStart;
+ }
+ }
+ return false;
scroggo 2015/02/25 17:22:43 Sorry, I thought I had documented this, but the re
+}
+
// n32
static bool swizzle_rgbx_to_n32(void* SK_RESTRICT dstRow,
const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int, const SkPMColor[]) {
+ int width, int deltaSrc, int, int,
+ const SkPMColor[], const uint32_t*,
+ const bool, bool*, bool*) {
SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
for (int x = 0; x < width; x++) {
dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]);
@@ -62,7 +283,9 @@ static bool swizzle_rgbx_to_n32(void* SK_RESTRICT dstRow,
static bool swizzle_rgba_to_n32_premul(void* SK_RESTRICT dstRow,
const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int, const SkPMColor[]) {
+ int width, int deltaSrc, int, int,
+ const SkPMColor[], const uint32_t*,
+ const bool, bool*, bool*) {
SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
unsigned alphaMask = 0xFF;
for (int x = 0; x < width; x++) {
@@ -76,8 +299,9 @@ static bool swizzle_rgba_to_n32_premul(void* SK_RESTRICT dstRow,
static bool swizzle_rgba_to_n32_unpremul(void* SK_RESTRICT dstRow,
const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int,
- const SkPMColor[]) {
+ int width, int deltaSrc, int, int,
+ const SkPMColor[], const uint32_t*,
+ const bool, bool*, bool*) {
uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow);
unsigned alphaMask = 0xFF;
for (int x = 0; x < width; x++) {
@@ -91,8 +315,10 @@ static bool swizzle_rgba_to_n32_unpremul(void* SK_RESTRICT dstRow,
static bool swizzle_rgba_to_n32_premul_skipZ(void* SK_RESTRICT dstRow,
const uint8_t* SK_RESTRICT src,
- int width, int deltaSrc, int,
- const SkPMColor[]) {
+ int width, int deltaSrc, int, int,
+ const SkPMColor[],
+ const uint32_t*, const bool,
+ bool*, bool*) {
SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
unsigned alphaMask = 0xFF;
for (int x = 0; x < width; x++) {
@@ -135,29 +361,60 @@ static bool swizzle_rgba_to_n32_unpremul_skipZ(void* SK_RESTRICT dstRow,
SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, const SkPMColor* ctable,
const SkImageInfo& info, void* dst,
- size_t dstRowBytes, bool skipZeroes) {
+ size_t dstRowBytes, bool skipZeroes,
+ const uint32_t* bitMasks,
+ const bool fixAlpha,
+ const bool inverted) {
if (info.colorType() == kUnknown_SkColorType) {
return NULL;
}
if (info.minRowBytes() > dstRowBytes) {
return NULL;
}
- if (kIndex == sc && NULL == ctable) {
+ if ((kIndex8 == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc)
+ && NULL == ctable) {
return NULL;
}
RowProc proc = NULL;
switch (sc) {
- case kIndex:
+ case kIndex1:
+ case kIndex2:
+ case kIndex4:
+ case kIndex8:
switch (info.colorType()) {
case kN32_SkColorType:
- // We assume the color premultiplied ctable (or not) as desired.
- if (skipZeroes) {
- proc = &swizzle_index_to_n32_skipZ;
- } else {
- proc = &swizzle_index_to_n32;
- }
+ proc = &swizzle_index_to_n32;
+ break;
+ default:
+ break;
+ }
+ break;
+ case kMask16:
+ case kMask24:
+ case kMask32:
+ switch (info.colorType()) {
+ case kN32_SkColorType:
+ proc = &swizzle_mask_to_n32;
+ break;
+ default:
+ break;
+ }
+ break;
+ case kBGR:
+ case kBGRX:
+ switch (info.colorType()) {
+ case kN32_SkColorType:
+ proc = &swizzle_bgrx_to_n32;
+ break;
+ default:
+ break;
+ }
+ break;
+ case kBGRA:
+ switch (info.colorType()) {
+ case kN32_SkColorType:
+ proc = &swizzle_bgra_to_n32;
break;
-
default:
break;
}
@@ -190,33 +447,83 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, const SkPMColor
break;
}
break;
+ case kRGB:
+ switch (info.colorType()) {
+ case kN32_SkColorType:
+ proc = &swizzle_rgbx_to_n32;
+ break;
+ default:
+ break;
+ }
+ break;
default:
break;
}
if (NULL == proc) {
return NULL;
}
- return SkNEW_ARGS(SkSwizzler, (proc, ctable, BytesPerPixel(sc), info, dst, dstRowBytes));
+ return SkNEW_ARGS(SkSwizzler, (proc, ctable, BytesPerPixel(sc),
+ BitsPerPixel(sc), info, dst, dstRowBytes,
+ bitMasks, fixAlpha, inverted));
}
SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, int srcBpp,
- const SkImageInfo& info, void* dst, size_t rowBytes)
+ int srcBitsPerPixel, const SkImageInfo& info, void* dst,
+ size_t rowBytes, const uint32_t* bitMasks,
+ const bool fixAlpha, const bool inverted)
: fRowProc(proc)
, fColorTable(ctable)
, fSrcPixelSize(srcBpp)
+ , fSrcBitsPerPixel(srcBitsPerPixel)
, fDstInfo(info)
, fDstRow(dst)
, fDstRowBytes(rowBytes)
, fCurrY(0)
+ , fBitMasks(bitMasks)
+ , fFixAlpha(fixAlpha)
+ , fInverted(inverted)
+ , fSeenNonZeroAlpha(false)
+ , fZeroPrevRows(false)
{
}
bool SkSwizzler::next(const uint8_t* SK_RESTRICT src) {
SkASSERT(fCurrY < fDstInfo.height());
- const bool hadAlpha = fRowProc(fDstRow, src, fDstInfo.width(), fSrcPixelSize,
- fCurrY, fColorTable);
+
+ // On the first iteration, if the image is inverted, start at the bottom
+ if (fCurrY == 0 && fInverted) {
+ fDstRow = SkTAddOffset<void>(fDstRow,
+ fDstRowBytes * (fDstInfo.height() - 1));
+ }
+
+ // Decode a row
+ const bool hadAlpha = fRowProc(fDstRow, src, fDstInfo.width(),
+ fSrcPixelSize, fSrcBitsPerPixel, fCurrY, fColorTable, fBitMasks,
+ fFixAlpha, &fSeenNonZeroAlpha, &fZeroPrevRows);
msarett 2015/02/24 21:56:06 The row procedure may need to let the caller know
+
+ // This flag indicates that we have decoded the image as opaque instead of
+ // transparent, and we just realized that it should have been transparent.
+ // To fix this, we zero the rows that have already been decoded.
+ if (fZeroPrevRows) {
+ SkDebugf("TESTING FIX ALPHA DECODE\n");
+ void* dstRow;
+ if (!fInverted) {
+ void* dstStart = SkTAddOffset<void>(fDstRow, -fCurrY*fDstRowBytes);
+ memset(dstStart, 0, fCurrY*fDstRowBytes);
+ } else {
+ void* dstStart = SkTAddOffset<void>(fDstRow, fDstRowBytes);
+ memset(dstStart, 0, fCurrY*fDstRowBytes);
+ }
+ fZeroPrevRows = false;
+ }
+
+ // Move to the next row and return the result
fCurrY++;
- fDstRow = SkTAddOffset<void>(fDstRow, fDstRowBytes);
+ if (!fInverted) {
+ fDstRow = SkTAddOffset<void>(fDstRow, fDstRowBytes);
+ } else {
+ fDstRow = SkTAddOffset<void>(fDstRow, -fDstRowBytes);
+ }
return hadAlpha;
}
« src/codec/SkSwizzler.h ('K') | « src/codec/SkSwizzler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698