2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkImage_DEFINED
9 #define SkImage_DEFINED
12 #include "SkImageEncoder.h"
29 kRGBA_8888_SkColorType,
30 kBGRA_8888_SkColorType,
32 #if SK_PMCOLOR_BYTE_ORDER(B,G,R,A)
33 kPMColor_SkColorType = kBGRA_8888_SkColorType,
34 #elif SK_PMCOLOR_BYTE_ORDER(R,G,B,A)
35 kPMColor_SkColorType = kRGBA_8888_SkColorType,
37 #error "SK_*32_SHFIT values must correspond to BGRA or RGBA byte order"
40 kLastEnum_SkColorType = kBGRA_8888_SkColorType
46 SkColorType fColorType;
47 SkAlphaType fAlphaType;
51 * SkImage is an abstraction for drawing a rectagle of pixels, though the
52 * particular type of image could be actually storing its data on the GPU, or
53 * as drawing commands (picture or PDF or otherwise), ready to be played back
54 * into another canvas.
56 * The content of SkImage is always immutable, though the actual storage may
57 * change, if for example that image can be re-created via encoded data or
60 class SK_API SkImage : public SkRefCnt {
62 SK_DECLARE_INST_COUNT(SkImage)
64 #ifdef SK_SUPPORT_LEGACY_COLORTYPE
65 typedef SkColorType ColorType;
67 static const SkColorType kAlpha_8_ColorType = kAlpha_8_SkColorType;
68 static const SkColorType kRGB_565_ColorType = kRGB_565_SkColorType;
69 static const SkColorType kRGBA_8888_ColorType = kRGBA_8888_SkColorType;
70 static const SkColorType kBGRA_8888_ColorType = kBGRA_8888_SkColorType;
71 static const SkColorType kPMColor_ColorType = kPMColor_SkColorType;
72 static const SkColorType kLastEnum_ColorType = kLastEnum_SkColorType;
75 #ifdef SK_SUPPORT_LEGACY_ALPHATYPE
76 typedef SkAlphaType AlphaType;
78 static const SkAlphaType kIgnore_AlphaType = kIgnore_SkAlphaType;
79 static const SkAlphaType kOpaque_AlphaType = kOpaque_SkAlphaType;
80 static const SkAlphaType kPremul_AlphaType = kPremul_SkAlphaType;
81 static const SkAlphaType kUnpremul_AlphaType = kUnpremul_SkAlphaType;
84 typedef SkImageInfo Info;
86 static SkImage* NewRasterCopy(const Info&, const void* pixels, size_t rowBytes);
87 static SkImage* NewRasterData(const Info&, SkData* pixels, size_t rowBytes);
88 static SkImage* NewEncodedData(SkData*);
91 * GrTexture is a more logical parameter for this factory, but its
92 * interactions with scratch cache still has issues, so for now we take
93 * SkBitmap instead. This will be changed in the future. skbug.com/1449
95 static SkImage* NewTexture(const SkBitmap&);
97 int width() const { return fWidth; }
98 int height() const { return fHeight; }
99 uint32_t uniqueID() const { return fUniqueID; }
102 * Return the GrTexture that stores the image pixels. Calling getTexture
103 * does not affect the reference count of the GrTexture object.
104 * Will return NULL if the image does not use a texture.
106 GrTexture* getTexture();
108 SkShader* newShaderClamp() const;
109 SkShader* newShader(SkShader::TileMode, SkShader::TileMode) const;
111 void draw(SkCanvas*, SkScalar x, SkScalar y, const SkPaint*);
114 * Draw the image, cropped to the src rect, to the dst rect of a canvas.
115 * If src is larger than the bounds of the image, the rest of the image is
116 * filled with transparent black pixels.
118 * See SkCanvas::drawBitmapRectToRect for similar behavior.
120 void draw(SkCanvas*, const SkRect* src, const SkRect& dst, const SkPaint*);
123 * Encode the image's pixels and return the result as a new SkData, which
124 * the caller must manage (i.e. call unref() when they are done).
126 * If the image type cannot be encoded, or the requested encoder type is
127 * not supported, this will return NULL.
129 SkData* encode(SkImageEncoder::Type t = SkImageEncoder::kPNG_Type,
130 int quality = 80) const;
133 SkImage(int width, int height) :
136 fUniqueID(NextUniqueID()) {
138 SkASSERT(width >= 0);
139 SkASSERT(height >= 0);
145 const uint32_t fUniqueID;
147 static uint32_t NextUniqueID();
149 typedef SkRefCnt INHERITED;