OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImage_Base.h" | 8 #include "SkImage_Base.h" |
9 #include "SkImagePriv.h" | 9 #include "SkImagePriv.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 #include "SkDataPixelRef.h" | 13 #include "SkDataPixelRef.h" |
14 | 14 |
15 class SkImage_Raster : public SkImage_Base { | 15 class SkImage_Raster : public SkImage_Base { |
16 public: | 16 public: |
17 static bool ValidArgs(const Info& info, size_t rowBytes) { | 17 static bool ValidArgs(const Info& info, size_t rowBytes) { |
18 const int maxDimension = SK_MaxS32 >> 2; | 18 const int maxDimension = SK_MaxS32 >> 2; |
19 const size_t kMaxPixelByteSize = SK_MaxS32; | 19 const size_t kMaxPixelByteSize = SK_MaxS32; |
20 | 20 |
21 if (info.fWidth < 0 || info.fHeight < 0) { | 21 if (info.fWidth < 0 || info.fHeight < 0) { |
22 return false; | 22 return false; |
23 } | 23 } |
24 if (info.fWidth > maxDimension || info.fHeight > maxDimension) { | 24 if (info.fWidth > maxDimension || info.fHeight > maxDimension) { |
25 return false; | 25 return false; |
26 } | 26 } |
27 if ((unsigned)info.fColorType > (unsigned)kLastEnum_ColorType) { | 27 if ((unsigned)info.fColorType > (unsigned)kLastEnum_SkColorType) { |
28 return false; | 28 return false; |
29 } | 29 } |
30 if ((unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType) { | 30 if ((unsigned)info.fAlphaType > (unsigned)kLastEnum_SkAlphaType) { |
31 return false; | 31 return false; |
32 } | 32 } |
33 | 33 |
34 if (SkImageInfoToBitmapConfig(info) == SkBitmap::kNo_Config) { | 34 if (SkImageInfoToBitmapConfig(info) == SkBitmap::kNo_Config) { |
35 return false; | 35 return false; |
36 } | 36 } |
37 | 37 |
38 // TODO: check colorspace | 38 // TODO: check colorspace |
39 | 39 |
40 if (rowBytes < SkImageMinRowBytes(info)) { | 40 if (rowBytes < SkImageMinRowBytes(info)) { |
41 return false; | 41 return false; |
42 } | 42 } |
43 | 43 |
44 int64_t size = (int64_t)info.fHeight * rowBytes; | 44 int64_t size = (int64_t)info.fHeight * rowBytes; |
45 if (size > (int64_t)kMaxPixelByteSize) { | 45 if (size > (int64_t)kMaxPixelByteSize) { |
46 return false; | 46 return false; |
47 } | 47 } |
48 return true; | 48 return true; |
49 } | 49 } |
50 | 50 |
51 static SkImage* NewEmpty(); | 51 static SkImage* NewEmpty(); |
52 | 52 |
53 SkImage_Raster(const SkImage::Info&, SkData*, size_t rb); | 53 SkImage_Raster(const SkImageInfo&, SkData*, size_t rb); |
54 virtual ~SkImage_Raster(); | 54 virtual ~SkImage_Raster(); |
55 | 55 |
56 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI
DE; | 56 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI
DE; |
57 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const
SkPaint*) SK_OVERRIDE; | 57 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const
SkPaint*) SK_OVERRIDE; |
58 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; | 58 virtual bool getROPixels(SkBitmap*) const SK_OVERRIDE; |
59 | 59 |
60 // exposed for SkSurface_Raster via SkNewImageFromPixelRef | 60 // exposed for SkSurface_Raster via SkNewImageFromPixelRef |
61 SkImage_Raster(const SkImage::Info&, SkPixelRef*, size_t rowBytes); | 61 SkImage_Raster(const SkImageInfo&, SkPixelRef*, size_t rowBytes); |
62 | 62 |
63 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } | 63 SkPixelRef* getPixelRef() const { return fBitmap.pixelRef(); } |
64 | 64 |
65 private: | 65 private: |
66 SkImage_Raster() : INHERITED(0, 0) {} | 66 SkImage_Raster() : INHERITED(0, 0) {} |
67 | 67 |
68 SkBitmap fBitmap; | 68 SkBitmap fBitmap; |
69 | 69 |
70 typedef SkImage_Base INHERITED; | 70 typedef SkImage_Base INHERITED; |
71 }; | 71 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); | 109 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
110 } | 110 } |
111 | 111 |
112 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { | 112 bool SkImage_Raster::getROPixels(SkBitmap* dst) const { |
113 *dst = fBitmap; | 113 *dst = fBitmap; |
114 return true; | 114 return true; |
115 } | 115 } |
116 | 116 |
117 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
118 | 118 |
119 SkImage* SkImage::NewRasterCopy(const SkImage::Info& info, const void* pixels, s
ize_t rowBytes) { | 119 SkImage* SkImage::NewRasterCopy(const SkImageInfo& info, const void* pixels, siz
e_t rowBytes) { |
120 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { | 120 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { |
121 return NULL; | 121 return NULL; |
122 } | 122 } |
123 if (0 == info.fWidth && 0 == info.fHeight) { | 123 if (0 == info.fWidth && 0 == info.fHeight) { |
124 return SkImage_Raster::NewEmpty(); | 124 return SkImage_Raster::NewEmpty(); |
125 } | 125 } |
126 // check this after empty-check | 126 // check this after empty-check |
127 if (NULL == pixels) { | 127 if (NULL == pixels) { |
128 return NULL; | 128 return NULL; |
129 } | 129 } |
130 | 130 |
131 // Here we actually make a copy of the caller's pixel data | 131 // Here we actually make a copy of the caller's pixel data |
132 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.fHeight * rowBytes)); | 132 SkAutoDataUnref data(SkData::NewWithCopy(pixels, info.fHeight * rowBytes)); |
133 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); | 133 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); |
134 } | 134 } |
135 | 135 |
136 | 136 |
137 SkImage* SkImage::NewRasterData(const SkImage::Info& info, SkData* pixelData, si
ze_t rowBytes) { | 137 SkImage* SkImage::NewRasterData(const SkImageInfo& info, SkData* pixelData, size
_t rowBytes) { |
138 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { | 138 if (!SkImage_Raster::ValidArgs(info, rowBytes)) { |
139 return NULL; | 139 return NULL; |
140 } | 140 } |
141 if (0 == info.fWidth && 0 == info.fHeight) { | 141 if (0 == info.fWidth && 0 == info.fHeight) { |
142 return SkImage_Raster::NewEmpty(); | 142 return SkImage_Raster::NewEmpty(); |
143 } | 143 } |
144 // check this after empty-check | 144 // check this after empty-check |
145 if (NULL == pixelData) { | 145 if (NULL == pixelData) { |
146 return NULL; | 146 return NULL; |
147 } | 147 } |
148 | 148 |
149 // did they give us enough data? | 149 // did they give us enough data? |
150 size_t size = info.fHeight * rowBytes; | 150 size_t size = info.fHeight * rowBytes; |
151 if (pixelData->size() < size) { | 151 if (pixelData->size() < size) { |
152 return NULL; | 152 return NULL; |
153 } | 153 } |
154 | 154 |
155 SkAutoDataUnref data(pixelData); | 155 SkAutoDataUnref data(pixelData); |
156 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); | 156 return SkNEW_ARGS(SkImage_Raster, (info, data, rowBytes)); |
157 } | 157 } |
158 | 158 |
159 SkImage* SkNewImageFromPixelRef(const SkImage::Info& info, SkPixelRef* pr, | 159 SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr, |
160 size_t rowBytes) { | 160 size_t rowBytes) { |
161 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes)); | 161 return SkNEW_ARGS(SkImage_Raster, (info, pr, rowBytes)); |
162 } | 162 } |
163 | 163 |
164 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) { | 164 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) { |
165 return ((SkImage_Raster*)image)->getPixelRef(); | 165 return ((SkImage_Raster*)image)->getPixelRef(); |
166 } | 166 } |
OLD | NEW |