OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2010, The Android Open Source Project | 2 * Copyright 2010, The Android Open Source Project |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); | 4 * Licensed under the Apache License, Version 2.0 (the "License"); |
5 * you may not use this file except in compliance with the License. | 5 * you may not use this file except in compliance with the License. |
6 * You may obtain a copy of the License at | 6 * You may obtain a copy of the License at |
7 * | 7 * |
8 * http://www.apache.org/licenses/LICENSE-2.0 | 8 * http://www.apache.org/licenses/LICENSE-2.0 |
9 * | 9 * |
10 * Unless required by applicable law or agreed to in writing, software | 10 * Unless required by applicable law or agreed to in writing, software |
11 * distributed under the License is distributed on an "AS IS" BASIS, | 11 * distributed under the License is distributed on an "AS IS" BASIS, |
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 * See the License for the specific language governing permissions and | 13 * See the License for the specific language governing permissions and |
14 * limitations under the License. | 14 * limitations under the License. |
15 */ | 15 */ |
16 | 16 |
17 #include "SkImageDecoder.h" | 17 #include "SkImageDecoder_libwebp.h" |
18 #include "SkImageEncoder.h" | 18 |
19 #include "SkColorPriv.h" | 19 #include "SkColorPriv.h" |
20 #include "SkScaledBitmapSampler.h" | 20 #include "SkScaledBitmapSampler.h" |
21 #include "SkStream.h" | 21 #include "SkStream.h" |
22 #include "SkTemplates.h" | 22 #include "SkTemplates.h" |
23 #include "SkUtils.h" | 23 #include "SkUtils.h" |
24 | 24 |
25 // A WebP decoder only, on top of (subset of) libwebp | 25 // A WebP decoder only, on top of (subset of) libwebp |
26 // For more information on WebP image format, and libwebp library, see: | 26 // For more information on WebP image format, and libwebp library, see: |
27 // http://code.google.com/speed/webp/ | 27 // http://code.google.com/speed/webp/ |
28 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library | 28 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 return NULL; | 582 return NULL; |
583 } | 583 } |
584 } | 584 } |
585 | 585 |
586 static int stream_writer(const uint8_t* data, size_t data_size, | 586 static int stream_writer(const uint8_t* data, size_t data_size, |
587 const WebPPicture* const picture) { | 587 const WebPPicture* const picture) { |
588 SkWStream* const stream = (SkWStream*)picture->custom_ptr; | 588 SkWStream* const stream = (SkWStream*)picture->custom_ptr; |
589 return stream->write(data, data_size) ? 1 : 0; | 589 return stream->write(data, data_size) ? 1 : 0; |
590 } | 590 } |
591 | 591 |
| 592 SkImageDecoder::Format SkDetectFormatWEBPImageDecoder(SkStreamRewindable* stream
) { |
| 593 int width, height, hasAlpha; |
| 594 if (webp_parse_header(stream, &width, &height, &hasAlpha)) { |
| 595 return SkImageDecoder::kWEBP_Format; |
| 596 } |
| 597 return SkImageDecoder::kUnknown_Format; |
| 598 } |
| 599 |
| 600 SkImageDecoder* SkCreateWEBPImageDecoder(SkStreamRewindable* stream) { |
| 601 if (SkDetectFormatWEBPImageDecoder(stream) == SkImageDecoder::kWEBP_Format)
{ |
| 602 return SkNEW(SkWEBPImageDecoder); |
| 603 } |
| 604 return NULL; |
| 605 } |
| 606 |
592 class SkWEBPImageEncoder : public SkImageEncoder { | 607 class SkWEBPImageEncoder : public SkImageEncoder { |
593 protected: | 608 protected: |
594 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK
_OVERRIDE; | 609 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK
_OVERRIDE; |
595 | 610 |
596 private: | 611 private: |
597 typedef SkImageEncoder INHERITED; | 612 typedef SkImageEncoder INHERITED; |
598 }; | 613 }; |
599 | 614 |
600 bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, | 615 bool SkWEBPImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bm, |
601 int quality) { | 616 int quality) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
646 ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride)); | 661 ok = SkToBool(WebPPictureImportRGBA(&pic, rgb, rgbStride)); |
647 } | 662 } |
648 delete[] rgb; | 663 delete[] rgb; |
649 | 664 |
650 ok = ok && WebPEncode(&webp_config, &pic); | 665 ok = ok && WebPEncode(&webp_config, &pic); |
651 WebPPictureFree(&pic); | 666 WebPPictureFree(&pic); |
652 | 667 |
653 return ok; | 668 return ok; |
654 } | 669 } |
655 | 670 |
656 | 671 SkImageEncoder* SkCreateWEBPImageEncoder(SkImageEncoder::Type t) { |
657 /////////////////////////////////////////////////////////////////////////////// | 672 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NULL; |
658 DEFINE_DECODER_CREATOR(WEBPImageDecoder); | |
659 DEFINE_ENCODER_CREATOR(WEBPImageEncoder); | |
660 /////////////////////////////////////////////////////////////////////////////// | |
661 | |
662 static SkImageDecoder* sk_libwebp_dfactory(SkStreamRewindable* stream) { | |
663 int width, height, hasAlpha; | |
664 if (!webp_parse_header(stream, &width, &height, &hasAlpha)) { | |
665 return NULL; | |
666 } | |
667 | |
668 // Magic matches, call decoder | |
669 return SkNEW(SkWEBPImageDecoder); | |
670 } | 673 } |
671 | |
672 static SkImageDecoder::Format get_format_webp(SkStreamRewindable* stream) { | |
673 int width, height, hasAlpha; | |
674 if (webp_parse_header(stream, &width, &height, &hasAlpha)) { | |
675 return SkImageDecoder::kWEBP_Format; | |
676 } | |
677 return SkImageDecoder::kUnknown_Format; | |
678 } | |
679 | |
680 static SkImageEncoder* sk_libwebp_efactory(SkImageEncoder::Type t) { | |
681 return (SkImageEncoder::kWEBP_Type == t) ? SkNEW(SkWEBPImageEncoder) : NUL
L; | |
682 } | |
683 | |
684 static SkImageDecoder_DecodeReg gDReg(sk_libwebp_dfactory); | |
685 static SkImageDecoder_FormatReg gFormatReg(get_format_webp); | |
686 static SkImageEncoder_EncodeReg gEReg(sk_libwebp_efactory); | |
OLD | NEW |