| 21 | } |
| 22 | |
| 23 | bool Decoder::readHeader(Header& header) |
| 24 | { |
| 25 | header.idLength = read8(); |
| 26 | header.colormapType = read8(); |
| 27 | header.imageType = read8(); |
| 28 | header.colormapOrigin = read16(); |
| 29 | header.colormapLength = read16(); |
| 30 | header.colormapDepth = read8(); |
| 31 | header.xOrigin = read16(); |
| 32 | header.yOrigin = read16(); |
| 33 | header.width = read16(); |
| 34 | header.height = read16(); |
| 35 | header.bitsPerPixel = read8(); |
| 36 | header.imageDescriptor = read8(); |
| 37 | |
| 38 | // Invalid image size |
| 39 | if (header.width == 0 || |
| 40 | header.height == 0) |
| 41 | return false; |
| 42 | |
| 43 | // Skip ID string (idLength bytes) |
| 44 | if (header.idLength > 0) { |
| 45 | uint8_t i = header.idLength; |
| 46 | while (i--) { |
| 47 | uint8_t chr = m_file->read8(); |
| 48 | header.imageId.push_back(chr); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | #if 0 |
| 53 | // In the best case the "alphaBits" should be valid, but there are |
| 54 | // invalid TGA files out there which don't indicate the |
| 55 | // "alphaBits" correctly, so they could be 0 and use the alpha |
| 56 | // channel anyway on each pixel. |
| 57 | int alphaBits = (header.imageDescriptor & 15); |
| 58 | m_hasAlpha = |
| 59 | (header.bitsPerPixel == 32 && alphaBits == 8) || |
| 60 | (header.bitsPerPixel == 16 && alphaBits == 1); |
| 61 | #else |
| 62 | // So to detect if a 32bpp or 16bpp TGA image has alpha, we'll use |
| 63 | // the "alpha histogram" in postProcessImage() to check if there are |
| 64 | // different alpha values. If there is only one alpha value (all 0 |
| 65 | // or all 255), we create an opaque image anyway. The only exception |
| 66 | // to this rule is when all pixels are black and transparent |
| 67 | // (RGBA=0), that is the only case when an image is fully |
| 68 | // transparent. |
| 69 | // |
| 70 | // Note: This same heuristic is used in apps like macOS Preview: |
| 71 | // https://twitter.com/davidcapello/status/1242803110868893697 |
| 72 | m_hasAlpha = |
| 73 | (header.bitsPerPixel == 32) || |
| 74 | (header.bitsPerPixel == 16); |
| 75 | #endif |
| 76 | |
| 77 | // Read colormap |
| 78 | if (header.colormapType == 1) |
| 79 | readColormap(header); |
| 80 |
nothing calls this directly
no test coverage detected