OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 "SkDrawCommand.h" | 8 #include "SkDrawCommand.h" |
9 | 9 |
| 10 #include "png.h" |
| 11 |
10 #include "SkBlurMaskFilter.h" | 12 #include "SkBlurMaskFilter.h" |
11 #include "SkColorFilter.h" | 13 #include "SkColorFilter.h" |
12 #include "SkDashPathEffect.h" | 14 #include "SkDashPathEffect.h" |
13 #include "SkImageFilter.h" | 15 #include "SkImageFilter.h" |
14 #include "SkJsonWriteBuffer.h" | 16 #include "SkJsonWriteBuffer.h" |
15 #include "SkMaskFilter.h" | 17 #include "SkMaskFilter.h" |
16 #include "SkObjectParser.h" | 18 #include "SkObjectParser.h" |
17 #include "SkPaintDefaults.h" | 19 #include "SkPaintDefaults.h" |
18 #include "SkPathEffect.h" | 20 #include "SkPathEffect.h" |
19 #include "SkPicture.h" | 21 #include "SkPicture.h" |
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 | 656 |
655 (*target) = jsonFlattenable; | 657 (*target) = jsonFlattenable; |
656 sk_free(data); | 658 sk_free(data); |
657 } | 659 } |
658 | 660 |
659 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { | 661 static void write_png_callback(png_structp png_ptr, png_bytep data, png_size_t l
ength) { |
660 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); | 662 SkWStream* out = (SkWStream*) png_get_io_ptr(png_ptr); |
661 out->write(data, length); | 663 out->write(data, length); |
662 } | 664 } |
663 | 665 |
664 void SkDrawCommand::WritePNG(const png_bytep rgba, png_uint_32 width, png_uint_3
2 height, | 666 void SkDrawCommand::WritePNG(const uint8_t* rgba, unsigned width, unsigned heigh
t, |
665 SkWStream& out, bool isOpaque) { | 667 SkWStream& out, bool isOpaque) { |
666 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL); | 668 png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
NULL); |
667 SkASSERT(png != nullptr); | 669 SkASSERT(png != nullptr); |
668 png_infop info_ptr = png_create_info_struct(png); | 670 png_infop info_ptr = png_create_info_struct(png); |
669 SkASSERT(info_ptr != nullptr); | 671 SkASSERT(info_ptr != nullptr); |
670 if (setjmp(png_jmpbuf(png))) { | 672 if (setjmp(png_jmpbuf(png))) { |
671 SkFAIL("png encode error"); | 673 SkFAIL("png encode error"); |
672 } | 674 } |
673 png_set_write_fn(png, &out, write_png_callback, NULL); | 675 png_set_write_fn(png, &out, write_png_callback, NULL); |
674 int colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA; | 676 int colorType = isOpaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGBA; |
675 png_set_IHDR(png, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE, | 677 png_set_IHDR(png, info_ptr, width, height, 8, colorType, PNG_INTERLACE_NONE, |
676 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); | 678 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); |
677 png_set_compression_level(png, 1); | 679 png_set_compression_level(png, 1); |
678 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); | 680 png_bytepp rows = (png_bytepp) sk_malloc_throw(height * sizeof(png_byte*)); |
679 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 4); | 681 png_bytep pixels = (png_bytep) sk_malloc_throw(width * height * 4); |
680 for (png_size_t y = 0; y < height; ++y) { | 682 for (png_size_t y = 0; y < height; ++y) { |
681 const png_bytep src = rgba + y * width * 4; | 683 const uint8_t* src = rgba + y * width * 4; |
682 rows[y] = pixels + y * width * 4; | 684 rows[y] = pixels + y * width * 4; |
683 for (png_size_t x = 0; x < width; ++x) { | 685 for (png_size_t x = 0; x < width; ++x) { |
684 rows[y][x * 4] = src[x * 4]; | 686 rows[y][x * 4] = src[x * 4]; |
685 rows[y][x * 4 + 1] = src[x * 4 + 1]; | 687 rows[y][x * 4 + 1] = src[x * 4 + 1]; |
686 rows[y][x * 4 + 2] = src[x * 4 + 2]; | 688 rows[y][x * 4 + 2] = src[x * 4 + 2]; |
687 rows[y][x * 4 + 3] = src[x * 4 + 3]; | 689 rows[y][x * 4 + 3] = src[x * 4 + 3]; |
688 } | 690 } |
689 } | 691 } |
690 png_write_info(png, info_ptr); | 692 png_write_info(png, info_ptr); |
691 if (isOpaque) { | 693 if (isOpaque) { |
(...skipping 15 matching lines...) Expand all Loading... |
707 if (!image.readPixels(dstInfo, buffer.get(), rowBytes, 0, 0)) { | 709 if (!image.readPixels(dstInfo, buffer.get(), rowBytes, 0, 0)) { |
708 SkDebugf("readPixels failed\n"); | 710 SkDebugf("readPixels failed\n"); |
709 return false; | 711 return false; |
710 } | 712 } |
711 | 713 |
712 SkBitmap bm; | 714 SkBitmap bm; |
713 bm.installPixels(dstInfo, buffer.get(), rowBytes); | 715 bm.installPixels(dstInfo, buffer.get(), rowBytes); |
714 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bm); | 716 sk_sp<SkData> encodedBitmap = sk_tools::encode_bitmap_for_png(bm); |
715 | 717 |
716 SkDynamicMemoryWStream out; | 718 SkDynamicMemoryWStream out; |
717 SkDrawCommand::WritePNG((const png_bytep) encodedBitmap->bytes(), image.widt
h(), image.height(), | 719 SkDrawCommand::WritePNG(encodedBitmap->bytes(), image.width(), image.height(
), |
718 out, false); | 720 out, false); |
719 sk_sp<SkData> encoded = out.detachAsData(); | 721 sk_sp<SkData> encoded = out.detachAsData(); |
720 Json::Value jsonData; | 722 Json::Value jsonData; |
721 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); | 723 encode_data(encoded->data(), encoded->size(), "image/png", urlDataManager, &
jsonData); |
722 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; | 724 (*target)[SKDEBUGCANVAS_ATTRIBUTE_DATA] = jsonData; |
723 return true; | 725 return true; |
724 } | 726 } |
725 | 727 |
726 static const char* color_type_name(SkColorType colorType) { | 728 static const char* color_type_name(SkColorType colorType) { |
727 switch (colorType) { | 729 switch (colorType) { |
(...skipping 2805 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3533 SkTranslateZCommand* SkTranslateZCommand::fromJSON(Json::Value& command, | 3535 SkTranslateZCommand* SkTranslateZCommand::fromJSON(Json::Value& command, |
3534 UrlDataManager& urlDataManager) { | 3536 UrlDataManager& urlDataManager) { |
3535 SkScalar z; | 3537 SkScalar z; |
3536 #ifdef SK_EXPERIMENTAL_SHADOWING | 3538 #ifdef SK_EXPERIMENTAL_SHADOWING |
3537 extract_json_scalar(command[SKDEBUGCANVAS_ATTRIBUTE_DRAWDEPTHTRANS], &z); | 3539 extract_json_scalar(command[SKDEBUGCANVAS_ATTRIBUTE_DRAWDEPTHTRANS], &z); |
3538 #else | 3540 #else |
3539 z = 0; | 3541 z = 0; |
3540 #endif | 3542 #endif |
3541 return new SkTranslateZCommand(z); | 3543 return new SkTranslateZCommand(z); |
3542 } | 3544 } |
OLD | NEW |