1/*
2 * Copyright 2012 Google Inc.
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 "gm.h"
9
10#include "Resources.h"
11#include "SkCanvas.h"
12#include "SkImageDecoder.h"
13#include "SkStream.h"
14
15namespace skiagm {
16
17/** Draw a CMYK encoded jpeg - libjpeg doesn't support CMYK->RGB
18    conversion so this tests Skia's internal processing
19*/
20class CMYKJpegGM : public GM {
21public:
22    CMYKJpegGM() {}
23
24protected:
25    virtual void onOnceBeforeDraw() SK_OVERRIDE {
26        // parameters to the "decode" call
27        bool dither = false;
28
29        SkString resourcePath = GetResourcePath();
30        if (!resourcePath.endsWith("/") && !resourcePath.endsWith("\\")) {
31            resourcePath.append("/");
32        }
33
34        resourcePath.append("CMYK.jpg");
35
36        SkFILEStream stream(resourcePath.c_str());
37        if (!stream.isValid()) {
38            SkDebugf("Could not find CMYK.jpg, please set --resourcePath correctly.\n");
39            return;
40        }
41
42        SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
43        if (codec) {
44            stream.rewind();
45            codec->setDitherImage(dither);
46            codec->decode(&stream, &fBitmap, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
47            SkDELETE(codec);
48        }
49    }
50
51    virtual SkString onShortName() {
52        return SkString("cmykjpeg");
53    }
54
55    virtual SkISize onISize() {
56        return SkISize::Make(640, 480);
57    }
58
59    virtual void onDraw(SkCanvas* canvas) {
60
61        canvas->translate(20*SK_Scalar1, 20*SK_Scalar1);
62        canvas->drawBitmap(fBitmap, 0, 0);
63    }
64
65private:
66    SkBitmap fBitmap;
67
68    typedef GM INHERITED;
69};
70
71//////////////////////////////////////////////////////////////////////////////
72
73static GM* MyFactory(void*) { return new CMYKJpegGM; }
74static GMRegistry reg(MyFactory);
75
76}
77