Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright 2015 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 #include "SkCodec.h" | |
| 9 #include "SkStream.h" | |
| 10 #include "SkCodec_wbmp.h" | |
| 11 | |
| 12 static bool read_byte(SkStream* stream, uint8_t* data) { | |
| 13 return stream->read(data, 1) == 1; | |
|
msarett
2015/03/26 12:37:35
I was going to suggest using get_byte, get_short,
hal.canary
2015/03/26 17:10:53
Acknowledged.
| |
| 14 } | |
| 15 | |
| 16 // http://en.wikipedia.org/wiki/Variable-length_quantity | |
| 17 static bool read_mbf(SkStream* stream, unsigned* value) { | |
| 18 unsigned n = 0; | |
| 19 uint8_t data; | |
| 20 do { | |
| 21 if (!read_byte(stream, &data)) { | |
| 22 return false; | |
| 23 } | |
| 24 n = (n << 7) | (data & 0x7F); | |
| 25 } while (data & 0x80); | |
| 26 *value = n; | |
| 27 return true; | |
| 28 } | |
| 29 | |
| 30 static bool read_header(SkStream* stream, SkISize* size) { | |
| 31 unsigned width, height; | |
| 32 uint8_t data; | |
| 33 if (!read_byte(stream, &data) || data != 0) { | |
| 34 return false; | |
| 35 } | |
| 36 if (!read_byte(stream, &data) || (data & 0x9F)) { // skip fixed header | |
| 37 return false; | |
| 38 } | |
| 39 if (!read_mbf(stream, &width) || width > 0xFFFF || !width) { | |
| 40 return false; | |
| 41 } | |
| 42 if (!read_mbf(stream, &height) || width > 0xFFFF || !height) { | |
| 43 return false; | |
| 44 } | |
| 45 if (size) { | |
| 46 *size = SkISize::Make(SkToS32(width), SkToS32(height)); | |
| 47 } | |
| 48 return true; | |
| 49 } | |
| 50 | |
| 51 static void expand_bits_to_bytes(uint8_t dst[], const uint8_t src[], int bits) { | |
|
scroggo
2015/03/26 14:37:57
Matt, doesn't some of your code do something like
msarett
2015/03/26 15:03:56
Yes thanks for the reminder. The swizzler support
hal.canary
2015/03/26 17:10:53
I put a TODO in here.
| |
| 52 int bytes = bits >> 3; | |
| 53 | |
| 54 for (int i = 0; i < bytes; i++) { | |
| 55 unsigned mask = *src++; | |
| 56 dst[0] = (mask >> 7) & 1; | |
| 57 dst[1] = (mask >> 6) & 1; | |
| 58 dst[2] = (mask >> 5) & 1; | |
| 59 dst[3] = (mask >> 4) & 1; | |
| 60 dst[4] = (mask >> 3) & 1; | |
| 61 dst[5] = (mask >> 2) & 1; | |
| 62 dst[6] = (mask >> 1) & 1; | |
| 63 dst[7] = (mask >> 0) & 1; | |
| 64 dst += 8; | |
| 65 } | |
| 66 | |
| 67 bits &= 7; | |
| 68 if (bits > 0) { | |
| 69 unsigned mask = *src; | |
| 70 do { | |
| 71 *dst++ = (mask >> 7) & 1; | |
| 72 mask <<= 1; | |
| 73 } while (--bits != 0); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 namespace { | |
| 78 class WbmpCodec : public SkCodec { | |
|
msarett
2015/03/26 12:37:35
nit: We have been declaring the class in the heade
scroggo
2015/03/26 14:37:57
+1 for consistency.
hal.canary
2015/03/26 17:10:53
Done.
| |
| 79 public: | |
| 80 WbmpCodec(const SkImageInfo& info, SkStream* stream) | |
| 81 : SkCodec(info, stream) {} | |
| 82 ~WbmpCodec() {} | |
|
msarett
2015/03/26 12:37:35
nit: Probably just a matter of preference, but we
hal.canary
2015/03/26 17:10:53
Done.
| |
| 83 SkEncodedFormat onGetEncodedFormat() const SK_OVERRIDE { | |
| 84 return kWBMP_SkEncodedFormat; | |
| 85 } | |
| 86 SkImageGenerator::Result onGetPixels( | |
| 87 const SkImageInfo&, void*, size_t, const Options&, SkPMColor[], int*); | |
|
msarett
2015/03/26 12:37:35
nit: Should these arguments be indented twice and
scroggo
2015/03/26 14:37:57
Yes.
hal.canary
2015/03/26 17:10:53
Done.
| |
| 88 }; | |
| 89 } // namespace | |
| 90 | |
| 91 SkImageGenerator::Result WbmpCodec::onGetPixels(const SkImageInfo& info, | |
| 92 void* pixels, | |
| 93 size_t rowBytes, | |
| 94 const Options&, | |
| 95 SkPMColor ctable[], | |
| 96 int* ctableCount) { | |
| 97 if (info.dimensions() != this->getInfo().dimensions()) { | |
| 98 return SkImageGenerator::kInvalidScale; | |
| 99 } else if (info != this->getInfo()) { | |
| 100 return SkImageGenerator::kInvalidConversion; | |
|
msarett
2015/03/26 12:37:35
We should also allow conversions to kN32_SkColorTy
scroggo
2015/03/26 14:37:57
That's correct. Android actually will copy an Inde
hal.canary
2015/03/26 17:10:53
Done.
| |
| 101 } | |
| 102 ctable[0] = SK_ColorBLACK; | |
| 103 ctable[1] = SK_ColorWHITE; | |
| 104 *ctableCount = 2; | |
| 105 SkISize size = info.dimensions(); | |
| 106 uint8_t* dst = static_cast<uint8_t*>(pixels); | |
| 107 size_t srcRowBytes = SkAlign8(size.width()) >> 3; | |
| 108 SkAutoTMalloc<uint8_t> src(srcRowBytes); | |
| 109 for (int y = 0; y < size.height(); ++y) { | |
| 110 if (this->stream()->read(src.get(), srcRowBytes) != srcRowBytes) { | |
| 111 return SkImageGenerator::kIncompleteInput; | |
| 112 } | |
| 113 expand_bits_to_bytes(dst, src.get(), size.width()); | |
|
msarett
2015/03/26 12:37:35
If you create a swizzler outside of the loop, afte
| |
| 114 dst += rowBytes; | |
| 115 } | |
| 116 return SkImageGenerator::kSuccess; | |
| 117 } | |
| 118 | |
| 119 bool SkWbmpCodec::IsWbmp(SkStream* stream) { return read_header(stream, NULL); } | |
| 120 | |
| 121 SkCodec* SkWbmpCodec::NewFromStream(SkStream* stream) { | |
| 122 SkISize size; | |
| 123 if (!read_header(stream, &size)) { | |
| 124 return NULL; | |
| 125 } | |
| 126 SkImageInfo info = | |
| 127 SkImageInfo::Make(size.width(), size.height(), kIndex_8_SkColorType, | |
| 128 kOpaque_SkAlphaType); | |
| 129 return SkNEW_ARGS(WbmpCodec, (info, stream)); | |
| 130 } | |
| OLD | NEW |