blob: 9188257b2deb581a00c0b0f1f226ef4ff6132c03 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com11344262009-07-08 20:09:23 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkImageDecoder.h"
12#include "SkStream.h"
13
14static 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
26class DecodeView : public SkView {
27public:
28 SkBitmap fBitmap[SK_ARRAY_COUNT(gRec)];
29
30 DecodeView() {
31 SkFILEStream stream("/skimages/index.png");
32 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
bsalomon@google.com5d561952011-04-01 18:43:22 +000033 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.com11344262009-07-08 20:09:23 +000040 }
41 }
42
43protected:
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 }
68private:
69 typedef SkView INHERITED;
70};
71
72//////////////////////////////////////////////////////////////////////////////
73
74static SkView* MyFactory() { return new DecodeView; }
75static SkViewRegister reg(MyFactory);
76