Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2350)

Unified Diff: src/pdf/SkPDFDevice.cpp

Issue 27236007: Implement perspective for bitmaps in pdf. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: reupload, some error happened at last upload Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pdf/SkPDFDevice.cpp
diff --git a/src/pdf/SkPDFDevice.cpp b/src/pdf/SkPDFDevice.cpp
index 43117a2b2fc4d5760d692cb50970070721d71b0c..d4bb2b64a5854a8a611b43462deba29389d8f1cf 100644
--- a/src/pdf/SkPDFDevice.cpp
+++ b/src/pdf/SkPDFDevice.cpp
@@ -2028,13 +2028,82 @@ int SkPDFDevice::getFontResourceIndex(SkTypeface* typeface, uint16_t glyphID) {
return resourceIndex;
}
-void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix,
+static void setup_transparent_bitmap(SkBitmap* bitmap, int width, int height) {
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ bitmap->allocPixels();
+ bitmap->eraseColor(SK_ColorTRANSPARENT);
+}
+
+void SkPDFDevice::internalDrawBitmap(const SkMatrix& origMatrix,
const SkClipStack* clipStack,
- const SkRegion& clipRegion,
- const SkBitmap& bitmap,
- const SkIRect* srcRect,
+ const SkRegion& origClipRegion,
+ const SkBitmap& origBitmap,
+ const SkIRect* origSrcRect,
const SkPaint& paint) {
- // TODO(edisonn): Perspective matrix support implemented here
+ SkMatrix matrix = origMatrix;
+ SkRegion all;
vandebo (ex-Chrome) 2013/10/15 21:41:32 nit: all -> perspectiveBounds ?
edisonn 2013/10/16 15:08:03 Done.
+ const SkRegion* clipRegion = &origClipRegion;
+ SkBitmap subsetBitmap = origBitmap;
vandebo (ex-Chrome) 2013/10/15 21:41:32 nit: Pull inside the hasPerspective if on 2051 and
edisonn 2013/10/16 15:08:03 Done.
+ SkBitmap bitmap = origBitmap;
vandebo (ex-Chrome) 2013/10/15 21:41:32 For both the perspective and non-perspective case
edisonn 2013/10/16 15:08:03 Done.
+ const SkIRect* srcRect = origSrcRect;
vandebo (ex-Chrome) 2013/10/15 21:41:32 not needed, just reset origSrcRect to NULL if you
edisonn 2013/10/16 15:08:03 Done.
+
+ // Raster the bitmap using perspective in a new bitmap.
+ if (matrix.hasPerspective()) {
vandebo (ex-Chrome) 2013/10/15 21:41:32 nit: use origMatrix in this code block (except whe
edisonn 2013/10/16 15:08:03 Done.
+ if (origSrcRect) {
+ if (!origBitmap.extractSubset(&subsetBitmap, *origSrcRect)) {
+ return;
+ }
+ } else {
+ subsetBitmap = origBitmap;
+ }
+ srcRect = NULL;
+
+ // Transform the bitmap in the new space.
+ SkPath path;
vandebo (ex-Chrome) 2013/10/15 21:41:32 nit: path -> perspectiveOutline ?
edisonn 2013/10/16 15:08:03 Done.
+ path.addRect(SkRect::MakeWH(SkIntToScalar(subsetBitmap.width()),
+ SkIntToScalar(subsetBitmap.height())));
+ path.transform(matrix);
+
+ // Retrieve the bounds of the new shape.
+ SkRect bounds = path.getBounds();
+
+ // TODO(edisonn): add DPI settings. Currently 1 pixel/point, which does not look great,
vandebo (ex-Chrome) 2013/10/15 21:41:32 Nothing to be done, but this isn't necessarily 72
+ // but it is not producing large PDFs.
+
+ // Create transparent bitmap.
+ setup_transparent_bitmap(&bitmap, bounds.width(), bounds.height());
vandebo (ex-Chrome) 2013/10/15 21:41:32 This approach looks fine and could be committed th
edisonn 2013/10/16 15:08:03 Added a todo, will keep the code as is On 2013/10/
+
+ SkAutoTUnref<SkBitmapDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap)));
vandebo (ex-Chrome) 2013/10/15 21:41:32 Why not stack allocate this?
edisonn 2013/10/16 15:08:03 Done.
+ SkCanvas canvas(device);
+
+ // Find where (0,0) gets mapped in the new space.
+ SkPoint zeroZero = SkPoint::Make(SkIntToScalar(0), SkIntToScalar(0));
+ matrix.mapPoints(&zeroZero, 1);
+
+ SkScalar deltaX = bounds.left();
+ SkScalar deltaY = bounds.top();
+
+ SkMatrix matrix2 = matrix;
vandebo (ex-Chrome) 2013/10/15 21:41:32 nit: matrix2 -> offsetMatrix ?
edisonn 2013/10/16 15:08:03 Done.
+ matrix2.postTranslate(-deltaX, -deltaY);
+
+ // Translate the draw in the new canvas, so we perfectly fit the shape in the bitmap.
+ canvas.setMatrix(matrix2);
+
+ canvas.drawBitmap(subsetBitmap, SkIntToScalar(0), SkIntToScalar(0));
+
+ // Make sure the final bits ar in the bitmap.
+ canvas.flush();
+
+ // In the new space, we use the identity matrix translated.
+ matrix.setTranslate(deltaX, deltaY);
+ all.setRect(SkIRect::MakeXYWH(SkScalarToFixed(bounds.x()),
+ SkScalarToFixed(bounds.y()),
+ SkScalarToFixed(bounds.width()),
+ SkScalarToFixed(bounds.height())));
+ clipRegion = &all;
+ srcRect = NULL;
+ }
+
SkMatrix scaled;
// Adjust for origin flip.
scaled.setScale(SK_Scalar1, -SK_Scalar1);
@@ -2044,7 +2113,8 @@ void SkPDFDevice::internalDrawBitmap(const SkMatrix& matrix,
scaled.postScale(SkIntToScalar(subset.width()),
SkIntToScalar(subset.height()));
scaled.postConcat(matrix);
- ScopedContentEntry content(this, clipStack, clipRegion, scaled, paint);
+
+ ScopedContentEntry content(this, clipStack, *clipRegion, scaled, paint);
if (!content.entry()) {
return;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698