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

Side by Side Diff: samplecode/SampleEncode.cpp

Issue 132513003: SampleApp Cleanup (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: final rebase Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « samplecode/SampleDitherBitmap.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #include "SampleCode.h" 8 #include "SampleCode.h"
9 #include "SkView.h" 9 #include "SkView.h"
10 #include "SkCanvas.h" 10 #include "SkCanvas.h"
11 #include "SkData.h"
12 #include "SkDecodingImageGenerator.h"
11 #include "SkGradientShader.h" 13 #include "SkGradientShader.h"
12 #include "SkGraphics.h" 14 #include "SkGraphics.h"
13 #include "SkImageDecoder.h" 15 #include "SkImageDecoder.h"
14 #include "SkImageEncoder.h" 16 #include "SkImageEncoder.h"
15 #include "SkPath.h" 17 #include "SkPath.h"
16 #include "SkRegion.h" 18 #include "SkRegion.h"
17 #include "SkShader.h" 19 #include "SkShader.h"
18 #include "SkUtils.h" 20 #include "SkUtils.h"
19 #include "SkXfermode.h" 21 #include "SkXfermode.h"
20 #include "SkColorPriv.h" 22 #include "SkColorPriv.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 static const SkImageEncoder::Type gTypes[] = { 98 static const SkImageEncoder::Type gTypes[] = {
97 SkImageEncoder::kJPEG_Type, 99 SkImageEncoder::kJPEG_Type,
98 SkImageEncoder::kPNG_Type 100 SkImageEncoder::kPNG_Type
99 }; 101 };
100 102
101 // must match up with gTypes[] 103 // must match up with gTypes[]
102 static const char* const gExt[] = { 104 static const char* const gExt[] = {
103 ".jpg", ".png" 105 ".jpg", ".png"
104 }; 106 };
105 107
106 static const char* gPath = "/encoded/";
107
108 static void make_name(SkString* name, int config, int ext) {
109 name->set(gPath);
110 name->append(gConfigLabels[config]);
111 name->append(gExt[ext]);
112 }
113
114 #include <sys/stat.h> 108 #include <sys/stat.h>
115 109
116 class EncodeView : public SampleView { 110 class EncodeView : public SampleView {
117 public: 111 public:
118 SkBitmap* fBitmaps; 112 SkBitmap* fBitmaps;
119 size_t fBitmapCount; 113 SkAutoDataUnref* fEncodedPNGs;
114 SkAutoDataUnref* fEncodedJPEGs;
115 size_t fBitmapCount;
120 116
121 EncodeView() { 117 EncodeView() {
122 #if 1 118 #if 1
123 (void)mkdir(gPath, S_IRWXU | S_IRWXG | S_IRWXO);
124
125 fBitmapCount = SK_ARRAY_COUNT(gConfigs); 119 fBitmapCount = SK_ARRAY_COUNT(gConfigs);
126 fBitmaps = new SkBitmap[fBitmapCount]; 120 fBitmaps = new SkBitmap[fBitmapCount];
121 fEncodedPNGs = new SkAutoDataUnref[fBitmapCount];
122 fEncodedJPEGs = new SkAutoDataUnref[fBitmapCount];
127 for (size_t i = 0; i < fBitmapCount; i++) { 123 for (size_t i = 0; i < fBitmapCount; i++) {
128 make_image(&fBitmaps[i], gConfigs[i], i); 124 make_image(&fBitmaps[i], gConfigs[i], i);
129 125
130 for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) { 126 for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) {
131 SkString path; 127 SkAutoTDelete<SkImageEncoder> codec(
132 make_name(&path, i, j); 128 SkImageEncoder::Create(gTypes[j]));
133 129 if (NULL == codec.get()) {
134 // remove any previous run of this file 130 SkDebugf("[%s:%d] failed to encode %s%s\n",
135 remove(path.c_str()); 131 __FILE__, __LINE__,gConfigLabels[i], gExt[j]);
136 132 continue;
137 SkImageEncoder* codec = SkImageEncoder::Create(gTypes[j]);
138 if (NULL == codec ||
139 !codec->encodeFile(path.c_str(), fBitmaps[i], 100)) {
140 SkDebugf("------ failed to encode %s\n", path.c_str());
141 remove(path.c_str()); // remove any partial file
142 } 133 }
143 delete codec; 134 SkAutoDataUnref data(codec->encodeData(fBitmaps[i], 100));
135 if (NULL == data.get()) {
136 SkDebugf("[%s:%d] failed to encode %s%s\n",
137 __FILE__, __LINE__,gConfigLabels[i], gExt[j]);
138 continue;
139 }
140 if (SkImageEncoder::kJPEG_Type == gTypes[j]) {
141 fEncodedJPEGs[i].reset(data.detach());
142 } else if (SkImageEncoder::kPNG_Type == gTypes[j]) {
143 fEncodedPNGs[i].reset(data.detach());
144 }
144 } 145 }
145 } 146 }
146 #else 147 #else
147 fBitmaps = NULL; 148 fBitmaps = NULL;
148 fBitmapCount = 0; 149 fBitmapCount = 0;
149 #endif 150 #endif
150 this->setBGColor(0xFFDDDDDD); 151 this->setBGColor(0xFFDDDDDD);
151 } 152 }
152 153
153 virtual ~EncodeView() { 154 virtual ~EncodeView() {
154 delete[] fBitmaps; 155 delete[] fBitmaps;
156 delete[] fEncodedPNGs;
157 delete[] fEncodedJPEGs;
155 } 158 }
156 159
157 protected: 160 protected:
158 // overrides from SkEventSink 161 // overrides from SkEventSink
159 virtual bool onQuery(SkEvent* evt) { 162 virtual bool onQuery(SkEvent* evt) {
160 if (SampleCode::TitleQ(*evt)) { 163 if (SampleCode::TitleQ(*evt)) {
161 SampleCode::TitleR(evt, "ImageEncoder"); 164 SampleCode::TitleR(evt, "ImageEncoder");
162 return true; 165 return true;
163 } 166 }
164 return this->INHERITED::onQuery(evt); 167 return this->INHERITED::onQuery(evt);
(...skipping 15 matching lines...) Expand all
180 183
181 for (size_t i = 0; i < fBitmapCount; i++) { 184 for (size_t i = 0; i < fBitmapCount; i++) {
182 canvas->drawText(gConfigLabels[i], strlen(gConfigLabels[i]), 185 canvas->drawText(gConfigLabels[i], strlen(gConfigLabels[i]),
183 x + SkIntToScalar(fBitmaps[i].width()) / 2, 0, 186 x + SkIntToScalar(fBitmaps[i].width()) / 2, 0,
184 paint); 187 paint);
185 y = paint.getTextSize(); 188 y = paint.getTextSize();
186 189
187 canvas->drawBitmap(fBitmaps[i], x, y); 190 canvas->drawBitmap(fBitmaps[i], x, y);
188 191
189 SkScalar yy = y; 192 SkScalar yy = y;
190 for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) { 193 for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) {
191 yy += SkIntToScalar(fBitmaps[i].height() + 10); 194 yy += SkIntToScalar(fBitmaps[i].height() + 10);
192 195
193 SkBitmap bm; 196 SkBitmap bm;
194 SkString name; 197 SkData* encoded = NULL;
195 198 if (SkImageEncoder::kJPEG_Type == gTypes[j]) {
196 make_name(&name, i, j); 199 encoded = fEncodedJPEGs[i].get();
197 200 } else if (SkImageEncoder::kPNG_Type == gTypes[j]) {
198 SkImageDecoder::DecodeFile(name.c_str(), &bm); 201 encoded = fEncodedPNGs[i].get();
199 canvas->drawBitmap(bm, x, yy); 202 }
203 if (encoded) {
204 if (!SkInstallDiscardablePixelRef(
205 SkDecodingImageGenerator::Create(encoded,
206 SkDecodingImageGenerator::Options()),
207 &bm, NULL)) {
208 SkDebugf("[%s:%d] failed to decode %s%s\n",
209 __FILE__, __LINE__,gConfigLabels[i], gExt[j]);
210 }
211 canvas->drawBitmap(bm, x, yy);
212 }
200 } 213 }
201 214
202 x += SkIntToScalar(fBitmaps[i].width() + SPACER); 215 x += SkIntToScalar(fBitmaps[i].width() + SPACER);
203 if (x > maxX) { 216 if (x > maxX) {
204 maxX = x; 217 maxX = x;
205 } 218 }
206 } 219 }
207 220
208 y = (paint.getTextSize() + SkIntToScalar(fBitmaps[0].height())) * 3 / 2; 221 y = (paint.getTextSize() + SkIntToScalar(fBitmaps[0].height())) * 3 / 2;
209 x = maxX + SkIntToScalar(10); 222 x = maxX + SkIntToScalar(10);
(...skipping 12 matching lines...) Expand all
222 } 235 }
223 236
224 private: 237 private:
225 typedef SampleView INHERITED; 238 typedef SampleView INHERITED;
226 }; 239 };
227 240
228 ////////////////////////////////////////////////////////////////////////////// 241 //////////////////////////////////////////////////////////////////////////////
229 242
230 static SkView* MyFactory() { return new EncodeView; } 243 static SkView* MyFactory() { return new EncodeView; }
231 static SkViewRegister reg(MyFactory); 244 static SkViewRegister reg(MyFactory);
OLDNEW
« no previous file with comments | « samplecode/SampleDitherBitmap.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698