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 #include "SkImage_Base.h"
9 #include "SkImagePriv.h"
13 #include "SkDecodingImageGenerator.h"
15 class SkImage_Raster : public SkImage_Base {
17 static bool ValidArgs(const Info& info, size_t rowBytes) {
18 const int maxDimension = SK_MaxS32 >> 2;
19 const size_t kMaxPixelByteSize = SK_MaxS32;
21 if (info.width() < 0 || info.height() < 0) {
24 if (info.width() > maxDimension || info.height() > maxDimension) {
27 if ((unsigned)info.colorType() > (unsigned)kLastEnum_SkColorType) {
30 if ((unsigned)info.alphaType() > (unsigned)kLastEnum_SkAlphaType) {
34 if (kUnknown_SkColorType == info.colorType()) {
38 // TODO: check colorspace
40 if (rowBytes < SkImageMinRowBytes(info)) {
44 int64_t size = (int64_t)info.height() * rowBytes;
45 if (size > (int64_t)kMaxPixelByteSize) {
51 static SkImage* NewEmpty();
53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb);
54 virtual ~SkImage_Raster();
56 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE;
57 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
58 const SkPaint*) const SK_OVERRIDE;
59 virtual bool onReadPixels(SkBitmap*, const SkIRect&) const SK_OVERRIDE;
60 virtual const void* onPeekPixels(SkImageInfo*, size_t* /*rowBytes*/) const SK_OVERRIDE;
61 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE;
63 // exposed for SkSurface_Raster via SkNewImageFromPixelRef
64 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes);
66 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); }
68 virtual SkShader* onNewShader(SkShader::TileMode,
70 const SkMatrix* localMatrix) const SK_OVERRIDE;
72 virtual bool isOpaque() const SK_OVERRIDE;
74 SkImage_Raster(const SkBitmap& bm)
75 : INHERITED(bm.width(), bm.height())
79 SkImage_Raster() : INHERITED(0, 0) {}
83 typedef SkImage_Base INHERITED;
86 ///////////////////////////////////////////////////////////////////////////////
88 SkImage* SkImage_Raster::NewEmpty() {
89 // Returns lazily created singleton
90 static SkImage* gEmpty;
92 gEmpty = SkNEW(SkImage_Raster);
98 static void release_data(void* addr, void* context) {
99 SkData* data = static_cast<SkData*>(context);
103 SkImage_Raster::SkImage_Raster(const Info& info, SkData* data, size_t rowBytes)
104 : INHERITED(info.width(), info.height())
107 void* addr = const_cast<void*>(data->data());
108 SkColorTable* ctable = NULL;
110 fBitmap.installPixels(info, addr, rowBytes, ctable, release_data, data);
111 fBitmap.setImmutable();
112 fBitmap.lockPixels();
115 SkImage_Raster::SkImage_Raster(const Info& info, SkPixelRef* pr, size_t rowBytes)
116 : INHERITED(info.width(), info.height())
118 fBitmap.setInfo(info, rowBytes);
119 fBitmap.setPixelRef(pr);
120 fBitmap.lockPixels();
123 SkImage_Raster::~SkImage_Raster() {}
125 SkShader* SkImage_Raster::onNewShader(SkShader::TileMode tileX, SkShader::TileMode tileY,
126 const SkMatrix* localMatrix) const {
127 return SkShader::CreateBitmapShader(fBitmap, tileX, tileY, localMatrix);
130 void SkImage_Raster::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
131 canvas->drawBitmap(fBitmap, x, y, paint);
134 void SkImage_Raster::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
135 const SkPaint* paint) const {
136 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
139 bool SkImage_Raster::onReadPixels(SkBitmap* dst, const SkIRect& subset) const {
140 if (dst->pixelRef()) {
141 return this->INHERITED::onReadPixels(dst, subset);
144 if (!fBitmap.extractSubset(&src, subset)) {
147 return src.copyTo(dst, src.colorType());
151 const void* SkImage_Raster::onPeekPixels(SkImageInfo* infoPtr, size_t* rowBytesPtr) const {
152 const SkImageInfo info = fBitmap.info();
153 if ((kUnknown_SkColorType == info.colorType()) || !fBitmap.getPixels()) {
157 *rowBytesPtr = fBitmap.rowBytes();
158 return fBitmap.getPixels();
161 bool SkImage_Raster::getROPixels(SkBitmap* dst) const {
166 ///////////////////////////////////////////////////////////////////////////////
168 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, size_t rowBytes) {
169 if (!SkImage_Raster::ValidArgs(info, rowBytes)) {
172 if (0 == info.width() && 0 == info.height()) {
173 return SkImage_Raster::NewEmpty();
175 // check this after empty-check
176 if (NULL == pixels) {
180 // Here we actually make a copy of the caller's pixel data
181 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.height() * rowBytes));
182 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes));
186 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* data, size_t rowBytes) {
187 if (!SkImage_Raster::ValidArgs(info, rowBytes)) {
190 if (0 == info.width() && 0 == info.height()) {
191 return SkImage_Raster::NewEmpty();
193 // check this after empty-check
198 // did they give us enough data?
199 size_t size = info.height() * rowBytes;
200 if (data->size() < size) {
204 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes));
207 SkImage* SkImage::NewFromGenerator(SkImageGenerator* generator) {
209 if (!SkInstallDiscardablePixelRef(generator, &bitmap)) {
212 return SkNEW_ARGS(SkImage_Raster, (bitmap));
215 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
217 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes));
220 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) {
221 return ((SkImage_Raster*)image)->getPixelRef();
224 bool SkImage_Raster::isOpaque() const {
225 return fBitmap.isOpaque();