static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* target, bool sendBinaries) { if (sendBinaries) { SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { // PNG encode doesn't necessarily support all color formats, convert to a different // format size_t rowBytes = 4 * image.width(); void* buffer = sk_malloc_throw(rowBytes * image.height()); SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), kN32_SkColorType, kPremul_SkAlphaType); if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { SkDebugf("readPixels failed\n"); return false; } SkImage* converted = SkImage::NewRasterCopy(dstInfo, buffer, rowBytes); encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); if (encoded == nullptr) { SkDebugf("image encode failed\n"); return false; } free(converted); free(buffer); } Json::Value bytes; encode_data(encoded->data(), encoded->size(), &bytes); (*target)[SKJSONCANVAS_ATTRIBUTE_BYTES] = bytes; encoded->unref(); } else { SkString description = SkStringPrintf("%dx%d pixel image", image.width(), image.height()); (*target)[SKJSONCANVAS_ATTRIBUTE_DESCRIPTION] = Json::Value(description.c_str()); } return true; }
void onDraw(SkCanvas* canvas) override { canvas->clear(SK_ColorBLACK); SkPaint paint; for (int i = 0; i < 4; i++) { SkImage* image = (i & 0x01) ? fCheckerboard.get() : fBitmap.get(); SkIRect cropRect = SkIRect::MakeXYWH(i * 12, i * 8, image->width() - i * 8, image->height() - i * 12); SkImageFilter::CropRect rect(SkRect::Make(cropRect)); SkAutoTUnref<SkImageFilter> tileInput(SkImageSource::Create(image)); SkScalar dx = SkIntToScalar(i*5); SkScalar dy = SkIntToScalar(i*10); SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(dx, dy, tileInput, &rect)); paint.setImageFilter(filter); DrawClippedImage(canvas, image, paint, 1, cropRect); canvas->translate(SkIntToScalar(image->width() + MARGIN), 0); } SkIRect cropRect = SkIRect::MakeXYWH(0, 0, 100, 100); SkImageFilter::CropRect rect(SkRect::Make(cropRect)); SkAutoTUnref<SkImageFilter> filter(SkOffsetImageFilter::Create(-5, -10, nullptr, &rect)); paint.setImageFilter(filter); DrawClippedImage(canvas, fBitmap.get(), paint, 2, cropRect); }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); SkImage* image = SkImage::NewEncodedData(data); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); image->draw(canvas,0, 0, NULL); image->unref(); } }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { // TODO: Make this draw a file that is checked in, so it can // be exercised on machines other than mike's. Will require a // rebaseline. SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg")); SkImage* image = SkImage::NewEncodedData(data); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); image->draw(canvas,0, 0, NULL); image->unref(); } }
static void drawJpeg(SkCanvas* canvas, const SkISize& size) { // TODO: Make this draw a file that is checked in, so it can // be exercised on machines other than mike's. Will require a // rebaseline. SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg")); if (NULL == data.get()) { return; } SkImage* image = SkImage::NewFromGenerator( SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options())); if (image) { SkAutoCanvasRestore acr(canvas, true); canvas->scale(size.width() * 1.0f / image->width(), size.height() * 1.0f / image->height()); canvas->drawImage(image, 0, 0, NULL); image->unref(); } }
void HelloWorldWindow::draw(SkCanvas* canvas) { drawContents(canvas); // in case we have queued drawing calls fContext->flush(); // Invalidate the window to force a redraw. Poor man's animation mechanism. this->inval(NULL); if (kRaster_DeviceType == fType) { // need to send the raster bits to the (gpu) window SkImage* snap = fSurface->newImageSnapshot(); size_t rowBytes; SkImageInfo info; const void* pixels = snap->peekPixels(&info, &rowBytes); fRenderTarget->writePixels(0, 0, snap->width(), snap->height(), SkImageInfo2GrPixelConfig(info.colorType(), info.alphaType(), info.profileType()), pixels, rowBytes, GrContext::kFlushWrites_PixelOp); SkSafeUnref(snap); } INHERITED::present(); }