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

Side by Side Diff: src/pdf/SkPDFDevice.cpp

Issue 54913004: Implement DPI for perspective bitmaps in PDF - we save the bitmap at the resolution requested. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: use SkScalar instead of float Created 7 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/pdf/SkPDFDevice.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 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 "SkPDFDevice.h" 8 #include "SkPDFDevice.h"
9 9
10 #include "SkAnnotation.h" 10 #include "SkAnnotation.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 int upperBounds; 42 int upperBounds;
43 43
44 bool operator==(const TypefaceFallbackData& b) const { 44 bool operator==(const TypefaceFallbackData& b) const {
45 return typeface == b.typeface && 45 return typeface == b.typeface &&
46 lowerBounds == b.lowerBounds && 46 lowerBounds == b.lowerBounds &&
47 upperBounds == b.upperBounds; 47 upperBounds == b.upperBounds;
48 } 48 }
49 }; 49 };
50 #endif 50 #endif
51 51
52 #define DPI_FOR_RASTER_SCALE_ONE 72
53
52 // Utility functions 54 // Utility functions
53 55
54 static void emit_pdf_color(SkColor color, SkWStream* result) { 56 static void emit_pdf_color(SkColor color, SkWStream* result) {
55 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere. 57 SkASSERT(SkColorGetA(color) == 0xFF); // We handle alpha elsewhere.
56 SkScalar colorMax = SkIntToScalar(0xFF); 58 SkScalar colorMax = SkIntToScalar(0xFF);
57 SkPDFScalar::Append( 59 SkPDFScalar::Append(
58 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result); 60 SkScalarDiv(SkIntToScalar(SkColorGetR(color)), colorMax), result);
59 result->writeText(" "); 61 result->writeText(" ");
60 SkPDFScalar::Append( 62 SkPDFScalar::Append(
61 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result); 63 SkScalarDiv(SkIntToScalar(SkColorGetG(color)), colorMax), result);
(...skipping 2094 matching lines...) Expand 10 before | Expand all | Expand 10 after
2156 const SkPaint& paint) { 2158 const SkPaint& paint) {
2157 SkMatrix matrix = origMatrix; 2159 SkMatrix matrix = origMatrix;
2158 SkRegion perspectiveBounds; 2160 SkRegion perspectiveBounds;
2159 const SkRegion* clipRegion = &origClipRegion; 2161 const SkRegion* clipRegion = &origClipRegion;
2160 SkBitmap perspectiveBitmap; 2162 SkBitmap perspectiveBitmap;
2161 const SkBitmap* bitmap = &origBitmap; 2163 const SkBitmap* bitmap = &origBitmap;
2162 SkBitmap tmpSubsetBitmap; 2164 SkBitmap tmpSubsetBitmap;
2163 2165
2164 // Rasterize the bitmap using perspective in a new bitmap. 2166 // Rasterize the bitmap using perspective in a new bitmap.
2165 if (origMatrix.hasPerspective()) { 2167 if (origMatrix.hasPerspective()) {
2168 if (fRasterDpi == 0) {
2169 return;
2170 }
2166 SkBitmap* subsetBitmap; 2171 SkBitmap* subsetBitmap;
2167 if (srcRect) { 2172 if (srcRect) {
2168 if (!origBitmap.extractSubset(&tmpSubsetBitmap, *srcRect)) { 2173 if (!origBitmap.extractSubset(&tmpSubsetBitmap, *srcRect)) {
2169 return; 2174 return;
2170 } 2175 }
2171 subsetBitmap = &tmpSubsetBitmap; 2176 subsetBitmap = &tmpSubsetBitmap;
2172 } else { 2177 } else {
2173 subsetBitmap = &tmpSubsetBitmap; 2178 subsetBitmap = &tmpSubsetBitmap;
2174 *subsetBitmap = origBitmap; 2179 *subsetBitmap = origBitmap;
2175 } 2180 }
2176 srcRect = NULL; 2181 srcRect = NULL;
2177 2182
2178 // Transform the bitmap in the new space. 2183 // Transform the bitmap in the new space.
2179 SkPath perspectiveOutline; 2184 SkPath perspectiveOutline;
2180 perspectiveOutline.addRect( 2185 perspectiveOutline.addRect(
2181 SkRect::MakeWH(SkIntToScalar(subsetBitmap->width()), 2186 SkRect::MakeWH(SkIntToScalar(subsetBitmap->width()),
2182 SkIntToScalar(subsetBitmap->height()))); 2187 SkIntToScalar(subsetBitmap->height())));
2183 perspectiveOutline.transform(origMatrix); 2188 perspectiveOutline.transform(origMatrix);
2184 2189
2185 // TODO(edisonn): perf - use current clip too. 2190 // TODO(edisonn): perf - use current clip too.
2186 // Retrieve the bounds of the new shape. 2191 // Retrieve the bounds of the new shape.
2187 SkRect bounds = perspectiveOutline.getBounds(); 2192 SkRect bounds = perspectiveOutline.getBounds();
2193 SkScalar scaleX = SkIntToScalar(fRasterDpi) /
2194 SkIntToScalar(DPI_FOR_RASTER_SCALE_ONE);
2195 SkScalar scaleY = scaleX;
2188 2196
2189 // TODO(edisonn): add DPI settings. Currently 1 pixel/point, which does 2197 scaleX *= fInitialTransform.getScaleX();
vandebo (ex-Chrome) 2013/11/04 19:55:10 The initial transform is already applied to the ca
edisonn 2013/11/05 20:33:32 Done.
2190 // not look great, but it is not producing large PDFs. 2198 scaleY *= fInitialTransform.getScaleY();
2199
2200 scaleX = SkScalarAbs(scaleX);
2201 scaleY = SkScalarAbs(scaleY);
2191 2202
2192 // TODO(edisonn): A better approach would be to use a bitmap shader 2203 // TODO(edisonn): A better approach would be to use a bitmap shader
2193 // (in clamp mode) and draw a rect over the entire bounding box. Then 2204 // (in clamp mode) and draw a rect over the entire bounding box. Then
2194 // intersect perspectiveOutline to the clip. That will avoid introducing 2205 // intersect perspectiveOutline to the clip. That will avoid introducing
2195 // alpha to the image while still giving good behavior at the edge of 2206 // alpha to the image while still giving good behavior at the edge of
2196 // the image. Avoiding alpha will reduce the pdf size and generation 2207 // the image. Avoiding alpha will reduce the pdf size and generation
2197 // CPU time some. 2208 // CPU time some.
2198 2209
2199 perspectiveBitmap.setConfig(SkBitmap::kARGB_8888_Config, 2210 perspectiveBitmap.setConfig(
2200 SkScalarCeilToInt(bounds.width()), 2211 SkBitmap::kARGB_8888_Config,
2201 SkScalarCeilToInt(bounds.height())); 2212 SkScalarCeilToInt(scaleX * bounds.width()),
vandebo (ex-Chrome) 2013/11/04 19:55:10 Apply Abs here instead of on 2200 so you don't hav
edisonn 2013/11/05 20:33:32 Done.
2213 SkScalarCeilToInt(scaleY * bounds.height()));
2202 perspectiveBitmap.allocPixels(); 2214 perspectiveBitmap.allocPixels();
2203 perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT); 2215 perspectiveBitmap.eraseColor(SK_ColorTRANSPARENT);
2204 2216
2205 SkBitmapDevice device(perspectiveBitmap); 2217 SkBitmapDevice device(perspectiveBitmap);
2206 SkCanvas canvas(&device); 2218 SkCanvas canvas(&device);
2207 2219
2208 SkScalar deltaX = bounds.left(); 2220 SkScalar deltaX = bounds.left();
2209 SkScalar deltaY = bounds.top(); 2221 SkScalar deltaY = bounds.top();
2210 2222
2211 SkMatrix offsetMatrix = origMatrix; 2223 SkMatrix offsetMatrix = origMatrix;
2212 offsetMatrix.postTranslate(-deltaX, -deltaY); 2224 offsetMatrix.postTranslate(-deltaX, -deltaY);
2225 offsetMatrix.postScale(scaleX, scaleY);
vandebo (ex-Chrome) 2013/11/04 19:55:10 I'm not sure if you need to unapply the initial tr
edisonn 2013/11/05 20:33:32 Done.
2213 2226
2214 // Translate the draw in the new canvas, so we perfectly fit the 2227 // Translate the draw in the new canvas, so we perfectly fit the
2215 // shape in the bitmap. 2228 // shape in the bitmap.
2216 canvas.setMatrix(offsetMatrix); 2229 canvas.setMatrix(offsetMatrix);
2217 2230
2218 canvas.drawBitmap(*subsetBitmap, SkIntToScalar(0), SkIntToScalar(0)); 2231 canvas.drawBitmap(*subsetBitmap, SkIntToScalar(0), SkIntToScalar(0));
2219 2232
2220 // Make sure the final bits are in the bitmap. 2233 // Make sure the final bits are in the bitmap.
2221 canvas.flush(); 2234 canvas.flush();
2222 2235
2223 // In the new space, we use the identity matrix translated. 2236 // In the new space, we use the identity matrix translated
2224 matrix.setTranslate(deltaX, deltaY); 2237 // and scaled to reflect DPI.
vandebo (ex-Chrome) 2013/11/04 19:55:10 But we need to unapply it here so that it doesn't
edisonn 2013/11/05 20:33:32 Done.
2238 matrix.setScale(1 / scaleX, 1 / scaleY);
2239 matrix.postTranslate(deltaX, deltaY);
2225 perspectiveBounds.setRect( 2240 perspectiveBounds.setRect(
2226 SkIRect::MakeXYWH(SkScalarFloorToInt(bounds.x()), 2241 SkIRect::MakeXYWH(SkScalarFloorToInt(bounds.x()),
2227 SkScalarFloorToInt(bounds.y()), 2242 SkScalarFloorToInt(bounds.y()),
2228 SkScalarCeilToInt(bounds.width()), 2243 SkScalarCeilToInt(bounds.width()),
2229 SkScalarCeilToInt(bounds.height()))); 2244 SkScalarCeilToInt(bounds.height())));
2230 clipRegion = &perspectiveBounds; 2245 clipRegion = &perspectiveBounds;
2231 srcRect = NULL; 2246 srcRect = NULL;
2232 bitmap = &perspectiveBitmap; 2247 bitmap = &perspectiveBitmap;
2233 } 2248 }
2234 2249
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2267 } 2282 }
2268 2283
2269 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y, 2284 bool SkPDFDevice::onReadPixels(const SkBitmap& bitmap, int x, int y,
2270 SkCanvas::Config8888) { 2285 SkCanvas::Config8888) {
2271 return false; 2286 return false;
2272 } 2287 }
2273 2288
2274 bool SkPDFDevice::allowImageFilter(SkImageFilter*) { 2289 bool SkPDFDevice::allowImageFilter(SkImageFilter*) {
2275 return false; 2290 return false;
2276 } 2291 }
OLDNEW
« no previous file with comments | « include/pdf/SkPDFDevice.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698