| 189 | } |
| 190 | |
| 191 | bool SkImageDecoder::cropBitmap(SkBitmap *dst, SkBitmap *src, int sampleSize, |
| 192 | int dstX, int dstY, int width, int height, |
| 193 | int srcX, int srcY) { |
| 194 | int w = width / sampleSize; |
| 195 | int h = height / sampleSize; |
| 196 | if (src->colorType() == kIndex_8_SkColorType) { |
| 197 | // kIndex8 does not allow drawing via an SkCanvas, as is done below. |
| 198 | // Instead, use extractSubset. Note that this shares the SkPixelRef and |
| 199 | // SkColorTable. |
| 200 | // FIXME: Since src is discarded in practice, this holds on to more |
| 201 | // pixels than is strictly necessary. Switch to a copy if memory |
| 202 | // savings are more important than speed here. This also means |
| 203 | // that the pixels in dst can not be reused (though there is no |
| 204 | // allocation, which was already done on src). |
| 205 | int x = (dstX - srcX) / sampleSize; |
| 206 | int y = (dstY - srcY) / sampleSize; |
| 207 | SkIRect subset = SkIRect::MakeXYWH(x, y, w, h); |
| 208 | return src->extractSubset(dst, subset); |
| 209 | } |
| 210 | // if the destination has no pixels then we must allocate them. |
| 211 | if (dst->isNull()) { |
| 212 | dst->setInfo(src->info().makeWH(w, h)); |
| 213 | |
| 214 | if (!this->allocPixelRef(dst, NULL)) { |
| 215 | SkDEBUGF(("failed to allocate pixels needed to crop the bitmap")); |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | // check to see if the destination is large enough to decode the desired |
| 220 | // region. If this assert fails we will just draw as much of the source |
| 221 | // into the destination that we can. |
| 222 | if (dst->width() < w || dst->height() < h) { |
| 223 | SkDEBUGF(("SkImageDecoder::cropBitmap does not have a large enough bitmap.\n")); |
| 224 | } |
| 225 | |
| 226 | // Set the Src_Mode for the paint to prevent transparency issue in the |
| 227 | // dest in the event that the dest was being re-used. |
| 228 | SkPaint paint; |
| 229 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 230 | |
| 231 | SkCanvas canvas(*dst); |
| 232 | canvas.drawSprite(*src, (srcX - dstX) / sampleSize, |
| 233 | (srcY - dstY) / sampleSize, |
| 234 | &paint); |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | /////////////////////////////////////////////////////////////////////////////// |
| 239 |
no test coverage detected