OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkDrawCommand.h" | 10 #include "SkDrawCommand.h" |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 buffer.writeToMemory(data); | 599 buffer.writeToMemory(data); |
600 Json::Value jsonData; | 600 Json::Value jsonData; |
601 encode_data(data, buffer.bytesWritten(), "application/octet-stream", urlData
Manager, &jsonData); | 601 encode_data(data, buffer.bytesWritten(), "application/octet-stream", urlData
Manager, &jsonData); |
602 Json::Value jsonFlattenable; | 602 Json::Value jsonFlattenable; |
603 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable->get
TypeName()); | 603 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_NAME] = Json::Value(flattenable->get
TypeName()); |
604 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; | 604 jsonFlattenable[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; |
605 (*target) = jsonFlattenable; | 605 (*target) = jsonFlattenable; |
606 sk_free(data); | 606 sk_free(data); |
607 } | 607 } |
608 | 608 |
| 609 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { |
| 610 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
| 611 out->write(data, length); |
| 612 } |
| 613 |
| 614 void SkDrawCommand::WritePNG(const png_bytep rgba, png_uint_32 width, png_uint_3
2 height, |
| 615 SkWStream& out) { |
| 616 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL); |
| 617 SkASSERT(png != nullptr); |
| 618 png_infop info_ptr = png_create_info_struct(png); |
| 619 SkASSERT(info_ptr != nullptr); |
| 620 if (setjmp(png_jmpbuf(png))) { |
| 621 SkFAIL("png encode error"); |
| 622 } |
| 623 png_set_IHDR(png, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB, PNG_INTERL
ACE_NONE, |
| 624 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
| 625 png_set_compression_level(png, 1); |
| 626 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); |
| 627 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 3); |
| 628 for (png_size_t y = 0; y < height; ++y) { |
| 629 const png_bytep src = rgba + y * width * 4; |
| 630 rows[y] = pixels + y * width * 3; |
| 631 // convert from RGBA to RGB |
| 632 for (png_size_t x = 0; x < width; ++x) { |
| 633 rows[y][x * 3] = src[x * 4]; |
| 634 rows[y][x * 3 + 1] = src[x * 4 + 1]; |
| 635 rows[y][x * 3 + 2] = src[x * 4 + 2]; |
| 636 } |
| 637 } |
| 638 png_set_filter(png, 0, PNG_NO_FILTERS); |
| 639 png_set_rows(png, info_ptr, &rows[0]); |
| 640 png_set_write_fn(png, &out, write_png_callback, NULL); |
| 641 png_write_png(png, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); |
| 642 png_destroy_write_struct(&png, NULL); |
| 643 sk_free(rows); |
| 644 sk_free(pixels); |
| 645 } |
| 646 |
609 static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* tar
get, | 647 static bool SK_WARN_UNUSED_RESULT flatten(const SkImage& image, Json::Value* tar
get, |
610 UrlDataManager& urlDataManager) { | 648 UrlDataManager& urlDataManager) { |
611 SkData* encoded = image.encode(SkImageEncoder::kPNG_Type, 100); | 649 size_t rowBytes = 4 * image.width(); |
612 if (encoded == nullptr) { | 650 SkAutoFree buffer(sk_malloc_throw(rowBytes * image.height())); |
613 // PNG encode doesn't necessarily support all color formats, convert to
a different | 651 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), |
614 // format | 652 kN32_SkColorType, kPremul_SkAlphaTyp
e); |
615 size_t rowBytes = 4 * image.width(); | 653 if (!image.readPixels(dstInfo, buffer.get(), rowBytes, 0, 0)) { |
616 void* buffer = sk_malloc_throw(rowBytes * image.height()); | 654 SkDebugf("readPixels failed\n"); |
617 SkImageInfo dstInfo = SkImageInfo::Make(image.width(), image.height(), | 655 return false; |
618 kN32_SkColorType, kPremul_SkAlph
aType); | |
619 if (!image.readPixels(dstInfo, buffer, rowBytes, 0, 0)) { | |
620 SkDebugf("readPixels failed\n"); | |
621 return false; | |
622 } | |
623 sk_sp<SkImage> converted = SkImage::MakeRasterCopy(SkPixmap(dstInfo, buf
fer, rowBytes)); | |
624 encoded = converted->encode(SkImageEncoder::kPNG_Type, 100); | |
625 if (encoded == nullptr) { | |
626 SkDebugf("image encode failed\n"); | |
627 return false; | |
628 } | |
629 sk_free(buffer); | |
630 } | 656 } |
| 657 SkDynamicMemoryWStream out; |
| 658 SkDrawCommand::WritePNG((png_bytep) buffer.get(), image.width(), image.heigh
t(), out); |
| 659 SkData* encoded = out.copyToData(); |
631 Json::Value jsonData; | 660 Json::Value jsonData; |
632 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); | 661 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); |
633 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; | 662 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; |
634 encoded->unref(); | 663 encoded->unref(); |
635 return true; | 664 return true; |
636 } | 665 } |
637 | 666 |
638 static const char* color_type_name(SkColorType colorType) { | 667 static const char* color_type_name(SkColorType colorType) { |
639 switch (colorType) { | 668 switch (colorType) { |
640 case kARGB_4444_SkColorType: | 669 case kARGB_4444_SkColorType: |
(...skipping 2462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3103 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); | 3132 result[SKDEBUGCANVAS_ATTRIBUTE_MATRIX] = MakeJsonMatrix(fMatrix); |
3104 return result; | 3133 return result; |
3105 } | 3134 } |
3106 | 3135 |
3107 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, | 3136 SkSetMatrixCommand* SkSetMatrixCommand::fromJSON(Json::Value& command, |
3108 UrlDataManager& urlDataManager)
{ | 3137 UrlDataManager& urlDataManager)
{ |
3109 SkMatrix matrix; | 3138 SkMatrix matrix; |
3110 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); | 3139 extract_json_matrix(command[SKDEBUGCANVAS_ATTRIBUTE_MATRIX], &matrix); |
3111 return new SkSetMatrixCommand(matrix); | 3140 return new SkSetMatrixCommand(matrix); |
3112 } | 3141 } |
OLD | NEW |