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

Side by Side Diff: src/images/SkImageDecoder_pkm.cpp

Issue 292663011: Simple PKM image decoder. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 /*
robertphillips 2014/05/22 14:41:35 Please use new header (not Android one).
krajcevski 2014/05/22 15:32:17 Done.
2 * Copyright 2006 The Android Open Source Project
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 "SkColorPriv.h"
9 #include "SkImageDecoder.h"
10 #include "SkStream.h"
11 #include "SkStreamHelpers.h"
12 #include "SkTypes.h"
13
14 #include "etc1.h"
15
16 class SkPKMImageDecoder : public SkImageDecoder {
17 public:
18 SkPKMImageDecoder();
19
20 virtual Format getFormat() const SK_OVERRIDE {
21 return kPKM_Format;
22 }
23
24 protected:
25 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE;
26
27 private:
28 typedef SkImageDecoder INHERITED;
29 };
30
31 //////////////////////////////////////////////////////////////////////////////// /////////
32
robertphillips 2014/05/22 14:41:35 Put this in the class?
krajcevski 2014/05/22 15:32:17 Done.
33 SkPKMImageDecoder::SkPKMImageDecoder()
34 {
35 }
36
37 bool SkPKMImageDecoder::onDecode(SkStream* stream, SkBitmap* bm, Mode mode)
38 {
39 SkAutoMalloc autoMal;
40 const size_t length = CopyStreamToStorage(&autoMal, stream);
41 if (0 == length) {
42 return false;
43 }
44
45 unsigned char* buf = (unsigned char*)autoMal.get();
46
47 // Make sure original PKM header is there...
48 SkASSERT(etc1_pkm_is_valid(buf));
49
50 const unsigned short width = etc1_pkm_get_width(buf);
51 const unsigned short height = etc1_pkm_get_height(buf);
52
53 // should we allow the Chooser (if present) to pick a config for us???
54 if (!this->chooseFromOneChoice(SkBitmap::kARGB_8888_Config,
55 width, height)
robertphillips 2014/05/22 14:41:35 Move the ") {" to the prior line? Can it all fit o
krajcevski 2014/05/22 15:32:17 Done.
56 ) {
57 return false;
58 }
59
robertphillips 2014/05/22 14:41:35 one 100 col line?
krajcevski 2014/05/22 15:32:17 Done.
60 bm->setConfig(SkBitmap::kARGB_8888_Config, width, height, 0,
61 kOpaque_SkAlphaType);
62 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
63 return true;
64 }
65
66 if (!this->allocPixelRef(bm, NULL)) {
67 return false;
68 }
69
70 // Lock the pixels, since we're about to write to them...
71 SkAutoLockPixels alp(*bm);
72
73 // Advance buffer past the header
74 buf += ETC_PKM_HEADER_SIZE;
75
76 // ETC1 Data is encoded as RGB pixels, so we should extract it as such
77 int nPixels = width * height;
78 SkAutoMalloc outRGBData(nPixels * 3);
79 etc1_byte *outRGBDataPtr = reinterpret_cast<etc1_byte *>(outRGBData.get());
80
81 // Decode ETC1
robertphillips 2014/05/22 14:41:35 space after if?
krajcevski 2014/05/22 15:32:17 Done.
82 if(etc1_decode_image(buf, outRGBDataPtr, width, height, 3, width*3)) {
83 return false;
84 }
85
86 // Set each of the pixels...
87 const uint8_t *src = reinterpret_cast<uint8_t *>(outRGBDataPtr);
88 uint8_t *dst = reinterpret_cast<uint8_t *>(bm->getPixels());
robertphillips 2014/05/22 14:41:35 ++i ?
krajcevski 2014/05/22 15:32:17 Done.
89 for(int i = 0; i < width*height; i++) {
90 *dst++ = src[2]; // B
91 *dst++ = src[1]; // G
92 *dst++ = src[0]; // R
93 *dst++ = 0xFF; // Opaque alpha...
94 src += 3;
95 }
96
97 return true;
robertphillips 2014/05/22 14:41:35 rm "//onDecode" ? Not really standard.
krajcevski 2014/05/22 15:32:17 Done.
98 } //onDecode
99
robertphillips 2014/05/22 14:41:35 Why are the "///*" lines different length. I think
krajcevski 2014/05/22 15:32:17 Done.
100 ///////////////////////////////////////////////////////////////////////////////
101 DEFINE_DECODER_CREATOR(PKMImageDecoder);
102 //////////////////////////////////////////////////////////////////////////////// /////////
103
104 static bool is_pkm(SkStreamRewindable* stream) {
robertphillips 2014/05/22 14:41:35 are ... ?
krajcevski 2014/05/22 15:32:17 Done.
105 // Check to see if the first four bytes are
robertphillips 2014/05/22 14:41:35 Do we really need an AutoMalloc for 6 bytes? Can w
krajcevski 2014/05/22 15:32:17 Done.
106 SkAutoMalloc autoMal(6);
107 unsigned char* buf = (unsigned char*)autoMal.get();
robertphillips 2014/05/22 14:41:35 space after if ?
krajcevski 2014/05/22 15:32:17 Done.
108 if(stream->read((void*)buf, ETC_PKM_HEADER_SIZE) != ETC_PKM_HEADER_SIZE) {
109 return false;
110 }
111
robertphillips 2014/05/22 14:41:35 return etc1_pkm_is_valid(buf); ?
krajcevski 2014/05/22 15:32:17 Done.
112 if (etc1_pkm_is_valid(buf)) {
113 return true;
114 }
115
116 return false;
117 }
118
119 static SkImageDecoder* sk_libpkm_dfactory(SkStreamRewindable* stream) {
120 if (is_pkm(stream)) {
121 return SkNEW(SkPKMImageDecoder);
122 }
123 return NULL;
124 }
125
126 static SkImageDecoder_DecodeReg gReg(sk_libpkm_dfactory);
127
128 static SkImageDecoder::Format get_format_pkm(SkStreamRewindable* stream) {
129 if (is_pkm(stream)) {
130 return SkImageDecoder::kPKM_Format;
131 }
132 return SkImageDecoder::kUnknown_Format;
133 }
134
135 static SkImageDecoder_FormatReg gFormatReg(get_format_pkm);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698