-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert: Use libpng for image output.
- Loading branch information
Showing
12 changed files
with
217 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright 2020-2020 the openage authors. See copying.md for legal info. | ||
|
||
# - Find libpng library | ||
# Find the native libpng headers and library. | ||
# This module defines | ||
# LIBPNG_INCLUDE_DIRS - where to find ogg/ogg.h etc. | ||
# LIBPNG_LIBRARIES - List of libraries when using libogg | ||
# LIBPNG_FOUND - True if ogg is found. | ||
|
||
find_path(LIBPNG_INCLUDE_DIR | ||
NAMES libpng/png.h | ||
DOC "libpng include directory" | ||
) | ||
|
||
find_library(LIBPNG_LIBRARY | ||
NAMES png | ||
DOC "Path to libpng library" | ||
) | ||
|
||
# handle the QUIETLY and REQUIRED arguments and set LIBPNG_FOUND to TRUE if | ||
# all listed variables are TRUE | ||
include(FindPackageHandleStandardArgs) | ||
find_package_handle_standard_args(Libpng DEFAULT_MSG LIBPNG_INCLUDE_DIR LIBPNG_LIBRARY) | ||
|
||
mark_as_advanced(LIBPNG_INCLUDE_DIR LIBPNG_LIBRARY) | ||
|
||
# export the variables | ||
set(LIBPNG_INCLUDE_DIRS "${LIBPNG_INCLUDE_DIR}") | ||
set(LIBPNG_LIBRARIES "${LIBPNG_LIBRARY}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
find_package(Libpng REQUIRED) | ||
|
||
# Currently there is no way to link cython modules to extra libraries. | ||
# Since PYEXT_LINK_LIBRARY normally only includes libopenage (what | ||
# opusenc doesn't need), we hijack this variable. This is ok, because | ||
# there are no subdirectories, that will see the changed variable. | ||
set(PYEXT_LINK_LIBRARY | ||
${LIBPNG_LIBRARIES} | ||
) | ||
|
||
set(PYEXT_INCLUDE_DIRS | ||
${PYEXT_INCLUDE_DIRS} | ||
${LIBPNG_INCLUDE_DIRS} | ||
) | ||
|
||
add_cython_modules( | ||
png_create.pyx | ||
) | ||
|
||
add_pxds( | ||
__init__.pxd | ||
libpng.pxd | ||
) | ||
|
||
add_py_modules( | ||
__init__.py | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright 2020-2020 the openage authors. See copying.md for legal info. | ||
|
||
""" | ||
Cython module to create png files using libpng. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2020-2020 the openage authors. See copying.md for legal info. | ||
|
||
from libc.stdio cimport FILE | ||
|
||
cdef extern from "libpng/png.h": | ||
const char PNG_LIBPNG_VER_STRING[] | ||
const int PNG_COLOR_TYPE_RGBA = 6 | ||
const int PNG_INTERLACE_NONE = 0 | ||
const int PNG_COMPRESSION_TYPE_DEFAULT = 0 | ||
const int PNG_FILTER_TYPE_DEFAULT = 0 | ||
const int PNG_TRANSFORM_IDENTITY = 0 | ||
|
||
ctypedef unsigned char png_byte | ||
ctypedef const png_byte *png_const_bytep | ||
ctypedef png_byte **png_bytepp | ||
ctypedef unsigned long int png_uint_32 | ||
|
||
ctypedef struct png_struct | ||
ctypedef png_struct *png_structp | ||
ctypedef png_struct *png_structrp | ||
ctypedef png_struct **png_structpp | ||
ctypedef const png_struct *png_const_structrp | ||
|
||
ctypedef struct png_info | ||
ctypedef png_info *png_infop | ||
ctypedef png_info *png_inforp | ||
ctypedef png_info **png_infopp | ||
ctypedef const png_info *png_const_inforp | ||
|
||
ctypedef const char *png_const_charp | ||
ctypedef void *png_voidp | ||
ctypedef (png_structp, png_const_charp) *png_error_ptr | ||
ctypedef FILE *png_FILE_p | ||
|
||
png_structp png_create_write_struct(png_const_charp user_png_ver, | ||
png_voidp error_ptr, | ||
png_error_ptr error_fn, | ||
png_error_ptr warn_fn) | ||
|
||
png_infop png_create_info_struct(png_const_structrp png_ptr) | ||
void png_set_IHDR(png_const_structrp png_ptr, | ||
png_inforp info_ptr, | ||
png_uint_32 width, | ||
png_uint_32 height, | ||
int bit_depth, | ||
int color_type, | ||
int interlace_method, | ||
int compression_method, | ||
int filter_method) | ||
void png_init_io(png_structrp png_ptr, | ||
png_FILE_p fp) | ||
void png_set_rows(png_const_structrp png_ptr, | ||
png_inforp info_ptr, | ||
png_bytepp row_pointers) | ||
void png_write_png(png_structrp png_ptr, | ||
png_inforp info_ptr, | ||
int transforms, | ||
png_voidp params) | ||
void png_destroy_write_struct(png_structpp png_ptr_ptr, | ||
png_infopp info_ptr_ptr) | ||
|
||
# Should not be necessary if png_write_png() works | ||
void png_write_info(png_structrp png_ptr, | ||
png_const_inforp info_ptr) | ||
void png_write_row(png_structrp png_ptr, | ||
png_const_bytep row) | ||
void png_write_end(png_structrp png_ptr, | ||
png_inforp info_ptr) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2020-2020 the openage authors. See copying.md for legal info. | ||
# | ||
# cython: profile=False | ||
|
||
from libc.stdio cimport fopen, fclose | ||
from libc.stdint cimport uint8_t | ||
from libc.stdlib cimport realloc | ||
from cpython.mem cimport PyMem_Malloc | ||
|
||
from . cimport libpng | ||
|
||
cimport cython | ||
import numpy | ||
cimport numpy | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def save(filename, numpy.ndarray[numpy.uint8_t, ndim=3, mode="c"] imagedata not None): | ||
""" | ||
Save an image as a PNG file. | ||
""" | ||
cdef unsigned int width = imagedata.shape[1] | ||
cdef unsigned int height = imagedata.shape[0] | ||
cdef numpy.uint8_t[:,:,::1] mview = imagedata | ||
png_create(filename.encode('UTF-8'), mview, width, height) | ||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
cdef void png_create(char* filename, numpy.uint8_t[:,:,::1] imagedata, | ||
int width, int height): | ||
""" | ||
Create a PNG file with libpng and write it to file. | ||
""" | ||
cdef libpng.png_FILE_p fp = fopen(filename, "wb") | ||
|
||
cdef libpng.png_structp png | ||
cdef libpng.png_infop info | ||
|
||
png = libpng.png_create_write_struct(libpng.PNG_LIBPNG_VER_STRING, NULL, NULL, NULL) | ||
info = libpng.png_create_info_struct(png) | ||
|
||
libpng.png_init_io(png, fp) | ||
libpng.png_set_IHDR(png, | ||
info, | ||
width, | ||
height, | ||
8, | ||
libpng.PNG_COLOR_TYPE_RGBA, | ||
libpng.PNG_INTERLACE_NONE, | ||
libpng.PNG_COMPRESSION_TYPE_DEFAULT, | ||
libpng.PNG_FILTER_TYPE_DEFAULT) | ||
libpng.png_write_info(png, info) | ||
|
||
for row_idx in range(height): | ||
libpng.png_write_row(png, &imagedata[row_idx,0,0]) | ||
|
||
libpng.png_write_end(png, info) | ||
|
||
# TODO: This doesn't work, but would be faster | ||
# libpng.png_set_rows(png, info, imagedata) | ||
# libpng.png_write_png(png, info, libpng.PNG_TRANSFORM_IDENTITY, NULL) | ||
|
||
fclose(fp) | ||
|
||
libpng.png_destroy_write_struct(&png, &info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters