OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkColorPriv.h" | 8 #include "SkColorPriv.h" |
9 #include "SkData.h" | 9 #include "SkData.h" |
10 #include "SkFlate.h" | 10 #include "SkFlate.h" |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 static SkPDFArray* make_indexed_color_space(const SkColorTable* table) { | 315 static SkPDFArray* make_indexed_color_space(const SkColorTable* table) { |
316 SkPDFArray* result = SkNEW(SkPDFArray); | 316 SkPDFArray* result = SkNEW(SkPDFArray); |
317 result->reserve(4); | 317 result->reserve(4); |
318 result->appendName("Indexed"); | 318 result->appendName("Indexed"); |
319 result->appendName("DeviceRGB"); | 319 result->appendName("DeviceRGB"); |
320 SkASSERT(table); | 320 SkASSERT(table); |
321 if (table->count() < 1) { | 321 if (table->count() < 1) { |
322 result->appendInt(0); | 322 result->appendInt(0); |
323 char shortTableArray[3] = {0, 0, 0}; | 323 char shortTableArray[3] = {0, 0, 0}; |
324 SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); | 324 SkString tableString(shortTableArray, SK_ARRAY_COUNT(shortTableArray)); |
325 result->append(new SkPDFString(tableString))->unref(); | 325 result->appendString(tableString); |
326 return result; | 326 return result; |
327 } | 327 } |
328 result->appendInt(table->count() - 1); // maximum color index. | 328 result->appendInt(table->count() - 1); // maximum color index. |
329 | 329 |
330 // Potentially, this could be represented in fewer bytes with a stream. | 330 // Potentially, this could be represented in fewer bytes with a stream. |
331 // Max size as a string is 1.5k. | 331 // Max size as a string is 1.5k. |
332 char tableArray[256 * 3]; | 332 char tableArray[256 * 3]; |
333 SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); | 333 SkASSERT(3u * table->count() <= SK_ARRAY_COUNT(tableArray)); |
334 uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); | 334 uint8_t* tablePtr = reinterpret_cast<uint8_t*>(tableArray); |
335 const SkPMColor* colors = table->readColors(); | 335 const SkPMColor* colors = table->readColors(); |
336 for (int i = 0; i < table->count(); i++) { | 336 for (int i = 0; i < table->count(); i++) { |
337 pmcolor_to_rgb24(colors[i], tablePtr); | 337 pmcolor_to_rgb24(colors[i], tablePtr); |
338 tablePtr += 3; | 338 tablePtr += 3; |
339 } | 339 } |
340 SkString tableString(tableArray, 3 * table->count()); | 340 SkString tableString(tableArray, 3 * table->count()); |
341 result->append(new SkPDFString(tableString))->unref(); | 341 result->appendString(tableString); |
342 return result; | 342 return result; |
343 } | 343 } |
344 | 344 |
345 void PDFDefaultBitmap::emitObject(SkWStream* stream, | 345 void PDFDefaultBitmap::emitObject(SkWStream* stream, |
346 const SkPDFObjNumMap& objNumMap, | 346 const SkPDFObjNumMap& objNumMap, |
347 const SkPDFSubstituteMap& substitutes) { | 347 const SkPDFSubstituteMap& substitutes) { |
348 SkAutoLockPixels autoLockPixels(fBitmap); | 348 SkAutoLockPixels autoLockPixels(fBitmap); |
349 SkASSERT(fBitmap.colorType() != kIndex_8_SkColorType || | 349 SkASSERT(fBitmap.colorType() != kIndex_8_SkColorType || |
350 fBitmap.getColorTable()); | 350 fBitmap.getColorTable()); |
351 | 351 |
352 // Write to a temporary buffer to get the compressed length. | 352 // Write to a temporary buffer to get the compressed length. |
353 SkDynamicMemoryWStream buffer; | 353 SkDynamicMemoryWStream buffer; |
354 SkDeflateWStream deflateWStream(&buffer); | 354 SkDeflateWStream deflateWStream(&buffer); |
355 bitmap_to_pdf_pixels(fBitmap, &deflateWStream); | 355 bitmap_to_pdf_pixels(fBitmap, &deflateWStream); |
356 deflateWStream.finalize(); // call before detachAsStream(). | 356 deflateWStream.finalize(); // call before detachAsStream(). |
357 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); | 357 SkAutoTDelete<SkStreamAsset> asset(buffer.detachAsStream()); |
358 | 358 |
359 SkPDFDict pdfDict("XObject"); | 359 SkPDFDict pdfDict("XObject"); |
360 pdfDict.insertName("Subtype", "Image"); | 360 pdfDict.insertName("Subtype", "Image"); |
361 pdfDict.insertInt("Width", fBitmap.width()); | 361 pdfDict.insertInt("Width", fBitmap.width()); |
362 pdfDict.insertInt("Height", fBitmap.height()); | 362 pdfDict.insertInt("Height", fBitmap.height()); |
363 if (fBitmap.colorType() == kIndex_8_SkColorType) { | 363 if (fBitmap.colorType() == kIndex_8_SkColorType) { |
364 SkASSERT(1 == pdf_color_component_count(fBitmap.colorType())); | 364 SkASSERT(1 == pdf_color_component_count(fBitmap.colorType())); |
365 pdfDict.insert("ColorSpace", make_indexed_color_space( | 365 pdfDict.insertObject("ColorSpace", |
366 fBitmap.getColorTable()))->unref(); | 366 make_indexed_color_space(fBitmap.getColorTable())); |
367 } else if (1 == pdf_color_component_count(fBitmap.colorType())) { | 367 } else if (1 == pdf_color_component_count(fBitmap.colorType())) { |
368 pdfDict.insertName("ColorSpace", "DeviceGray"); | 368 pdfDict.insertName("ColorSpace", "DeviceGray"); |
369 } else { | 369 } else { |
370 pdfDict.insertName("ColorSpace", "DeviceRGB"); | 370 pdfDict.insertName("ColorSpace", "DeviceRGB"); |
371 } | 371 } |
372 pdfDict.insertInt("BitsPerComponent", 8); | 372 pdfDict.insertInt("BitsPerComponent", 8); |
373 if (fSMask) { | 373 if (fSMask) { |
374 pdfDict.insert("SMask", new SkPDFObjRef(fSMask))->unref(); | 374 pdfDict.insertObjRef("SMask", SkRef(fSMask.get())); |
375 } | 375 } |
376 pdfDict.insertName("Filter", "FlateDecode"); | 376 pdfDict.insertName("Filter", "FlateDecode"); |
377 pdfDict.insertInt("Length", asset->getLength()); | 377 pdfDict.insertInt("Length", asset->getLength()); |
378 pdfDict.emitObject(stream, objNumMap, substitutes); | 378 pdfDict.emitObject(stream, objNumMap, substitutes); |
379 | 379 |
380 pdf_stream_begin(stream); | 380 pdf_stream_begin(stream); |
381 stream->writeStream(asset.get(), asset->getLength()); | 381 stream->writeStream(asset.get(), asset->getLength()); |
382 pdf_stream_end(stream); | 382 pdf_stream_end(stream); |
383 } | 383 } |
384 | 384 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 } | 473 } |
474 | 474 |
475 SkPDFObject* smask = NULL; | 475 SkPDFObject* smask = NULL; |
476 if (!bm.isOpaque() && !SkBitmap::ComputeIsOpaque(bm)) { | 476 if (!bm.isOpaque() && !SkBitmap::ComputeIsOpaque(bm)) { |
477 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); | 477 smask = SkNEW_ARGS(PDFAlphaBitmap, (bm)); |
478 } | 478 } |
479 SkPDFBitmap* pdfBitmap = SkNEW_ARGS(PDFDefaultBitmap, (bm, smask)); | 479 SkPDFBitmap* pdfBitmap = SkNEW_ARGS(PDFDefaultBitmap, (bm, smask)); |
480 canon->addBitmap(pdfBitmap); | 480 canon->addBitmap(pdfBitmap); |
481 return pdfBitmap; | 481 return pdfBitmap; |
482 } | 482 } |
OLD | NEW |