|
| 1 | +/* |
| 2 | + Copyright 2011 Larry Gritz and the other authors and contributors. |
| 3 | + All Rights Reserved. |
| 4 | +
|
| 5 | + Redistribution and use in source and binary forms, with or without |
| 6 | + modification, are permitted provided that the following conditions are |
| 7 | + met: |
| 8 | + * Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | + * 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 | + * Neither the name of the software's owners nor the names of its |
| 14 | + contributors may be used to endorse or promote products derived from |
| 15 | + this software without specific prior written permission. |
| 16 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +
|
| 28 | + (This is the Modified BSD License) |
| 29 | +*/ |
| 30 | +#include <cstdio> |
| 31 | +#include <webp/decode.h> |
| 32 | +#include "imageio.h" |
| 33 | +#include "fmath.h" |
| 34 | + |
| 35 | +OIIO_PLUGIN_NAMESPACE_BEGIN |
| 36 | + |
| 37 | +namespace webp_pvt { |
| 38 | + |
| 39 | + |
| 40 | +class WebpInput : public ImageInput |
| 41 | +{ |
| 42 | + public: |
| 43 | + WebpInput() { init(); } |
| 44 | + virtual ~WebpInput() { close(); } |
| 45 | + virtual const char* format_name() const { return "webp"; } |
| 46 | + virtual bool open (const std::string &name, ImageSpec &spec); |
| 47 | + virtual bool read_native_scanline (int y, int z, void *data); |
| 48 | + virtual bool close (); |
| 49 | + |
| 50 | + private: |
| 51 | + std::string m_filename; |
| 52 | + uint8_t *m_decoded_image; |
| 53 | + long int m_image_size; |
| 54 | + long int m_scanline_size; |
| 55 | + FILE *m_file; |
| 56 | + |
| 57 | + void init() |
| 58 | + { |
| 59 | + m_image_size = m_scanline_size = 0; |
| 60 | + m_decoded_image = NULL; |
| 61 | + m_file = NULL; |
| 62 | + } |
| 63 | +}; |
| 64 | + |
| 65 | + |
| 66 | +bool |
| 67 | +WebpInput::open (const std::string &name, ImageSpec &spec) |
| 68 | +{ |
| 69 | + m_filename = name; |
| 70 | + |
| 71 | + m_file = fopen(m_filename.c_str(), "rb"); |
| 72 | + if (!m_file) |
| 73 | + { |
| 74 | + error ("Could not open file \"%s\"", m_filename.c_str()); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + fseek (m_file, 0, SEEK_END); |
| 79 | + m_image_size = ftell(m_file); |
| 80 | + fseek (m_file, 0, SEEK_SET); |
| 81 | + |
| 82 | + std::vector<uint8_t> encoded_image; |
| 83 | + encoded_image.resize(m_image_size, 0); |
| 84 | + fread(&encoded_image[0], sizeof(uint8_t), encoded_image.size(), m_file); |
| 85 | + |
| 86 | + int width = 0, height = 0; |
| 87 | + if(!WebPGetInfo(&encoded_image[0], encoded_image.size(), &width, &height)) |
| 88 | + { |
| 89 | + error ("%s is not a WebP image file", m_filename.c_str()); |
| 90 | + close(); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + const int CHANNEL_NUM = 4; |
| 95 | + m_scanline_size = width * CHANNEL_NUM; |
| 96 | + m_spec = ImageSpec(width, height, CHANNEL_NUM, TypeDesc::UINT8); |
| 97 | + spec = m_spec; |
| 98 | + |
| 99 | + if (!(m_decoded_image = WebPDecodeRGBA(&encoded_image[0], encoded_image.size(), &m_spec.width, &m_spec.height))) |
| 100 | + { |
| 101 | + error ("Couldn't decode %s", m_filename.c_str()); |
| 102 | + close(); |
| 103 | + return false; |
| 104 | + } |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | + |
| 109 | +bool |
| 110 | +WebpInput::read_native_scanline (int y, int z, void *data) |
| 111 | +{ |
| 112 | + if (y < 0 || y >= m_spec.width) // out of range scanline |
| 113 | + return false; |
| 114 | + memcpy(data, &m_decoded_image[y*m_scanline_size], m_scanline_size); |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +bool |
| 120 | +WebpInput::close() |
| 121 | +{ |
| 122 | + if (m_file) |
| 123 | + { |
| 124 | + fclose(m_file); |
| 125 | + m_file = NULL; |
| 126 | + } |
| 127 | + if (m_decoded_image) |
| 128 | + { |
| 129 | + free(m_decoded_image); // this was allocated by WebPDecodeRGB and should be fread by free |
| 130 | + m_decoded_image = NULL; |
| 131 | + } |
| 132 | + return true; |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace webp_pvt |
| 136 | + |
| 137 | +// Obligatory material to make this a recognizeable imageio plugin |
| 138 | +OIIO_PLUGIN_EXPORTS_BEGIN |
| 139 | + |
| 140 | + DLLEXPORT int webp_imageio_version = OIIO_PLUGIN_VERSION; |
| 141 | + DLLEXPORT ImageInput *webp_input_imageio_create () { |
| 142 | + return new webp_pvt::WebpInput; |
| 143 | + } |
| 144 | + DLLEXPORT const char *webp_input_extensions[] = { |
| 145 | + "webp", NULL |
| 146 | + }; |
| 147 | + |
| 148 | +OIIO_PLUGIN_EXPORTS_END |
| 149 | + |
| 150 | +OIIO_PLUGIN_NAMESPACE_END |
0 commit comments