Index: samplecode/SampleEncode.cpp |
diff --git a/samplecode/SampleEncode.cpp b/samplecode/SampleEncode.cpp |
index 594c2a1644a9c9d22bb8db866359d76393199310..45666a3b06e92ede94efcf8ccbb39a9fb4e1ee5b 100644 |
--- a/samplecode/SampleEncode.cpp |
+++ b/samplecode/SampleEncode.cpp |
@@ -8,6 +8,8 @@ |
#include "SampleCode.h" |
#include "SkView.h" |
#include "SkCanvas.h" |
+#include "SkData.h" |
+#include "SkDecodingImageGenerator.h" |
#include "SkGradientShader.h" |
#include "SkGraphics.h" |
#include "SkImageDecoder.h" |
@@ -103,44 +105,43 @@ static const char* const gExt[] = { |
".jpg", ".png" |
}; |
-static const char* gPath = "/encoded/"; |
- |
-static void make_name(SkString* name, int config, int ext) { |
- name->set(gPath); |
- name->append(gConfigLabels[config]); |
- name->append(gExt[ext]); |
-} |
- |
#include <sys/stat.h> |
class EncodeView : public SampleView { |
public: |
- SkBitmap* fBitmaps; |
- size_t fBitmapCount; |
+ SkBitmap* fBitmaps; |
+ SkAutoDataUnref* fEncodedPNGs; |
+ SkAutoDataUnref* fEncodedJPEGs; |
+ size_t fBitmapCount; |
EncodeView() { |
#if 1 |
- (void)mkdir(gPath, S_IRWXU | S_IRWXG | S_IRWXO); |
- |
fBitmapCount = SK_ARRAY_COUNT(gConfigs); |
fBitmaps = new SkBitmap[fBitmapCount]; |
+ fEncodedPNGs = new SkAutoDataUnref[fBitmapCount]; |
+ fEncodedJPEGs = new SkAutoDataUnref[fBitmapCount]; |
for (size_t i = 0; i < fBitmapCount; i++) { |
make_image(&fBitmaps[i], gConfigs[i], i); |
- for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) { |
- SkString path; |
- make_name(&path, i, j); |
- |
- // remove any previous run of this file |
- remove(path.c_str()); |
- |
- SkImageEncoder* codec = SkImageEncoder::Create(gTypes[j]); |
- if (NULL == codec || |
- !codec->encodeFile(path.c_str(), fBitmaps[i], 100)) { |
- SkDebugf("------ failed to encode %s\n", path.c_str()); |
- remove(path.c_str()); // remove any partial file |
+ for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) { |
+ SkAutoTDelete<SkImageEncoder> codec( |
+ SkImageEncoder::Create(gTypes[j])); |
+ if (NULL == codec.get()) { |
+ SkDebugf("[%s:%d] failed to encode %s%s\n", |
+ __FILE__, __LINE__,gConfigLabels[i], gExt[j]); |
+ continue; |
+ } |
+ SkAutoDataUnref data(codec->encodeData(fBitmaps[i], 100)); |
+ if (NULL == data.get()) { |
+ SkDebugf("[%s:%d] failed to encode %s%s\n", |
+ __FILE__, __LINE__,gConfigLabels[i], gExt[j]); |
+ continue; |
+ } |
+ if (SkImageEncoder::kJPEG_Type == gTypes[j]) { |
+ fEncodedJPEGs[i].reset(data.detach()); |
+ } else if (SkImageEncoder::kPNG_Type == gTypes[j]) { |
+ fEncodedPNGs[i].reset(data.detach()); |
} |
- delete codec; |
} |
} |
#else |
@@ -152,6 +153,8 @@ public: |
virtual ~EncodeView() { |
delete[] fBitmaps; |
+ delete[] fEncodedPNGs; |
+ delete[] fEncodedJPEGs; |
} |
protected: |
@@ -187,16 +190,26 @@ protected: |
canvas->drawBitmap(fBitmaps[i], x, y); |
SkScalar yy = y; |
- for (size_t j = 0; j < SK_ARRAY_COUNT(gExt); j++) { |
+ for (size_t j = 0; j < SK_ARRAY_COUNT(gTypes); j++) { |
yy += SkIntToScalar(fBitmaps[i].height() + 10); |
SkBitmap bm; |
- SkString name; |
- |
- make_name(&name, i, j); |
- |
- SkImageDecoder::DecodeFile(name.c_str(), &bm); |
- canvas->drawBitmap(bm, x, yy); |
+ SkData* encoded = NULL; |
+ if (SkImageEncoder::kJPEG_Type == gTypes[j]) { |
+ encoded = fEncodedJPEGs[i].get(); |
+ } else if (SkImageEncoder::kPNG_Type == gTypes[j]) { |
+ encoded = fEncodedPNGs[i].get(); |
+ } |
+ if (encoded) { |
+ if (!SkInstallDiscardablePixelRef( |
+ SkDecodingImageGenerator::Create(encoded, |
+ SkDecodingImageGenerator::Options()), |
+ &bm, NULL)) { |
+ SkDebugf("[%s:%d] failed to decode %s%s\n", |
+ __FILE__, __LINE__,gConfigLabels[i], gExt[j]); |
+ } |
+ canvas->drawBitmap(bm, x, yy); |
+ } |
} |
x += SkIntToScalar(fBitmaps[i].width() + SPACER); |