epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkImageDecoder.h" |
| 12 | #include "SkStream.h" |
| 13 | |
| 14 | static const struct { |
| 15 | SkBitmap::Config fPrefConfig; |
| 16 | bool fDither; |
| 17 | } gRec[] = { |
| 18 | { SkBitmap::kIndex8_Config, false }, |
| 19 | { SkBitmap::kARGB_8888_Config, false }, |
| 20 | { SkBitmap::kARGB_4444_Config, false }, |
| 21 | { SkBitmap::kARGB_4444_Config, true }, |
| 22 | { SkBitmap::kRGB_565_Config, false }, |
| 23 | { SkBitmap::kRGB_565_Config, true }, |
| 24 | }; |
| 25 | |
| 26 | class DecodeView : public SkView { |
| 27 | public: |
| 28 | SkBitmap fBitmap[SK_ARRAY_COUNT(gRec)]; |
| 29 | |
| 30 | DecodeView() { |
| 31 | SkFILEStream stream("/skimages/index.png"); |
| 32 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
bsalomon@google.com | 5d56195 | 2011-04-01 18:43:22 +0000 | [diff] [blame] | 33 | if (codec) { |
| 34 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 35 | stream.rewind(); |
| 36 | codec->setDitherImage(gRec[i].fDither); |
| 37 | codec->decode(&stream, &fBitmap[i], gRec[i].fPrefConfig, |
| 38 | SkImageDecoder::kDecodePixels_Mode); |
| 39 | } |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 40 | } |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | // overrides from SkEventSink |
| 45 | virtual bool onQuery(SkEvent* evt) { |
| 46 | if (SampleCode::TitleQ(*evt)) { |
| 47 | SampleCode::TitleR(evt, "ImageDecoder"); |
| 48 | return true; |
| 49 | } |
| 50 | return this->INHERITED::onQuery(evt); |
| 51 | } |
| 52 | |
| 53 | void drawBG(SkCanvas* canvas) { |
| 54 | canvas->drawColor(0xFFDDDDDD); |
| 55 | // canvas->drawColor(SK_ColorWHITE); |
| 56 | } |
| 57 | |
| 58 | virtual void onDraw(SkCanvas* canvas) { |
| 59 | this->drawBG(canvas); |
| 60 | |
| 61 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
| 62 | |
| 63 | for (size_t i = 0; i < SK_ARRAY_COUNT(fBitmap); i++) { |
| 64 | canvas->drawBitmap(fBitmap[i], 0, 0); |
| 65 | canvas->translate(SkIntToScalar(fBitmap[i].width()), 0); |
| 66 | } |
| 67 | } |
| 68 | private: |
| 69 | typedef SkView INHERITED; |
| 70 | }; |
| 71 | |
| 72 | ////////////////////////////////////////////////////////////////////////////// |
| 73 | |
| 74 | static SkView* MyFactory() { return new DecodeView; } |
| 75 | static SkViewRegister reg(MyFactory); |
| 76 | |