Skip to content

Commit

Permalink
Merge pull request #8 from tanabi/main
Browse files Browse the repository at this point in the history
#7 Added webp load/save support
  • Loading branch information
mdadams committed May 27, 2023
2 parents 6ae9e10 + 2378dad commit 7e21eeb
Show file tree
Hide file tree
Showing 8 changed files with 612 additions and 5 deletions.
12 changes: 10 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cmake_minimum_required(VERSION 3.12)
# The major, minor, and micro version numbers of the project.
set(XV_VERSION_MAJOR 4)
set(XV_VERSION_MINOR 0)
set(XV_VERSION_PATCH 0)
set(XV_VERSION_DATE "20220216")
set(XV_VERSION_PATCH 1)
set(XV_VERSION_DATE "20230429")

# Generate the project version.
set(XV_VERSION "${XV_VERSION_MAJOR}.${XV_VERSION_MINOR}.${XV_VERSION_PATCH}")
Expand All @@ -27,6 +27,7 @@ option(XV_ENABLE_JP2K "Enable JP2K Support" ON)
option(XV_ENABLE_PDS "Enable PDF Support" ON)
option(XV_ENABLE_PNG "Enable PNG Support" ON)
option(XV_ENABLE_TIFF "Enable TIFF Support" ON)
option(XV_ENABLE_WEBP "Enable WEBP Support" ON)

option(XV_STRICT "Treat compiler warnings as errors" OFF)

Expand Down Expand Up @@ -191,10 +192,17 @@ if(XV_ENABLE_TIFF AND NOT TARGET TIFF::TIFF)
set(XV_ENABLE_TIFF OFF)
endif()

find_package(WebP)
if(XV_ENABLE_WEBP AND NOT TARGET WebP::libwebp)
message(WARNING "Disabling WEBP Support.")
set(XV_ENABLE_WEBP OFF)
endif()

message("JP2K: ${XV_ENABLE_JP2K}")
message("JPEG: ${XV_ENABLE_JPEG}")
message("TIFF: ${XV_ENABLE_TIFF}")
message("PNG: ${XV_ENABLE_PNG}")
message("WEBP: ${XV_ENABLE_WEBP}")

################################################################################
# Subdirectories.
Expand Down
174 changes: 174 additions & 0 deletions build/cmake/modules/FindWebP.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Copyright (C) 2020 Sony Interactive Entertainment Inc.
# Copyright (C) 2012 Raphael Kubo da Costa <rakuco@webkit.org>
# Copyright (C) 2013 Igalia S.L.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND ITS CONTRIBUTORS ``AS
# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR ITS
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#
# This is borrowed from:
#
# https://github.com/WebKit/WebKit/blob/main/Source/cmake/FindWebP.cmake
#
# Copyright notices kept intact above.
#

#[=======================================================================[.rst:
FindWebP
--------------

Find WebP headers and libraries.

Imported Targets
^^^^^^^^^^^^^^^^

``WebP::libwebp``
The WebP library, if found.

``WebP::demux``
The WebP demux library, if found.

Result Variables
^^^^^^^^^^^^^^^^

This will define the following variables in your project:

``WebP_FOUND``
true if (the requested version of) WebP is available.
``WebP_VERSION``
the version of WebP.
``WebP_LIBRARIES``
the libraries to link against to use WebP.
``WebP_INCLUDE_DIRS``
where to find the WebP headers.
``WebP_COMPILE_OPTIONS``
this should be passed to target_compile_options(), if the
target is not used for linking

#]=======================================================================]

find_package(PkgConfig QUIET)
pkg_check_modules(PC_WEBP QUIET libwebp)
set(WebP_COMPILE_OPTIONS ${PC_WEBP_CFLAGS_OTHER})
set(WebP_VERSION ${PC_WEBP_CFLAGS_VERSION})

find_path(WebP_INCLUDE_DIR
NAMES webp/decode.h
HINTS ${PC_WEBP_INCLUDEDIR} ${PC_WEBP_INCLUDE_DIRS}
)

find_library(WebP_LIBRARY
NAMES ${WebP_NAMES} webp libwebp
HINTS ${PC_WEBP_LIBDIR} ${PC_WEBP_LIBRARY_DIRS}
)

# There's nothing in the WebP headers that could be used to detect the exact
# WebP version being used so don't attempt to do so. A version can only be found
# through pkg-config
if ("${WebP_FIND_VERSION}" VERSION_GREATER "${WebP_VERSION}")
if (WebP_VERSION)
message(FATAL_ERROR "Required version (" ${WebP_FIND_VERSION} ") is higher than found version (" ${WebP_VERSION} ")")
else ()
message(WARNING "Cannot determine WebP version without pkg-config")
endif ()
endif ()

# Find components
if (WebP_INCLUDE_DIR AND WebP_LIBRARY)
set(_WebP_REQUIRED_LIBS_FOUND ON)
set(WebP_LIBS_FOUND "WebP (required): ${WebP_LIBRARY}")
else ()
set(_WebP_REQUIRED_LIBS_FOUND OFF)
set(WebP_LIBS_NOT_FOUND "WebP (required)")
endif ()

if ("demux" IN_LIST WebP_FIND_COMPONENTS)
find_library(WebP_DEMUX_LIBRARY
NAMES ${WebP_DEMUX_NAMES} webpdemux libwebpdemux
HINTS ${PC_WEBP_LIBDIR} ${PC_WEBP_LIBRARY_DIRS}
)

if (WebP_DEMUX_LIBRARY)
if (WebP_FIND_REQUIRED_demux)
list(APPEND WebP_LIBS_FOUND "demux (required): ${WebP_DEMUX_LIBRARY}")
else ()
list(APPEND WebP_LIBS_FOUND "demux (optional): ${WebP_DEMUX_LIBRARY}")
endif ()
else ()
if (WebP_FIND_REQUIRED_demux)
set(_WebP_REQUIRED_LIBS_FOUND OFF)
list(APPEND WebP_LIBS_NOT_FOUND "demux (required)")
else ()
list(APPEND WebP_LIBS_NOT_FOUND "demux (optional)")
endif ()
endif ()
endif ()

if (NOT WebP_FIND_QUIETLY)
if (WebP_LIBS_FOUND)
message(STATUS "Found the following WebP libraries:")
foreach (found ${WebP_LIBS_FOUND})
message(STATUS " ${found}")
endforeach ()
endif ()
if (WebP_LIBS_NOT_FOUND)
message(STATUS "The following WebP libraries were not found:")
foreach (found ${WebP_LIBS_NOT_FOUND})
message(STATUS " ${found}")
endforeach ()
endif ()
endif ()

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(WebP
FOUND_VAR WebP_FOUND
REQUIRED_VARS WebP_INCLUDE_DIR WebP_LIBRARY _WebP_REQUIRED_LIBS_FOUND
VERSION_VAR WebP_VERSION
)

if (WebP_LIBRARY AND NOT TARGET WebP::libwebp)
add_library(WebP::libwebp UNKNOWN IMPORTED GLOBAL)
set_target_properties(WebP::libwebp PROPERTIES
IMPORTED_LOCATION "${WebP_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${WebP_COMPILE_OPTIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${WebP_INCLUDE_DIR}"
)
endif ()

if (WebP_DEMUX_LIBRARY AND NOT TARGET WebP::demux)
add_library(WebP::demux UNKNOWN IMPORTED GLOBAL)
set_target_properties(WebP::demux PROPERTIES
IMPORTED_LOCATION "${WebP_DEMUX_LIBRARY}"
INTERFACE_COMPILE_OPTIONS "${WebP_COMPILE_OPTIONS}"
INTERFACE_INCLUDE_DIRECTORIES "${WebP_INCLUDE_DIR}"
)
endif ()

mark_as_advanced(
WebP_INCLUDE_DIR
WebP_LIBRARY
WebP_DEMUX_LIBRARY
)

if (WebP_FOUND)
set(WebP_LIBRARIES ${WebP_LIBRARY} ${WebP_DEMUX_LIBRARY})
set(WebP_INCLUDE_DIRS ${WebP_INCLUDE_DIR})
endif ()
Binary file added data/images/newtonian_fractal_flipped.webp
Binary file not shown.
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ if(XV_ENABLE_PNG)
set(xv_libs ${xv_libs} PNG::PNG)
endif()

if(XV_ENABLE_WEBP)
add_compile_definitions(DOWEBP)
set(xv_libs ${xv_libs} WebP::libwebp)
endif()

if(XV_ENABLE_JPEG)
add_compile_definitions(DOJPEG)
set(xv_libs ${xv_libs} JPEG::JPEG)
Expand Down Expand Up @@ -95,6 +100,7 @@ set(xv_sources
xvtiffwr.c
xvvd.c
xvwbmp.c
xvwebp.c
xvxbm.c
xvxpm.c
xvxwd.c
Expand Down
15 changes: 15 additions & 0 deletions src/xv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,10 @@ static void cmdSyntax()
#ifdef HAVE_PNG
VersionInfoPNG();
#endif
#ifdef HAVE_WEBP
VersionInfoWEBP();
#endif

/* pbm/pgm/ppm support is native, not via pbmplus/netpbm libraries */
fprintf(stderr, "\n");

Expand Down Expand Up @@ -3149,6 +3153,13 @@ int ReadFileType(fname)
magicno[2]=='N' && magicno[3]=='G') rv = RFT_PNG;
#endif

#ifdef HAVE_WEBP
else if (magicno[0]=='R' && magicno[1]=='I' &&
magicno[2]=='F' && magicno[3]=='F' &&
magicno[8]=='W' && magicno[9]=='E' &&
magicno[10]=='B' && magicno[11]=='P') rv = RFT_WEBP;
#endif

#ifdef HAVE_PDS
else if (strncmp((char *) magicno, "NJPL1I00", (size_t) 8)==0 ||
strncmp((char *) magicno+2,"NJPL1I", (size_t) 6)==0 ||
Expand Down Expand Up @@ -3286,6 +3297,10 @@ int ReadPicFile(fname, ftype, pinfo, quick)
case RFT_PNG: rv = LoadPNG (fname, pinfo); break;
#endif

#ifdef HAVE_WEBP
case RFT_WEBP: rv = LoadWebP (fname, pinfo); break;
#endif

#ifdef HAVE_PDS
case RFT_PDSVICAR: rv = LoadPDS (fname, pinfo); break;
#endif
Expand Down
24 changes: 21 additions & 3 deletions src/xv.h
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@
# define HAVE_G3
#endif

#ifdef DOWEBP
# define HAVE_WEBP
#endif

#define PROGNAME "xv" /* used in resource database */

Expand Down Expand Up @@ -640,6 +643,12 @@
# define F_MGCSFXINC 0
#endif

#ifdef HAVE_WEBP
# define F_WEBPINC 1
#else
# define F_WEBPINC 0
#endif

#ifdef MACBINARY
# define MACBSIZE 128
#endif
Expand All @@ -665,7 +674,8 @@
#define F_PM (11 + F_TIFF)
#define F_ZX (12 + F_TIFF) /* [JCE] */
#define F_WBMP (13 + F_TIFF)
#define JP_EXT_F (F_WBMP)
#define F_WEBP (F_WEBPINC + F_WBMP)
#define JP_EXT_F (F_WEBP)
#define F_MAG (JP_EXT_F + F_MAGINC)
#define F_PIC (JP_EXT_F + F_MAGINC + F_PICINC)
#define F_MAKI (JP_EXT_F + F_MAGINC + F_PICINC + F_MAKINC)
Expand Down Expand Up @@ -714,7 +724,8 @@
#define RFT_JPC 26
#define RFT_JP2 27
#define RFT_G3 28
#define JP_EXT_RFT (RFT_G3)
#define RFT_WEBP 29
#define JP_EXT_RFT (RFT_WEBP)
#define RFT_MAG (JP_EXT_RFT + 1)
#define RFT_MAKI (JP_EXT_RFT + 2)
#define RFT_PIC (JP_EXT_RFT + 3)
Expand Down Expand Up @@ -1457,7 +1468,6 @@ WHERE Window pngW;
WHERE int pngUp; /* is pngW mapped, or what? */
#endif


#ifdef ENABLE_FIXPIX_SMOOTH
WHERE int do_fixpix_smooth; /* GRR 19980607: runtime FS dithering */
#endif
Expand Down Expand Up @@ -2178,6 +2188,14 @@ int LoadWBMP PARM((char *, PICINFO *));
int WriteWBMP PARM((FILE *, byte *, int, int, int, byte *,
byte *, byte *, int, int));

/**************************** XVWEBP.C ***************************/
int LoadWEBP PARM((char *, PICINFO *));
int WriteWEBP PARM((FILE *, byte *, int, int, int, byte *,
byte *, byte *, int, int));

void VersionInfoWEBP PARM((void));


/**************************** XVXBM.C ***************************/
int LoadXBM PARM((char *, PICINFO *));
int WriteXBM PARM((FILE *, byte *, int, int, byte *, byte *,
Expand Down
17 changes: 17 additions & 0 deletions src/xvdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ static const char *saveFormats[] = {
"PM",
"Spectrum SCREEN$", /* [JCE] */
"WBMP",
#ifdef HAVE_WEBP
"WEBP",
#endif
#ifdef HAVE_MAG
"MAG",
#endif
Expand Down Expand Up @@ -1277,6 +1280,13 @@ int DoSave()
rv = 0;

switch (fmt) {

#ifdef HAVE_WEBP
case F_WEBP:
rv = WriteWEBP (fp, thepic, ptype, w, h, rp,gp,bp, nc,col);
break;
#endif

case F_GIF:
rv = WriteGIF (fp, thepic, ptype, w, h, rp,gp,bp, nc,col,picComments);
break;
Expand Down Expand Up @@ -1585,6 +1595,9 @@ static void changeSuffix()
#endif
#ifdef HAVE_PNG
(strcmp(lowsuf,"png" )==0) ||
#endif
#ifdef HAVE_WEBP
(strcmp(lowsuf,"webp" )==0) ||
#endif
(strcmp(lowsuf,"wbmp")==0) ||
(strcmp(lowsuf,"xpm" )==0) ||
Expand Down Expand Up @@ -1641,6 +1654,10 @@ static void changeSuffix()
case F_PNG: strcpy(lowsuf,"png"); break;
#endif

#ifdef HAVE_WEBP
case F_WEBP: strcpy(lowsuf,"webp"); break;
#endif

#ifdef HAVE_MAG
case F_MAG: strcpy(lowsuf,"mag"); break;
#endif
Expand Down

0 comments on commit 7e21eeb

Please sign in to comment.