Skip to content

Commit 5625a76

Browse files
Robert Matusewiczlgritz
authored andcommitted
Added support for webp format. For now plugin reads the whole content of the file before decoding. The same with encoding
1 parent 0a10062 commit 5625a76

File tree

9 files changed

+436
-0
lines changed

9 files changed

+436
-0
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ oiio_add_tests (fits
199199
IMAGEDIR fits-images
200200
URL http://www.cv.nrao.edu/fits/data/tests/)
201201

202+
oiio_add_tests (webp
203+
IMAGEDIR webp-images
204+
URL http://code.google.com/speed/webp/gallery.html)
205+
202206
#########################################################################
203207
# Packaging
204208
set (CPACK_PACKAGE_VERSION_MAJOR ${OIIO_VERSION_MAJOR})

src/cmake/externalpackages.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,25 @@ endif ()
237237
# end Field3d setup
238238
###########################################################################
239239

240+
###########################################################################
241+
# WebP setup
242+
message (STATUS "WEBP_HOME=${WEBP_HOME}")
243+
find_path (WEBP_INCLUDE_DIR webp/encode.h
244+
${THIRD_PARTY_TOOLS}/include
245+
${PROJECT_SOURCE_DIR}/include
246+
${WEBP_HOME}/)
247+
find_library (WEBP_LIBRARY
248+
NAMES webp
249+
PATHS ${THIRD_PARTY_TOOLS_HOME}/lib/
250+
${WEBP_HOME}/
251+
)
252+
if (WEBP_INCLUDE_DIR AND WEBP_LIBRARY)
253+
set (WEBP_FOUND TRUE)
254+
message (STATUS "WEBP includes = ${WEBP_INCLUDE_DIR} ")
255+
message (STATUS "WEBP library = ${WEBP_LIBRARY} ")
256+
else()
257+
set (WEBP_FOUND FALSE)
258+
message (STATUS "WebP library not found")
259+
endif()
260+
# end Webp setup
261+
###########################################################################

src/libOpenImageIO/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ if (EMBEDPLUGINS)
152152
set (FIELD3D_LIBRARY "")
153153
set (HDF5_LIBRARIES "")
154154
endif ()
155+
156+
if (WEBP_FOUND)
157+
set (libOpenImageIO_srcs ${libOpenImageIO_srcs}
158+
../webp.imageio/webpinput.cpp
159+
../webp.imageio/webpoutput.cpp
160+
)
161+
include_directories (${WEBP_INCLUDE_DIR})
162+
add_definitions ("-DUSE_WEBP=1")
163+
else ()
164+
message (STATUS "WebP plugin will not be built")
165+
set (WEBP_LIBRARY "")
166+
endif ()
155167
endif ()
156168

157169
if (BUILDSTATIC)
@@ -171,6 +183,7 @@ if (EMBEDPLUGINS)
171183
${JASPER_LIBRARY}
172184
${FIELD3D_LIBRARY}
173185
${HDF5_LIBRARIES}
186+
${WEBP_LIBRARY}
174187
)
175188
link_openexr (OpenImageIO)
176189
endif ()

src/libOpenImageIO/imageioplugin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ catalog_plugin (const std::string &format_name,
199199
PLUGENTRY (softimage);
200200
PLUGENTRY (tiff);
201201
PLUGENTRY (targa);
202+
PLUGENTRY (webp);
202203
PLUGENTRY (zfile);
203204

204205

@@ -249,6 +250,9 @@ catalog_builtin_plugins ()
249250
DECLAREPLUG (softimage);
250251
DECLAREPLUG (tiff);
251252
DECLAREPLUG (targa);
253+
#ifdef USE_WEBP
254+
DECLAREPLUG (webp);
255+
#endif
252256
DECLAREPLUG (zfile);
253257
#endif
254258
}

src/webp.imageio/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if (WEBP_FOUND)
2+
add_oiio_plugin (webpinput.cpp jwebpoutput.cpp LINK_LIBRARIES ${WEBP_LIBRARY})
3+
endif()
4+
5+

src/webp.imageio/webpinput.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)