1 | /* |
2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2008-2009 Torch Mobile, Inc. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "ScalableImageDecoder.h" |
30 | #include <stdio.h> // Needed by jpeglib.h for FILE. |
31 | |
32 | // ICU defines TRUE and FALSE macros, breaking libjpeg v9 headers |
33 | #undef TRUE |
34 | #undef FALSE |
35 | extern "C" { |
36 | #include "jpeglib.h" |
37 | } |
38 | |
39 | namespace WebCore { |
40 | |
41 | class JPEGImageReader; |
42 | |
43 | // This class decodes the JPEG image format. |
44 | class JPEGImageDecoder final : public ScalableImageDecoder { |
45 | public: |
46 | static Ref<ScalableImageDecoder> create(AlphaOption alphaOption, GammaAndColorProfileOption gammaAndColorProfileOption) |
47 | { |
48 | return adoptRef(*new JPEGImageDecoder(alphaOption, gammaAndColorProfileOption)); |
49 | } |
50 | |
51 | virtual ~JPEGImageDecoder(); |
52 | |
53 | // ScalableImageDecoder |
54 | String filenameExtension() const override { return "jpg"_s ; } |
55 | bool setSize(const IntSize&) override; |
56 | ScalableImageDecoderFrame* frameBufferAtIndex(size_t index) override; |
57 | // CAUTION: setFailed() deletes |m_reader|. Be careful to avoid |
58 | // accessing deleted memory, especially when calling this from inside |
59 | // JPEGImageReader! |
60 | bool setFailed() override; |
61 | |
62 | bool willDownSample() |
63 | { |
64 | ASSERT(ScalableImageDecoder::encodedDataStatus() >= EncodedDataStatus::SizeAvailable); |
65 | return m_scaled; |
66 | } |
67 | |
68 | bool outputScanlines(); |
69 | void jpegComplete(); |
70 | |
71 | void setOrientation(ImageOrientation orientation) { m_orientation = orientation; } |
72 | |
73 | private: |
74 | JPEGImageDecoder(AlphaOption, GammaAndColorProfileOption); |
75 | void tryDecodeSize(bool allDataReceived) override { decode(true, allDataReceived); } |
76 | |
77 | // Decodes the image. If |onlySize| is true, stops decoding after |
78 | // calculating the image size. If decoding fails but there is no more |
79 | // data coming, sets the "decode failure" flag. |
80 | void decode(bool onlySize, bool allDataReceived); |
81 | |
82 | template <J_COLOR_SPACE colorSpace> |
83 | bool outputScanlines(ScalableImageDecoderFrame& buffer); |
84 | |
85 | template <J_COLOR_SPACE colorSpace, bool isScaled> |
86 | bool outputScanlines(ScalableImageDecoderFrame& buffer); |
87 | |
88 | std::unique_ptr<JPEGImageReader> m_reader; |
89 | }; |
90 | |
91 | } // namespace WebCore |
92 | |