diff --git a/ext/common/psx_list.h b/ext/common/psx_list.h new file mode 100644 index 0000000..f22568a --- /dev/null +++ b/ext/common/psx_list.h @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024, Zhang Ji Peng + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * * 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 HOLDERS AND 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 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. + */ + +#ifndef _PSX_LIST_H_ +#define _PSX_LIST_H_ + +#if defined(__GNUC__) + #define INLINE inline +#elif defined(_MSC_VER) + #define INLINE __inline +#else + #define INLINE +#endif + +struct list_hdr { + struct list_hdr* next; + struct list_hdr* prev; +}; + +/* all function return 0 is success or true, -1 is fail or false. */ + +static INLINE void list_init(struct list_hdr* head) +{ + head->next = head; + head->prev = head; +} + +static INLINE int list_empty(const struct list_hdr* head) +{ + return head->next == head ? 0 : -1; +} + +#define list_add(head, value) \ + list_add_entry((struct list_hdr*)(head), (struct list_hdr*)(value)) + +static INLINE void list_add_entry(struct list_hdr* head, struct list_hdr* value) +{ + register struct list_hdr* next = head->next; + + next->prev = value; + value->next = next; + value->prev = head; + head->next = value; +} + +#define list_add_tail(head, value) \ + list_add_tail_entry((struct list_hdr*)(head), (struct list_hdr*)(value)) + +static INLINE void list_add_tail_entry(struct list_hdr* head, struct list_hdr* value) +{ + register struct list_hdr* prev = head->prev; + + head->prev = value; + value->next = head; + value->prev = prev; + prev->next = value; +} + +#define list_remove(entry) \ + list_remove_entry((struct list_hdr*)(entry)) + +static INLINE void list_remove_entry(struct list_hdr* entry) +{ + entry->next->prev = entry->prev; + entry->prev->next = entry->next; + entry->next = entry->prev = NULL; +} + +#define list_for_each(head, iterator) \ + for ((iterator) = (head)->next; (iterator) != (head); (iterator) = (iterator)->next) + +#define list_for_each_start_with(head, start, iterator) \ + for ((iterator) = ((struct list_hdr*)(start))->next; (iterator) != (head); (iterator) = (iterator)->next) + +#endif /*_PSX_LIST_H_*/ diff --git a/ext/image_coders/gif/gif_module.c b/ext/image_coders/gif/gif_module.c index 3e35716..ac20ea2 100644 --- a/ext/image_coders/gif/gif_module.c +++ b/ext/image_coders/gif/gif_module.c @@ -15,34 +15,34 @@ #include "psx_image_io.h" #include "psx_color_convert.h" #if defined(WIN32) && defined(_MSC_VER) -#include + #include #endif #include #if GIFLIB_MAJOR > 5 || GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1 -#define GIF_CLOSE_DFILE(gif) DGifCloseFile(gif, NULL) -#define GIF_CLOSE_EFILE(gif) EGifCloseFile(gif, NULL) -#define QuantizeBuffer GifQuantizeBuffer -#define MakeMapObject GifMakeMapObject -#define FreeMapObject GifFreeMapObject -#ifndef TRUE -#define TRUE 1 -#endif /* TRUE */ -#ifndef FALSE -#define FALSE 0 -#endif /* FALSE */ + #define GIF_CLOSE_DFILE(gif) DGifCloseFile(gif, NULL) + #define GIF_CLOSE_EFILE(gif) EGifCloseFile(gif, NULL) + #define QuantizeBuffer GifQuantizeBuffer + #define MakeMapObject GifMakeMapObject + #define FreeMapObject GifFreeMapObject + #ifndef TRUE + #define TRUE 1 + #endif /* TRUE */ + #ifndef FALSE + #define FALSE 0 + #endif /* FALSE */ #else -#define GIF_CLOSE_DFILE(gif) DGifCloseFile(gif) -#define GIF_CLOSE_EFILE(gif) EGifCloseFile(gif) -/* extract bytes from an unsigned word */ -#define LOBYTE(x) ((x) & 0xff) -#define HIBYTE(x) (((x) >> 8) & 0xff) + #define GIF_CLOSE_DFILE(gif) DGifCloseFile(gif) + #define GIF_CLOSE_EFILE(gif) EGifCloseFile(gif) + /* extract bytes from an unsigned word */ + #define LOBYTE(x) ((x) & 0xff) + #define HIBYTE(x) (((x) >> 8) & 0xff) #endif struct gif_image_ctx { GifFileType* gif; // read - uint8_t *buf; + uint8_t* buf; uint32_t len; uint32_t pos; // write @@ -54,9 +54,9 @@ struct gif_image_ctx { GifByteType* output_buffer; }; -static int read_gif_from_memory(GifFileType *gif, GifByteType *buf, int len) +static int read_gif_from_memory(GifFileType* gif, GifByteType* buf, int len) { - struct gif_image_ctx *data = (struct gif_image_ctx *)gif->UserData; + struct gif_image_ctx* data = (struct gif_image_ctx*)gif->UserData; if ((data->pos + len) > data->len) { len = data->len - data->pos; } @@ -110,10 +110,10 @@ static int read_gif_info(const ps_byte* data, size_t len, psx_image_header* head return 0; } -static int get_gif_transparent_color(GifFileType *gif, int frame) +static int get_gif_transparent_color(GifFileType* gif, int frame) { int x; - ExtensionBlock *ext = gif->SavedImages[frame].ExtensionBlocks; + ExtensionBlock* ext = gif->SavedImages[frame].ExtensionBlocks; int len = gif->SavedImages[frame].ExtensionBlockCount; for (x = 0; x < len; ++x, ++ext) { if ((ext->Function == GRAPHICS_EXT_FUNC_CODE) && (ext->Bytes[0] & 1)) { @@ -123,10 +123,10 @@ static int get_gif_transparent_color(GifFileType *gif, int frame) return -1; } -static int get_gif_delay_time(GifFileType *gif, int frame) +static int get_gif_delay_time(GifFileType* gif, int frame) { int x; - ExtensionBlock *ext = gif->SavedImages[frame].ExtensionBlocks; + ExtensionBlock* ext = gif->SavedImages[frame].ExtensionBlocks; int len = gif->SavedImages[frame].ExtensionBlockCount; for (x = 0; x < len; ++x, ++ext) { if (ext->Function == GRAPHICS_EXT_FUNC_CODE) { @@ -136,10 +136,10 @@ static int get_gif_delay_time(GifFileType *gif, int frame) return 0; } -static int get_gif_disposal_method(GifFileType *gif, int frame) +static int get_gif_disposal_method(GifFileType* gif, int frame) { int x; - ExtensionBlock *ext = gif->SavedImages[frame].ExtensionBlocks; + ExtensionBlock* ext = gif->SavedImages[frame].ExtensionBlocks; int len = gif->SavedImages[frame].ExtensionBlockCount; for (x = 0; x < len; ++x, ++ext) { if (ext->Function == GRAPHICS_EXT_FUNC_CODE) { @@ -155,10 +155,10 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx int bg_color = 0; int alpha_color = 0; int disposal = 0; - struct GifImageDesc *img = NULL; - struct ColorMapObject *colormap = NULL; - uint8_t *src_data = NULL; - uint32_t *dst_data = NULL; + struct GifImageDesc* img = NULL; + struct ColorMapObject* colormap = NULL; + uint8_t* src_data = NULL; + uint32_t* dst_data = NULL; struct gif_image_ctx* ctx = (struct gif_image_ctx*)header->priv; @@ -185,14 +185,14 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx for (x = 0; x < header->width; ++x) { if (*src_data != alpha_color) { *dst_data = 255 << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } else { *dst_data = ((*src_data == alpha_color) ? 0 : 255) << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } dst_data++; src_data++; @@ -209,15 +209,15 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx for (x = 0; x < header->width; ++x) { if (idx == 0) { *dst_data = ((*src_data == alpha_color) ? 0 : 255) << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } else { if (*src_data != alpha_color) { *dst_data = 255 << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } } dst_data++; @@ -242,30 +242,30 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx if (y < img->Top || y >= bottom || x < img->Left || x >= right) { if (idx == 0) { *dst_data = ((bg_color == alpha_color) ? 0 : 255) << 24 - | colormap->Colors[bg_color].Blue << 16 - | colormap->Colors[bg_color].Green << 8 - | colormap->Colors[bg_color].Red; + | colormap->Colors[bg_color].Blue << 16 + | colormap->Colors[bg_color].Green << 8 + | colormap->Colors[bg_color].Red; } } else { if (disposal == 2) { // bg_color if (*src_data != alpha_color) { *dst_data = 255 << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } else { *dst_data = ((*src_data == alpha_color) ? 0 : 255) << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; } } else { if (*src_data != alpha_color) { *dst_data = 255 << 24 - | colormap->Colors[*src_data].Blue << 16 - | colormap->Colors[*src_data].Green << 8 - | colormap->Colors[*src_data].Red; - } + | colormap->Colors[*src_data].Blue << 16 + | colormap->Colors[*src_data].Green << 8 + | colormap->Colors[*src_data].Red; + } } src_data++; } @@ -274,8 +274,8 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx } } } else { - uint32_t *dst_ptr; - uint8_t *src_ptr = src_data; + uint32_t* dst_ptr; + uint8_t* src_ptr = src_data; // Image is interlaced so that it streams nice over 14.4k and 28.8k modems :) // We first load in 1/8 of the image, followed by another 1/8, followed by // 1/4 and finally the remaining 1/2. @@ -287,9 +287,9 @@ static int decode_gif_data(psx_image_header* header, const psx_image* image, psx dst_ptr = dst_data + header->width * y; for (x = 0; x < header->width; ++x) { *dst_ptr = ((*src_ptr == alpha_color) ? 0 : 255) << 24 - | (colormap->Colors[*src_ptr].Blue) << 16 - | (colormap->Colors[*src_ptr].Green) << 8 - | (colormap->Colors[*src_ptr].Red); + | (colormap->Colors[*src_ptr].Blue) << 16 + | (colormap->Colors[*src_ptr].Green) << 8 + | (colormap->Colors[*src_ptr].Red); dst_ptr++; src_ptr++; @@ -312,50 +312,50 @@ static int release_read_gif_info(psx_image_header* header) static int get_bpp(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 4; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 3; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 2; - default: - return 4; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 4; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 3; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 2; + default: + return 4; } } static int get_depth(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 32; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 24; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 16; - default: - return 32; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 32; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 24; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 16; + default: + return 32; } } -static int write_gif_from_memory(GifFileType *gif, const GifByteType *buf, int len) +static int write_gif_from_memory(GifFileType* gif, const GifByteType* buf, int len) { - struct gif_image_ctx *ctx = (struct gif_image_ctx *)gif->UserData; + struct gif_image_ctx* ctx = (struct gif_image_ctx*)gif->UserData; ctx->writer(ctx->writer_param, buf, len); return len; } static int write_gif_info(const psx_image* image, image_writer_fn func, void* param, - float quality, psx_image_header* header) + float quality, psx_image_header* header) { size_t buf_size; #if GIFLIB_MAJOR >= 5 @@ -420,14 +420,18 @@ static int write_gif_info(const psx_image* image, image_writer_fn func, void* pa if (!ctx->red_buf || !ctx->green_buf || !ctx->blue_buf || !ctx->output_buffer) { GIF_CLOSE_EFILE(ctx->gif); - if (ctx->red_buf) + if (ctx->red_buf) { free(ctx->red_buf); - if (ctx->green_buf) + } + if (ctx->green_buf) { free(ctx->green_buf); - if (ctx->blue_buf) + } + if (ctx->blue_buf) { free(ctx->blue_buf); - if (ctx->output_buffer) + } + if (ctx->output_buffer) { free(ctx->output_buffer); + } free(ctx); return -1; @@ -447,81 +451,79 @@ static int write_gif_info(const psx_image* image, image_writer_fn func, void* pa static void gif_get_pixel_rgba_premultiply(int format, ps_byte* input_buffer, uint32_t idx, uint32_t rgba[]) { - uint8_t color[4] = {0, 0, 0, 255}; // opaque black + uint8_t color[4] = {0, 0, 0, 255}; // opaque black switch (format) { - case COLOR_FORMAT_RGBA: - { - color[0] = input_buffer[idx * 4]; - color[1] = input_buffer[idx * 4 + 1]; - color[2] = input_buffer[idx * 4 + 2]; - color[3] = input_buffer[idx * 4 + 3]; - } - break; - case COLOR_FORMAT_BGRA: - _bgra_to_rgba(color, input_buffer + idx * 4, 1); - break; - case COLOR_FORMAT_ARGB: - _argb_to_rgba(color, input_buffer + idx * 4, 1); - break; - case COLOR_FORMAT_ABGR: - _abgr_to_rgba(color, input_buffer + idx * 4, 1); - break; - case COLOR_FORMAT_RGB: - { - color[0] = input_buffer[idx * 3]; - color[1] = input_buffer[idx * 3 + 1]; - color[2] = input_buffer[idx * 3 + 2]; - } - break; - case COLOR_FORMAT_BGR: - _bgr_to_rgb(color, input_buffer + idx * 3, 1); - break; - case COLOR_FORMAT_RGB565: - _rgb565_to_rgb(color, input_buffer + idx * 2, 1); - break; - case COLOR_FORMAT_RGB555: - _rgb555_to_rgb(color, input_buffer + idx * 2, 1); - break; - default: - // do nothing - return; + case COLOR_FORMAT_RGBA: { + color[0] = input_buffer[idx * 4]; + color[1] = input_buffer[idx * 4 + 1]; + color[2] = input_buffer[idx * 4 + 2]; + color[3] = input_buffer[idx * 4 + 3]; + } + break; + case COLOR_FORMAT_BGRA: + _bgra_to_rgba(color, input_buffer + idx * 4, 1); + break; + case COLOR_FORMAT_ARGB: + _argb_to_rgba(color, input_buffer + idx * 4, 1); + break; + case COLOR_FORMAT_ABGR: + _abgr_to_rgba(color, input_buffer + idx * 4, 1); + break; + case COLOR_FORMAT_RGB: { + color[0] = input_buffer[idx * 3]; + color[1] = input_buffer[idx * 3 + 1]; + color[2] = input_buffer[idx * 3 + 2]; + } + break; + case COLOR_FORMAT_BGR: + _bgr_to_rgb(color, input_buffer + idx * 3, 1); + break; + case COLOR_FORMAT_RGB565: + _rgb565_to_rgb(color, input_buffer + idx * 2, 1); + break; + case COLOR_FORMAT_RGB555: + _rgb555_to_rgb(color, input_buffer + idx * 2, 1); + break; + default: + // do nothing + return; } - rgba[0] = ((uint32_t)color[0] * color[3] + 255) >> 8; - rgba[1] = ((uint32_t)color[1] * color[3] + 255) >> 8; - rgba[2] = ((uint32_t)color[2] * color[3] + 255) >> 8; + rgba[0] = ((uint32_t)color[0] * color[3] + 255) >> 8; + rgba[1] = ((uint32_t)color[1] * color[3] + 255) >> 8; + rgba[2] = ((uint32_t)color[2] * color[3] + 255) >> 8; rgba[3] = (uint32_t)color[3]; } static int encode_gif_data(psx_image_header* header, const psx_image* image, psx_image_frame* frame, int idx, const ps_byte* buffer, size_t buffer_len, int* ret) { int x, y; - ColorMapObject *output_map = NULL; + ColorMapObject* output_map = NULL; int map_size = 256; struct gif_image_ctx* ctx = (struct gif_image_ctx*)header->priv; if ((output_map = MakeMapObject(map_size, NULL)) == NULL) { - if (ret) *ret = S_FAILURE; + if (ret) { *ret = S_FAILURE; } return -1; } for (y = 0; y < header->height; y++) { ps_byte* row = (ps_byte*)(buffer + header->pitch * y); for (x = 0; x < header->width; x++) { - uint32_t rgba[4] = {0}; // r, g, b, a + uint32_t rgba[4] = {0}; // r, g, b, a gif_get_pixel_rgba_premultiply(header->format, row, x, rgba); ctx->red_buf[header->width * y + x] = rgba[0]; ctx->green_buf[header->width * y + x] = rgba[1]; ctx->blue_buf[header->width * y + x] = rgba[2]; } } - + if (QuantizeBuffer(header->width, header->height, &map_size, - ctx->red_buf, ctx->green_buf, ctx->blue_buf, ctx->output_buffer, output_map->Colors) == GIF_ERROR) { + ctx->red_buf, ctx->green_buf, ctx->blue_buf, ctx->output_buffer, output_map->Colors) == GIF_ERROR) { FreeMapObject(output_map); - if (ret) *ret = S_FAILURE; + if (ret) { *ret = S_FAILURE; } return -1; } @@ -540,18 +542,18 @@ static int encode_gif_data(psx_image_header* header, const psx_image* image, psx extension[0] = 0; extension[1] = LOBYTE(delay); extension[2] = HIBYTE(delay); - extension[3] = (char)-1; + extension[3] = (char) -1; #endif if (EGifPutExtension(ctx->gif, GRAPHICS_EXT_FUNC_CODE, 4, extension) == GIF_ERROR) { FreeMapObject(output_map); - if (ret) *ret = S_FAILURE; + if (ret) { *ret = S_FAILURE; } return -1; } } if (EGifPutImageDesc(ctx->gif, 0, 0, header->width, header->height, FALSE, output_map) == GIF_ERROR) { FreeMapObject(output_map); - if (ret) *ret = S_FAILURE; + if (ret) { *ret = S_FAILURE; } return -1; } @@ -568,20 +570,24 @@ static int release_write_gif_info(psx_image_header* header) GIF_CLOSE_EFILE(ctx->gif); - if (ctx->red_buf) + if (ctx->red_buf) { free(ctx->red_buf); - if (ctx->green_buf) + } + if (ctx->green_buf) { free(ctx->green_buf); - if (ctx->blue_buf) + } + if (ctx->blue_buf) { free(ctx->blue_buf); - if (ctx->output_buffer) + } + if (ctx->output_buffer) { free(ctx->output_buffer); + } free(ctx); return 0; } -static psx_image_operator * gif_coder = NULL; +static psx_image_operator* gif_coder = NULL; static module_handle lib_image = INVALID_HANDLE; typedef int (*register_func)(const char*, const ps_byte*, size_t, size_t, psx_priority_level, psx_image_operator*); @@ -591,11 +597,12 @@ typedef int (*unregister_func)(psx_image_operator*); static wchar_t g_path[MAX_PATH]; static wchar_t* get_library_path(void) { - wchar_t *p = 0; + wchar_t* p = 0; memset(g_path, 0, sizeof(wchar_t) * MAX_PATH); GetModuleFileName(NULL, g_path, MAX_PATH); p = wcsrchr(g_path, '\\'); - p++; *p = 0; + p++; + *p = 0; lstrcat(g_path, L"psx_image.dll"); return g_path; } @@ -610,16 +617,19 @@ void psx_image_module_init(void) #else lib_image = _module_load("libpsx_image.so"); #endif - if (lib_image == INVALID_HANDLE) + if (lib_image == INVALID_HANDLE) { return; + } func = _module_get_symbol(lib_image, "psx_image_register_operator"); - if (!func) + if (!func) { return; + } gif_coder = (psx_image_operator*)calloc(1, sizeof(psx_image_operator)); - if (!gif_coder) + if (!gif_coder) { return; + } gif_coder->read_header_info = read_gif_info; gif_coder->decode_image_data = decode_gif_data; @@ -644,8 +654,9 @@ void psx_image_module_shutdown(void) } } - if (lib_image != INVALID_HANDLE) + if (lib_image != INVALID_HANDLE) { _module_unload(lib_image); + } } const char* psx_image_module_get_string(int idx) @@ -657,4 +668,3 @@ const char* psx_image_module_get_string(int idx) return "unknown"; } } - diff --git a/ext/image_coders/image.cmake b/ext/image_coders/image.cmake index 4508c04..4be60de 100644 --- a/ext/image_coders/image.cmake +++ b/ext/image_coders/image.cmake @@ -6,7 +6,6 @@ set(PXIMG_DIR ${PROJECT_ROOT}/ext/image_coders) set(PXIMG_SOURCES - ${PXIMG_DIR}/psx_list.h ${PXIMG_DIR}/psx_image_io.h ${PXIMG_DIR}/psx_image_io.c ${PXIMG_DIR}/psx_image_loader.h @@ -31,11 +30,11 @@ add_library(${LIBX_IMAGE} ${PXIMG_SOURCES}) install(TARGETS ${LIBX_IMAGE} LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) set_target_properties(${LIBX_IMAGE} PROPERTIES VERSION ${VERSION_INFO} SOVERSION 1) -include_directories(${PXIMG_DIR} ${PROJECT_ROOT}/include ${PROJECT_ROOT}/include/images) -target_link_libraries(psx_image PRIVATE ${LIB_NAME}) +include_directories(${PXIMG_DIR} ${PROJECT_ROOT}/ext/common ${PROJECT_ROOT}/include ${PROJECT_ROOT}/include/images) +target_link_libraries(${LIBX_IMAGE} PRIVATE ${LIB_NAME}) if (UNIX AND NOT APPLE) -target_link_libraries(psx_image PUBLIC dl) +target_link_libraries(${LIBX_IMAGE} PUBLIC dl) endif() if (NOT APPLE) diff --git a/ext/image_coders/jpeg/jpeg_module.c b/ext/image_coders/jpeg/jpeg_module.c index e27d64e..d7eb9ee 100644 --- a/ext/image_coders/jpeg/jpeg_module.c +++ b/ext/image_coders/jpeg/jpeg_module.c @@ -15,7 +15,7 @@ #include "psx_image_io.h" #include "psx_color_convert.h" #if defined(WIN32) && defined(_MSC_VER) -#include + #include #endif #include "jpeglib.h" #include @@ -97,12 +97,14 @@ static int read_jpg_info(const ps_byte* data, size_t len, psx_image_header* head static int decode_jpg_data(psx_image_header* header, const psx_image* image, psx_image_frame* frame, int idx, ps_byte* buffer, size_t buffer_len) { - int y; size_t i; + int y; + size_t i; struct jpeg_image_ctx* ctx = (struct jpeg_image_ctx*)header->priv; - unsigned char * row_buffer = (unsigned char*)malloc(ctx->dinfo.image_width * 3); - if (!row_buffer) + unsigned char* row_buffer = (unsigned char*)malloc(ctx->dinfo.image_width * 3); + if (!row_buffer) { return -1; + } if (setjmp(ctx->err.jmp_buffer)) { free(row_buffer); @@ -150,37 +152,37 @@ static int release_read_jpg_info(psx_image_header* header) static int get_bpp(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 4; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - case COLOR_FORMAT_RGB565: // rgb565 will convert to rgb - case COLOR_FORMAT_RGB555: // rgb555 will convert to rgb - return 3; - default: - return 4; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 4; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + case COLOR_FORMAT_RGB565: // rgb565 will convert to rgb + case COLOR_FORMAT_RGB555: // rgb555 will convert to rgb + return 3; + default: + return 4; } } static int get_depth(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 32; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 24; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 16; - default: - return 32; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 32; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 24; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 16; + default: + return 32; } } @@ -223,7 +225,7 @@ static void _term_destination(j_compress_ptr cinfo) } static int write_jpg_info(const psx_image* image, image_writer_fn func, void* param, - float quality, psx_image_header* header) + float quality, psx_image_header* header) { struct jpeg_image_ctx* ctx = (struct jpeg_image_ctx*)calloc(1, sizeof(struct jpeg_image_ctx)); if (!ctx) { @@ -241,7 +243,7 @@ static int write_jpg_info(const psx_image* image, image_writer_fn func, void* pa jpeg_create_compress(&ctx->cinfo); - ctx->cinfo.dest = (struct jpeg_destination_mgr *)&ctx->dst; + ctx->cinfo.dest = (struct jpeg_destination_mgr*)&ctx->dst; ctx->dst.writer = func; ctx->dst.writer_param = param; @@ -275,12 +277,13 @@ static void jpeg_convert_32bit(psx_image_header* header, const ps_byte* buffer, for (y = 0; y < header->height; y++) { ps_byte* row = (ps_byte*)(buffer + header->pitch * y); - if (header->format == (int)COLOR_FORMAT_BGRA) + if (header->format == (int)COLOR_FORMAT_BGRA) { _bgra_to_rgba(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_ABGR) + } else if (header->format == (int)COLOR_FORMAT_ABGR) { _abgr_to_rgba(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_ARGB) + } else if (header->format == (int)COLOR_FORMAT_ARGB) { _argb_to_rgba(cbuf, row, header->width); + } ctx->rowp[0] = cbuf; jpeg_write_scanlines(&ctx->cinfo, ctx->rowp, 1); @@ -294,12 +297,13 @@ static void jpeg_convert_24bit(psx_image_header* header, const ps_byte* buffer, for (y = 0; y < header->height; y++) { ps_byte* row = (ps_byte*)(buffer + header->pitch * y); - if (header->format == (int)COLOR_FORMAT_BGR) + if (header->format == (int)COLOR_FORMAT_BGR) { _bgr_to_rgb(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_RGB565) + } else if (header->format == (int)COLOR_FORMAT_RGB565) { _rgb565_to_rgb(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_RGB555) + } else if (header->format == (int)COLOR_FORMAT_RGB555) { _rgb555_to_rgb(cbuf, row, header->width); + } ctx->rowp[0] = cbuf; jpeg_write_scanlines(&ctx->cinfo, ctx->rowp, 1); @@ -331,13 +335,13 @@ static int encode_jpg_data(psx_image_header* header, const psx_image* image, psx } } else { if (header->format == (int)COLOR_FORMAT_BGRA - || header->format == (int)COLOR_FORMAT_ABGR - || header->format == (int)COLOR_FORMAT_ARGB) { - // convert to 32bit - jpeg_convert_32bit(header, buffer, buffer_len, cbuf); + || header->format == (int)COLOR_FORMAT_ABGR + || header->format == (int)COLOR_FORMAT_ARGB) { + // convert to 32bit + jpeg_convert_32bit(header, buffer, buffer_len, cbuf); } else { - // convert to 24bit - jpeg_convert_24bit(header, buffer, buffer_len, cbuf); + // convert to 24bit + jpeg_convert_24bit(header, buffer, buffer_len, cbuf); } } @@ -354,7 +358,7 @@ static int release_write_jpg_info(psx_image_header* header) return 0; } -static psx_image_operator * jpg_coder = NULL; +static psx_image_operator* jpg_coder = NULL; static module_handle lib_image = INVALID_HANDLE; typedef int (*register_func)(const char*, const ps_byte*, size_t, size_t, psx_priority_level, psx_image_operator*); @@ -364,11 +368,12 @@ typedef int (*unregister_func)(psx_image_operator*); static wchar_t g_path[MAX_PATH]; static wchar_t* get_library_path(void) { - wchar_t *p = 0; + wchar_t* p = 0; memset(g_path, 0, sizeof(wchar_t) * MAX_PATH); GetModuleFileName(NULL, g_path, MAX_PATH); p = wcsrchr(g_path, '\\'); - p++; *p = 0; + p++; + *p = 0; lstrcat(g_path, L"psx_image.dll"); return g_path; } @@ -383,16 +388,19 @@ void psx_image_module_init(void) #else lib_image = _module_load("libpsx_image.so"); #endif - if (lib_image == INVALID_HANDLE) + if (lib_image == INVALID_HANDLE) { return; + } func = _module_get_symbol(lib_image, "psx_image_register_operator"); - if (!func) + if (!func) { return; + } jpg_coder = (psx_image_operator*)calloc(1, sizeof(psx_image_operator)); - if (!jpg_coder) + if (!jpg_coder) { return; + } jpg_coder->read_header_info = read_jpg_info; jpg_coder->decode_image_data = decode_jpg_data; @@ -419,8 +427,9 @@ void psx_image_module_shutdown(void) } } - if (lib_image != INVALID_HANDLE) + if (lib_image != INVALID_HANDLE) { _module_unload(lib_image); + } } const char* psx_image_module_get_string(int idx) @@ -432,4 +441,3 @@ const char* psx_image_module_get_string(int idx) return "unknown"; } } - diff --git a/ext/image_coders/png/png_module.c b/ext/image_coders/png/png_module.c index 99ef244..1161863 100644 --- a/ext/image_coders/png/png_module.c +++ b/ext/image_coders/png/png_module.c @@ -16,7 +16,7 @@ #include "psx_image_io.h" #include "psx_color_convert.h" #if defined(WIN32) && defined(_MSC_VER) -#include + #include #endif struct png_image_ctx { @@ -34,8 +34,9 @@ struct png_image_ctx { static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { struct png_image_ctx* ctx = (struct png_image_ctx*)png_get_io_ptr(png_ptr); - if (ctx->pos + length > ctx->end) + if (ctx->pos + length > ctx->end) { png_error(png_ptr, "PNG read error!"); + } memcpy(data, ctx->pos, length); ctx->pos += length; @@ -43,7 +44,7 @@ static void png_read_data(png_structp png_ptr, png_bytep data, png_size_t length static int read_png_info(const ps_byte* data, size_t len, psx_image_header* header) { - png_uint_32 width,height; + png_uint_32 width, height; int bit_depth, color_type, interlace_type, rowbytes, bpp; double screen_gamma = 2.2; // typical value double gamma; @@ -55,7 +56,7 @@ static int read_png_info(const ps_byte* data, size_t len, psx_image_header* head } ctx->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!ctx->png_ptr){ + if (!ctx->png_ptr) { free(ctx); return -1; } @@ -81,39 +82,47 @@ static int read_png_info(const ps_byte* data, size_t len, psx_image_header* head png_read_info(ctx->png_ptr, ctx->info_ptr); png_get_IHDR(ctx->png_ptr, ctx->info_ptr, &width, &height, - &bit_depth, &color_type, &interlace_type, NULL, NULL); + &bit_depth, &color_type, &interlace_type, NULL, NULL); - // configure transformations, we always want RGB data in the end + // configure transformations, we always want RGB data in the end if (color_type == PNG_COLOR_TYPE_GRAY || - color_type == PNG_COLOR_TYPE_GRAY_ALPHA) - png_set_gray_to_rgb(ctx->png_ptr); - if (color_type == PNG_COLOR_TYPE_PALETTE) - png_set_palette_to_rgb(ctx->png_ptr); + color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { + png_set_gray_to_rgb(ctx->png_ptr); + } + if (color_type == PNG_COLOR_TYPE_PALETTE) { + png_set_palette_to_rgb(ctx->png_ptr); + } if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) #if PNG_LIBPNG_VER >= 10209 - png_set_expand_gray_1_2_4_to_8(ctx->png_ptr); + png_set_expand_gray_1_2_4_to_8(ctx->png_ptr); #else - png_set_gray_1_2_4_to_8(ctx->png_ptr); + png_set_gray_1_2_4_to_8(ctx->png_ptr); #endif - if (png_get_valid(ctx->png_ptr, ctx->info_ptr,PNG_INFO_tRNS)) - png_set_tRNS_to_alpha(ctx->png_ptr); - if (bit_depth == 16) - png_set_strip_16(ctx->png_ptr); - if (bit_depth < 8) - png_set_packing(ctx->png_ptr); - - if (png_get_bKGD(ctx->png_ptr, ctx->info_ptr, &dib_background)) + if (png_get_valid(ctx->png_ptr, ctx->info_ptr, PNG_INFO_tRNS)) { + png_set_tRNS_to_alpha(ctx->png_ptr); + } + if (bit_depth == 16) { + png_set_strip_16(ctx->png_ptr); + } + if (bit_depth < 8) { + png_set_packing(ctx->png_ptr); + } + + if (png_get_bKGD(ctx->png_ptr, ctx->info_ptr, &dib_background)) { png_set_background(ctx->png_ptr, dib_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); + } - if (png_get_gAMA(ctx->png_ptr, ctx->info_ptr, &gamma)) + if (png_get_gAMA(ctx->png_ptr, ctx->info_ptr, &gamma)) { png_set_gamma(ctx->png_ptr, screen_gamma, gamma); - else + } else { png_set_gamma(ctx->png_ptr, screen_gamma, 0.45455); + } - if (interlace_type != PNG_INTERLACE_NONE) + if (interlace_type != PNG_INTERLACE_NONE) { ctx->interlace_pass = png_set_interlace_handling(ctx->png_ptr); - else + } else { ctx->interlace_pass = 1; + } // update info after applying transformations png_read_update_info(ctx->png_ptr, ctx->info_ptr); @@ -169,38 +178,38 @@ static void png_write_data(png_structp png_ptr, png_bytep data, png_size_t lengt static int get_bpp(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 4; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 3; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 2; - default: - return 4; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 4; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 3; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 2; + default: + return 4; } } static int get_depth(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 32; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 24; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 16; - default: - return 32; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 32; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 24; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 16; + default: + return 32; } } @@ -213,7 +222,7 @@ static int write_png_info(const psx_image* image, image_writer_fn func, void* pa } ctx->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!ctx->png_ptr){ + if (!ctx->png_ptr) { free(ctx); return -1; } @@ -236,27 +245,26 @@ static int write_png_info(const psx_image* image, image_writer_fn func, void* pa png_set_write_fn(ctx->png_ptr, (void*)ctx, png_write_data, NULL); - switch (image->format) - { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - fmt = PNG_COLOR_TYPE_RGB_ALPHA; - break; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - case COLOR_FORMAT_RGB565: // rgb565 will convert to rgb - case COLOR_FORMAT_RGB555: // rgb555 will convert to rgb - fmt = PNG_COLOR_TYPE_RGB; - break; - default: - fmt = PNG_COLOR_TYPE_RGB_ALPHA; // impossible here. - break; + switch (image->format) { + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + fmt = PNG_COLOR_TYPE_RGB_ALPHA; + break; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + case COLOR_FORMAT_RGB565: // rgb565 will convert to rgb + case COLOR_FORMAT_RGB555: // rgb555 will convert to rgb + fmt = PNG_COLOR_TYPE_RGB; + break; + default: + fmt = PNG_COLOR_TYPE_RGB_ALPHA; // impossible here. + break; } png_set_IHDR(ctx->png_ptr, ctx->info_ptr, image->width, image->height, 8, fmt, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(ctx->png_ptr, ctx->info_ptr); @@ -287,12 +295,13 @@ static void png_convert_32bit(psx_image_header* header, const ps_byte* buffer, s for (y = 0; y < header->height; y++) { ps_byte* row = (ps_byte*)(buffer + header->pitch * y); - if (header->format == (int)COLOR_FORMAT_BGRA) + if (header->format == (int)COLOR_FORMAT_BGRA) { _bgra_to_rgba(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_ABGR) + } else if (header->format == (int)COLOR_FORMAT_ABGR) { _abgr_to_rgba(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_ARGB) + } else if (header->format == (int)COLOR_FORMAT_ARGB) { _argb_to_rgba(cbuf, row, header->width); + } png_write_row(ctx->png_ptr, cbuf); } @@ -305,12 +314,13 @@ static void png_convert_24bit(psx_image_header* header, const ps_byte* buffer, s for (y = 0; y < header->height; y++) { ps_byte* row = (ps_byte*)(buffer + header->pitch * y); - if (header->format == (int)COLOR_FORMAT_BGR) + if (header->format == (int)COLOR_FORMAT_BGR) { _bgr_to_rgb(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_RGB565) + } else if (header->format == (int)COLOR_FORMAT_RGB565) { _rgb565_to_rgb(cbuf, row, header->width); - else if (header->format == (int)COLOR_FORMAT_RGB555) + } else if (header->format == (int)COLOR_FORMAT_RGB555) { _rgb555_to_rgb(cbuf, row, header->width); + } png_write_row(ctx->png_ptr, cbuf); } @@ -338,13 +348,13 @@ static int encode_png_data(psx_image_header* header, const psx_image* image, psx } } else { if (header->format == (int)COLOR_FORMAT_BGRA - || header->format == (int)COLOR_FORMAT_ABGR - || header->format == (int)COLOR_FORMAT_ARGB) { - // convert to 32bit - png_convert_32bit(header, buffer, buffer_len, cbuf); + || header->format == (int)COLOR_FORMAT_ABGR + || header->format == (int)COLOR_FORMAT_ARGB) { + // convert to 32bit + png_convert_32bit(header, buffer, buffer_len, cbuf); } else { - // convert to 24bit - png_convert_24bit(header, buffer, buffer_len, cbuf); + // convert to 24bit + png_convert_24bit(header, buffer, buffer_len, cbuf); } } @@ -353,7 +363,7 @@ static int encode_png_data(psx_image_header* header, const psx_image* image, psx return 0; } -static psx_image_operator * png_coder = NULL; +static psx_image_operator* png_coder = NULL; static module_handle lib_image = INVALID_HANDLE; typedef int (*register_func)(const char*, const ps_byte*, size_t, size_t, psx_priority_level, psx_image_operator*); @@ -363,11 +373,12 @@ typedef int (*unregister_func)(psx_image_operator*); static wchar_t g_path[MAX_PATH]; static wchar_t* get_library_path(void) { - wchar_t *p = 0; + wchar_t* p = 0; memset(g_path, 0, sizeof(wchar_t) * MAX_PATH); GetModuleFileName(NULL, g_path, MAX_PATH); p = wcsrchr(g_path, '\\'); - p++; *p = 0; + p++; + *p = 0; lstrcat(g_path, L"psx_image.dll"); return g_path; } @@ -382,16 +393,19 @@ void psx_image_module_init(void) #else lib_image = _module_load("libpsx_image.so"); #endif - if (lib_image == INVALID_HANDLE) + if (lib_image == INVALID_HANDLE) { return; + } func = _module_get_symbol(lib_image, "psx_image_register_operator"); - if (!func) + if (!func) { return; + } png_coder = (psx_image_operator*)calloc(1, sizeof(psx_image_operator)); - if (!png_coder) + if (!png_coder) { return; + } png_coder->read_header_info = read_png_info; png_coder->decode_image_data = decode_png_data; @@ -416,8 +430,9 @@ void psx_image_module_shutdown(void) } } - if (lib_image != INVALID_HANDLE) + if (lib_image != INVALID_HANDLE) { _module_unload(lib_image); + } } const char* psx_image_module_get_string(int idx) @@ -429,4 +444,3 @@ const char* psx_image_module_get_string(int idx) return "unknown"; } } - diff --git a/ext/image_coders/psx_image_io.c b/ext/image_coders/psx_image_io.c index cade9a8..974de44 100644 --- a/ext/image_coders/psx_image_io.c +++ b/ext/image_coders/psx_image_io.c @@ -11,30 +11,31 @@ #include #if defined(WIN32) -#include + #include #else -#include -#include -#include -#include + #include + #include + #include + #include #endif #include "psx_image_io.h" #if defined(WIN32) && defined(_MSC_VER) -wchar_t* pstring_create(const char* str, size_t * rlen) +wchar_t* pstring_create(const char* str, size_t* rlen) { wchar_t* ustr = NULL; size_t len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); - if (!len) + if (!len) { return NULL; + } - ustr = (wchar_t*)malloc((len+1) * sizeof(wchar_t)); - memset(ustr, 0, sizeof(wchar_t) * (len+1)); + ustr = (wchar_t*)malloc((len + 1) * sizeof(wchar_t)); + memset(ustr, 0, sizeof(wchar_t) * (len + 1)); MultiByteToWideChar(CP_UTF8, 0, str, -1, ustr, (int)len); - if (rlen) *rlen = len; + if (rlen) { *rlen = len; } return ustr; } @@ -44,17 +45,19 @@ wchar_t* pstring_create(const char* str, size_t * rlen) int _file_exists(const wchar_t* path) { struct _stat info; - if (STAT(path, &info) != 0) + if (STAT(path, &info) != 0) { return -1; + } - return S_IFREG &info.st_mode ? 0 : -1; + return S_IFREG & info.st_mode ? 0 : -1; } size_t _file_size(const wchar_t* path) { struct _stat info; - if (STAT(path, &info) != 0) + if (STAT(path, &info) != 0) { return 0; + } return info.st_size; } @@ -67,8 +70,9 @@ int _file_remove(const wchar_t* path) module_handle _module_load(const wchar_t* path) { HMODULE dl = LoadLibraryW(path); - if (!dl) + if (!dl) { fwprintf(stderr, L"Load Module [%s] failed code: %x\n", path, (int)GetLastError()); + } return (module_handle)dl; } @@ -84,10 +88,11 @@ void _module_unload(module_handle module) static wchar_t* _get_current_path(wchar_t* buffer, size_t len, size_t* rlen) { - wchar_t *p = 0; + wchar_t* p = 0; *rlen = (size_t)GetModuleFileName(NULL, buffer, (DWORD)len); p = wcsrchr(buffer, '\\'); - p++; *p = 0; + p++; + *p = 0; return buffer; } @@ -104,13 +109,14 @@ static int _directory_is_exists(const wchar_t* path) wchar_t* _module_get_modules_dir(wchar_t* path_buffer, size_t buffer_size) { size_t len = 0; - wchar_t * current_path = _get_current_path(path_buffer, buffer_size, &len); - if ((len + 24) >= buffer_size) + wchar_t* current_path = _get_current_path(path_buffer, buffer_size, &len); + if ((len + 24) >= buffer_size) { return NULL; //path is too long. + } lstrcat (current_path, L"modules"); - if (_directory_is_exists(current_path) == 0){ + if (_directory_is_exists(current_path) == 0) { return path_buffer; } else { return NULL; @@ -136,12 +142,13 @@ size_t _module_get_modules(const wchar_t* dir_path, wchar_t* paths[], size_t siz dir_path_len = lstrlen(dir_path); while (1) { - if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) + if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { continue; + } if (paths && (nums < size)) { size_t len = lstrlen(fd.cFileName); - wchar_t* path = (wchar_t*)calloc((dir_path_len+len+2), sizeof(wchar_t)); + wchar_t* path = (wchar_t*)calloc((dir_path_len + len + 2), sizeof(wchar_t)); if (path) { lstrcpy(path, dir_path); lstrcat(path, L"\\"); @@ -151,8 +158,9 @@ size_t _module_get_modules(const wchar_t* dir_path, wchar_t* paths[], size_t siz } nums++; - if (!FindNextFile(handle, &fd)) - break; + if (!FindNextFile(handle, &fd)) { + break; + } } FindClose(handle); return nums; @@ -161,8 +169,9 @@ size_t _module_get_modules(const wchar_t* dir_path, wchar_t* paths[], size_t siz int _file_exists(const char* path) { struct stat info; - if (stat(path, &info) != 0) + if (stat(path, &info) != 0) { return -1; + } return S_ISREG(info.st_mode) ? 0 : -1; } @@ -170,8 +179,9 @@ int _file_exists(const char* path) size_t _file_size(const char* path) { struct stat info; - if (stat(path, &info) != 0) + if (stat(path, &info) != 0) { return 0; + } return info.st_size; } @@ -182,9 +192,9 @@ int _file_remove(const char* path) } #ifdef __APPLE__ -#define SO_EXT "dylib" + #define SO_EXT "dylib" #else -#define SO_EXT "so" + #define SO_EXT "so" #endif size_t _module_get_modules(const char* dir_path, char* paths[], size_t size) @@ -195,20 +205,22 @@ size_t _module_get_modules(const char* dir_path, char* paths[], size_t size) size_t dir_path_len = 0; dir = opendir(dir_path); - if (!dir) + if (!dir) { return 0; + } dir_path_len = strlen(dir_path); while ((de = readdir(dir)) != NULL) { - if (fnmatch("*."SO_EXT, de->d_name, 0) != 0) + if (fnmatch("*."SO_EXT, de->d_name, 0) != 0) { continue; + } if (paths && (nums < size)) { size_t len = strlen(de->d_name); - char* path = (char*)malloc(dir_path_len+len+1); + char* path = (char*)malloc(dir_path_len + len + 1); if (path) { memcpy(path, dir_path, dir_path_len); - memcpy(path+dir_path_len, de->d_name, len+1); + memcpy(path + dir_path_len, de->d_name, len + 1); paths[nums] = path; } } @@ -225,69 +237,78 @@ char* _module_get_modules_dir(char* path_buffer, size_t buffer_size) char* path_seek = NULL; size_t length = 0; - lib_paths = (char*)getenv("PS_IMAGE_MODULES_DIR"); if (lib_paths) { return lib_paths; } - if ((stat("/usr/lib/modules", &info) == 0) && S_ISDIR(info.st_mode)){ + if ((stat("/usr/lib/modules", &info) == 0) && S_ISDIR(info.st_mode)) { if (buffer_size > 24) { strncpy(path_buffer, "/usr/lib/modules/", buffer_size); return path_buffer; - } else + } else { return NULL; + } } - if ((stat("/usr/local/lib/modules", &info) == 0) && S_ISDIR(info.st_mode)){ + if ((stat("/usr/local/lib/modules", &info) == 0) && S_ISDIR(info.st_mode)) { if (buffer_size > 32) { strncpy(path_buffer, "/usr/local/lib/modules/", buffer_size); return path_buffer; - } else + } else { return NULL; + } } lib_paths = (char*)getenv("LD_LIBRARY_PATH"); - if (!lib_paths) + if (!lib_paths) { return NULL; + } length = strlen(lib_paths); - if (!length) + if (!length) { return NULL; + } while (length > 0) { memset(path_buffer, 0, buffer_size); path_seek = strchr(lib_paths, ':'); if (!path_seek) { //end of string - if ((length + 16) > buffer_size) + if ((length + 16) > buffer_size) { return NULL; //path_buffer too small. + } strncpy(path_buffer, lib_paths, buffer_size); - if (path_buffer[length-1] != '/') + if (path_buffer[length - 1] != '/') { path_buffer[length] = '/'; + } strcat(path_buffer, "modules/"); - if ((stat(path_buffer, &info) == 0) && S_ISDIR(info.st_mode)) + if ((stat(path_buffer, &info) == 0) && S_ISDIR(info.st_mode)) { return path_buffer; + } length -= length; } else { size_t len = path_seek - lib_paths; - if ((len + 16) > buffer_size) + if ((len + 16) > buffer_size) { return NULL; //path_buffer too small. + } strncpy(path_buffer, lib_paths, len); - if (path_buffer[len-1] != '/') + if (path_buffer[len - 1] != '/') { path_buffer[len] = '/'; + } strcat(path_buffer, "modules/"); - if ((stat(path_buffer, &info) == 0) && S_ISDIR(info.st_mode)) + if ((stat(path_buffer, &info) == 0) && S_ISDIR(info.st_mode)) { return path_buffer; + } length -= len; lib_paths = path_seek + 1; @@ -299,8 +320,9 @@ char* _module_get_modules_dir(char* path_buffer, size_t buffer_size) module_handle _module_load(const char* path) { void* dl = dlopen(path, RTLD_LAZY); - if (!dl) + if (!dl) { fprintf(stderr, "Load Module [%s] failed: %s\n", path, dlerror()); + } return dl; } @@ -314,9 +336,9 @@ void _module_unload(module_handle module) dlclose(module); } -char* pstring_create(const char* str, size_t * rlen) +char* pstring_create(const char* str, size_t* rlen) { - if (rlen) *rlen = strlen(str); + if (rlen) { *rlen = strlen(str); } return strdup(str); } @@ -330,11 +352,12 @@ int _file_read(const pchar* path, unsigned char* buffer, size_t buffer_size) int retval = 0; FILE* fp = FOPEN(path, "rb"); - if (!fp) + if (!fp) { return -1; + } read_bytes = fread(buffer, 1, buffer_size, fp); - if (read_bytes != buffer_size){ + if (read_bytes != buffer_size) { retval = -1; } @@ -348,15 +371,15 @@ int _file_write(const pchar* path, unsigned char* buffer, size_t buffer_size) int retval = 0; FILE* fp = FOPEN(path, "ab"); - if (!fp) + if (!fp) { return -1; + } write_bytes = fwrite(buffer, 1, buffer_size, fp); - if (write_bytes != buffer_size){ + if (write_bytes != buffer_size) { retval = -1; } fclose(fp); return retval; } - diff --git a/ext/image_coders/psx_image_loader.c b/ext/image_coders/psx_image_loader.c index 5e6ce37..1653d1a 100644 --- a/ext/image_coders/psx_image_loader.c +++ b/ext/image_coders/psx_image_loader.c @@ -20,36 +20,34 @@ static struct image_modules_mgr* g_modules = NULL; static ps_color_format get_format(const psx_image_header* hdr) { - switch (hdr->bpp) - { - case 4: - return COLOR_FORMAT_RGBA; - case 3: - return COLOR_FORMAT_RGB; - case 2: - return COLOR_FORMAT_RGB565; - default: - return COLOR_FORMAT_UNKNOWN; + switch (hdr->bpp) { + case 4: + return COLOR_FORMAT_RGBA; + case 3: + return COLOR_FORMAT_RGB; + case 2: + return COLOR_FORMAT_RGB565; + default: + return COLOR_FORMAT_UNKNOWN; } } static int get_bpp(ps_color_format fmt) { - switch (fmt) - { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - case COLOR_FORMAT_BGRA: - return 4; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 3; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 2; - default: - return 0; + switch (fmt) { + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + case COLOR_FORMAT_BGRA: + return 4; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 3; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 2; + default: + return 0; } } @@ -64,8 +62,9 @@ int PICAPI psx_image_init(void) if (!g_modules) { g_modules = (struct image_modules_mgr*)calloc(1, sizeof(struct image_modules_mgr)); - if (!g_modules) + if (!g_modules) { return S_OUT_OF_MEMORY; + } if (modules_init(g_modules) != 0) { free(g_modules); @@ -93,17 +92,19 @@ psx_image* PICAPI psx_image_create_from_data(ps_byte* data, ps_color_format fmt, { int i; size_t size; - psx_image * image; + psx_image* image; if (!data || width <= 0 || height <= 0 || pitch <= 0) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } return NULL; } if ((int)fmt < 0 || fmt >= COLOR_FORMAT_UNKNOWN) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } return NULL; } @@ -118,8 +119,9 @@ psx_image* PICAPI psx_image_create_from_data(ps_byte* data, ps_color_format fmt, image->frames = (psx_image_frame*)calloc(image->num_frames, sizeof(psx_image_frame)); if (!image->frames) { - if (err_code) + if (err_code) { *err_code = S_OUT_OF_MEMORY; + } free(image); return NULL; } @@ -130,8 +132,9 @@ psx_image* PICAPI psx_image_create_from_data(ps_byte* data, ps_color_format fmt, image->frames[0].duration = 0; if (!image->frames[0].data) { - if (err_code) + if (err_code) { *err_code = S_OUT_OF_MEMORY; + } free(image->frames); free(image); return NULL; @@ -143,26 +146,30 @@ psx_image* PICAPI psx_image_create_from_data(ps_byte* data, ps_color_format fmt, // create ps_image object. image->frames[0].img = ps_image_create_with_data(image->frames[0].data, image->format, - image->width, image->height, image->pitch); + image->width, image->height, image->pitch); if (!image->frames[0].img) { - if (err_code) + if (err_code) { *err_code = S_OUT_OF_MEMORY; + } free(image->frames[0].data); free(image->frames); free(image); return NULL; } - if (get_bpp(fmt) == 4) // has alpha + if (get_bpp(fmt) == 4) { // has alpha ps_image_set_allow_transparent(image->frames[0].img, True); + } } else { - if (err_code) + if (err_code) { *err_code = S_OUT_OF_MEMORY; - return NULL; + } + return NULL; } - if (err_code) + if (err_code) { *err_code = S_OK; + } return image; } @@ -170,49 +177,55 @@ psx_image* PICAPI psx_image_load(const char* name, int* err_code) { size_t size; ps_byte* file_data; - psx_image * image; + psx_image* image; pchar* file_name; if (!name) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } return NULL; } file_name = pstring_create(name, NULL); if (!file_name) { - if (err_code) + if (err_code) { *err_code = S_FAILURE; + } return NULL; } if (_file_exists(file_name) != 0) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } free(file_name); return NULL; } size = _file_size(file_name); if (!size) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } free(file_name); return NULL; } file_data = (ps_byte*)malloc(size); if (!file_data) { - if (err_code) + if (err_code) { *err_code = S_OUT_OF_MEMORY; + } free(file_name); return NULL; } // read file data. - if (_file_read(file_name, file_data, size) != 0){ + if (_file_read(file_name, file_data, size) != 0) { free(file_data); - if (err_code) + if (err_code) { *err_code = S_FAILURE; + } free(file_name); return NULL; } @@ -220,8 +233,9 @@ psx_image* PICAPI psx_image_load(const char* name, int* err_code) image = psx_image_load_from_memory(file_data, size, err_code); free(file_name); free(file_data); - if (err_code) + if (err_code) { *err_code = S_OK; + } return image; } @@ -230,11 +244,13 @@ static psx_image* load_psx_image(psx_image_operator* op, const ps_byte* data, si size_t i; psx_image_header header; psx_image* image = NULL; - if (!op->read_header_info || !op->decode_image_data) + if (!op->read_header_info || !op->decode_image_data) { return NULL; + } - if (op->read_header_info(data, len, &header) != 0) + if (op->read_header_info(data, len, &header) != 0) { return NULL; + } image = (psx_image*)calloc(1, sizeof(psx_image)); if (image) { @@ -246,33 +262,39 @@ static psx_image* load_psx_image(psx_image_operator* op, const ps_byte* data, si image->num_frames = header.frames; image->frames = (psx_image_frame*)calloc(image->num_frames, sizeof(psx_image_frame)); - if (!image->frames) + if (!image->frames) { goto error; + } for (i = 0; i < image->num_frames; i++) { size_t size = image->pitch * image->height; image->frames[i].size = size; image->frames[i].data = (ps_byte*)calloc(1, size); image->frames[i].duration = 0; - if (!image->frames[i].data) + if (!image->frames[i].data) { goto error; + } - if (op->decode_image_data(&header, image, &image->frames[i], (int)i, image->frames[i].data, size) != 0) + if (op->decode_image_data(&header, image, &image->frames[i], (int)i, image->frames[i].data, size) != 0) { goto error; + } // create ps_image object. image->frames[i].img = ps_image_create_with_data(image->frames[i].data, image->format, image->width, image->height, image->pitch); - if (!image->frames[i].img) + if (!image->frames[i].img) { goto error; + } - if (header.alpha == 1) // has alpha + if (header.alpha == 1) { // has alpha ps_image_set_allow_transparent(image->frames[i].img, True); + } } } - if (op->release_read_header_info) + if (op->release_read_header_info) { op->release_read_header_info(&header); + } return image; error: @@ -280,8 +302,9 @@ static psx_image* load_psx_image(psx_image_operator* op, const ps_byte* data, si for (i = 0; i < image->num_frames; i++) { // free buffer pixels data and ps_image object. if (image->frames[i].data) { - if (image->frames[i].img) + if (image->frames[i].img) { ps_image_unref(image->frames[i].img); + } free(image->frames[i].data); } } @@ -289,67 +312,77 @@ static psx_image* load_psx_image(psx_image_operator* op, const ps_byte* data, si } free(image); - if (op->release_read_header_info) + if (op->release_read_header_info) { op->release_read_header_info(&header); + } return NULL; } static int save_psx_image(psx_image_operator* op, const psx_image* image, - image_writer_fn func, void* param, float quality) + image_writer_fn func, void* param, float quality) { int i; int ret = S_OK; psx_image_header header; - if (!op->write_header_info || !op->encode_image_data) + if (!op->write_header_info || !op->encode_image_data) { return S_NOT_SUPPORT; + } - if (op->write_header_info(image, func, param, quality, &header) != 0) + if (op->write_header_info(image, func, param, quality, &header) != 0) { return S_NOT_SUPPORT; + } for (i = 0; i < header.frames; i++) { - if (op->encode_image_data(&header, image, &image->frames[i], i, image->frames[i].data, image->frames[i].size, &ret) != 0) + if (op->encode_image_data(&header, image, &image->frames[i], i, image->frames[i].data, image->frames[i].size, &ret) != 0) { break; + } } - if (op->release_write_header_info) + if (op->release_write_header_info) { op->release_write_header_info(&header); + } return ret; } psx_image* PICAPI psx_image_load_from_memory(const ps_byte* data, size_t length, int* err_code) { struct image_coder_node* node = NULL; - psx_image * image = NULL; + psx_image* image = NULL; if (!data || !length) { - if (err_code) + if (err_code) { *err_code = S_BAD_PARAMS; + } return NULL; } if (!g_modules) { - if (err_code) + if (err_code) { *err_code = S_INIT_FAILURE; + } return NULL; } node = get_first_operator(g_modules, data, length); if (!node || !node->op) { - if (err_code) + if (err_code) { *err_code = S_NOT_SUPPORT; + } return NULL; } while ((image = load_psx_image(node->op, data, length)) == NULL) { node = get_next_operator(g_modules, node, data, length); if (!node || !node->op) { - if (err_code) + if (err_code) { *err_code = S_NOT_SUPPORT; + } return NULL; } } - if (err_code) + if (err_code) { *err_code = S_OK; + } return image; } @@ -357,20 +390,24 @@ int PICAPI psx_image_save(const psx_image* image, image_writer_fn func, void* pa { struct image_coder_node* node = NULL; int ret = S_OK; - if (!image || !func || !type || quality <= 0.0f) + if (!image || !func || !type || quality <= 0.0f) { return S_BAD_PARAMS; + } - if (!g_modules) + if (!g_modules) { return S_INIT_FAILURE; + } node = get_first_operator_by_name(g_modules, type); - if (!node || !node->op) + if (!node || !node->op) { return S_NOT_SUPPORT; + } while ((ret = save_psx_image(node->op, image, func, param, quality)) != S_OK) { node = get_next_operator_by_name(g_modules, node, type); - if (!node || !node->op) + if (!node || !node->op) { return S_NOT_SUPPORT; + } } return ret; } @@ -378,10 +415,11 @@ int PICAPI psx_image_save(const psx_image* image, image_writer_fn func, void* pa static int file_writer(void* param, const ps_byte* data, size_t len) { const pchar* path = (const pchar*)param; - if (_file_write(path, (unsigned char*)data, len) == 0) + if (_file_write(path, (unsigned char*)data, len) == 0) { return S_OK; - else + } else { return S_FAILURE; + } } int PICAPI psx_image_save_to_file(const psx_image* image, const char* name, const char* type, float quality) @@ -389,11 +427,13 @@ int PICAPI psx_image_save_to_file(const psx_image* image, const char* name, cons int ret; pchar* file_name; - if (!image || !name || !type || quality <= 0.0f) + if (!image || !name || !type || quality <= 0.0f) { return S_BAD_PARAMS; + } file_name = pstring_create(name, NULL); - if (_file_exists(file_name) == 0) + if (_file_exists(file_name) == 0) { _file_remove(file_name); // remove old file. + } ret = psx_image_save(image, file_writer, (void*)file_name, type, quality); free(file_name); return ret; @@ -402,13 +442,15 @@ int PICAPI psx_image_save_to_file(const psx_image* image, const char* name, cons int PICAPI psx_image_destroy(psx_image* image) { size_t i; - if (!image) + if (!image) { return S_BAD_PARAMS; + } for (i = 0; i < image->num_frames; i++) { // free buffer pixels data and ps_image object. - if (image->frames[i].img) + if (image->frames[i].img) { ps_image_unref(image->frames[i].img); + } free(image->frames[i].data); } @@ -416,4 +458,3 @@ int PICAPI psx_image_destroy(psx_image* image) free(image); return S_OK; } - diff --git a/ext/image_coders/psx_image_modules.c b/ext/image_coders/psx_image_modules.c index 029a1ed..8373ce2 100644 --- a/ext/image_coders/psx_image_modules.c +++ b/ext/image_coders/psx_image_modules.c @@ -27,10 +27,7 @@ int modules_init(struct image_modules_mgr* mgr) memset(modules_dir, 0, sizeof(pchar) * PATH_MAX); //init coder list. - if (list_init(&(mgr->coders)) != 0){ - fprintf(stderr, "internal error!\n"); - return -1; - } + list_init(&(mgr->coders)); dir_path = _module_get_modules_dir(modules_dir, PATH_MAX); if (!dir_path) { @@ -49,7 +46,7 @@ int modules_init(struct image_modules_mgr* mgr) _module_get_modules(dir_path, mod_paths, nums); for (i = 0, n = 0; i < nums; i++) { - pchar * ps = mod_paths[i]; + pchar* ps = mod_paths[i]; module_handle h = _module_load(ps); // init module func = _module_get_symbol(h, "psx_image_module_init"); @@ -76,8 +73,9 @@ void modules_destroy(struct image_modules_mgr* mgr) for (i = 0; i < mgr->num_modules; i++) { mod_func func = _module_get_symbol(mgr->modules[i].handle, "psx_image_module_shutdown"); - if (func) + if (func) { func(); // call module deinit + } _module_unload(mgr->modules[i].handle); free(mgr->modules[i].path); } @@ -91,8 +89,9 @@ struct image_coder_node* get_first_operator(struct image_modules_mgr* mgr, const list_for_each(&(mgr->coders), ptr) { entry = (struct image_coder_node*)ptr; - if (memcmp(data + entry->magic_offset, entry->magic_hdr, entry->magic_len) == 0) + if (memcmp(data + entry->magic_offset, entry->magic_hdr, entry->magic_len) == 0) { break; + } entry = NULL; } @@ -106,8 +105,9 @@ struct image_coder_node* get_first_operator_by_name(struct image_modules_mgr* mg list_for_each(&(mgr->coders), ptr) { entry = (struct image_coder_node*)ptr; - if (strncmp(type, entry->type_name, strlen(entry->type_name)) == 0) + if (strncmp(type, entry->type_name, strlen(entry->type_name)) == 0) { break; + } entry = NULL; } @@ -121,8 +121,9 @@ struct image_coder_node* get_next_operator(struct image_modules_mgr* mgr, struct list_for_each_start_with(&(mgr->coders), node, ptr) { entry = (struct image_coder_node*)ptr; - if (memcmp(data + entry->magic_offset, entry->magic_hdr, entry->magic_len) == 0) + if (memcmp(data + entry->magic_offset, entry->magic_hdr, entry->magic_len) == 0) { break; + } entry = NULL; } @@ -136,8 +137,9 @@ struct image_coder_node* get_next_operator_by_name(struct image_modules_mgr* mgr list_for_each_start_with(&(mgr->coders), node, ptr) { entry = (struct image_coder_node*)ptr; - if (strncmp(type, entry->type_name, strlen(entry->type_name)) == 0) + if (strncmp(type, entry->type_name, strlen(entry->type_name)) == 0) { break; + } entry = NULL; } @@ -146,7 +148,7 @@ struct image_coder_node* get_next_operator_by_name(struct image_modules_mgr* mgr static char* copy_magic(const char* str, size_t len) { - char* dst = (char*)calloc(len+1, sizeof(char)); //malloc and clear + char* dst = (char*)calloc(len + 1, sizeof(char)); //malloc and clear memcpy(dst, str, len); return dst; } @@ -158,12 +160,14 @@ int psx_image_register_operator(const char* type, const ps_byte* header_magic, struct list_hdr* ptr = NULL; struct image_coder_node* entry = NULL; - if (!type || !header_magic || !magic_len || !coder) + if (!type || !header_magic || !magic_len || !coder) { return S_BAD_PARAMS; + } mgr = _get_modules(); - if (!mgr) + if (!mgr) { return S_INIT_FAILURE; + } list_for_each(&(mgr->coders), ptr) { entry = (struct image_coder_node*)ptr; @@ -184,12 +188,13 @@ int psx_image_register_operator(const char* type, const ps_byte* header_magic, new_entry->type_name = strdup(type); new_entry->op = coder; - if (level == PRIORITY_MASTER) + if (level == PRIORITY_MASTER) { list_add_tail(entry, new_entry); - else if (level == PRIORITY_EXTENTED) + } else if (level == PRIORITY_EXTENTED) { list_add_tail(&(mgr->coders), new_entry); - else + } else { list_add(entry, new_entry); + } } else { entry = (struct image_coder_node*)calloc(1, sizeof(struct image_coder_node)); @@ -207,27 +212,30 @@ int psx_image_register_operator(const char* type, const ps_byte* header_magic, return S_OK; } - int psx_image_unregister_operator(psx_image_operator* coder) { struct image_modules_mgr* mgr = NULL; struct list_hdr* ptr = NULL; struct image_coder_node* entry = NULL; - if (!coder) + if (!coder) { return S_BAD_PARAMS; + } mgr = _get_modules(); - if (!mgr) + if (!mgr) { return S_INIT_FAILURE; + } - if (list_empty(&(mgr->coders)) == 0) + if (list_empty(&(mgr->coders)) == 0) { return S_OK; + } list_for_each(&(mgr->coders), ptr) { entry = (struct image_coder_node*)ptr; - if (entry && (entry->op == coder)) + if (entry && (entry->op == coder)) { break; + } entry = NULL; } @@ -241,5 +249,3 @@ int psx_image_unregister_operator(psx_image_operator* coder) } return S_OK; } - - diff --git a/ext/image_coders/psx_list.h b/ext/image_coders/psx_list.h deleted file mode 100644 index 3a156ce..0000000 --- a/ext/image_coders/psx_list.h +++ /dev/null @@ -1,82 +0,0 @@ -/* Picasso - a vector graphics library - * - * Copyright (C) 2016 Zhang Ji Peng - * Contact: onecoolx@gmail.com - */ - -#ifndef _PSX_LIST_H_ -#define _PSX_LIST_H_ - -#if defined(__GNUC__) - #define INLINE inline -#elif defined(_MSC_VER) - #define INLINE __inline -#else - #define INLINE -#endif - -struct list_hdr { - struct list_hdr* next; - struct list_hdr* prev; -}; - -/* all function return 0 is success or true, -1 is fail or false. */ - -static INLINE int list_init(struct list_hdr* head) -{ - head->next = head; - head->prev = head; - return 0; -} - -static INLINE int list_empty(const struct list_hdr* head) -{ - return head->next == head ? 0 : -1; -} - -#define list_add(head, value) \ - list_add_entry((struct list_hdr*)(head), (struct list_hdr*)(value)) - -static INLINE int list_add_entry(struct list_hdr* head, struct list_hdr* value) -{ - register struct list_hdr* next = head->next; - - next->prev = value; - value->next = next; - value->prev = head; - head->next = value; - return 0; -} - -#define list_add_tail(head, value) \ - list_add_tail_entry((struct list_hdr*)(head), (struct list_hdr*)(value)) - -static INLINE int list_add_tail_entry(struct list_hdr* head, struct list_hdr* value) -{ - register struct list_hdr* prev = head->prev; - - head->prev = value; - value->next = head; - value->prev = prev; - prev->next = value; - return 0; -} - -#define list_remove(entry) \ - list_remove_entry((struct list_hdr*)(entry)) - -static INLINE int list_remove_entry(struct list_hdr* entry) -{ - entry->next->prev = entry->prev; - entry->prev->next = entry->next; - entry->next = entry->prev = NULL; - return 0; -} - -#define list_for_each(head, iterator) \ - for ((iterator) = (head)->next; (iterator) != (head); (iterator) = (iterator)->next) - -#define list_for_each_start_with(head, start, iterator) \ - for ((iterator) = ((struct list_hdr*)(start))->next; (iterator) != (head); (iterator) = (iterator)->next) - -#endif /*_PSX_LIST_H_*/ diff --git a/ext/image_coders/webp/webp_module.c b/ext/image_coders/webp/webp_module.c index 4ebd0ac..1f1d0bf 100644 --- a/ext/image_coders/webp/webp_module.c +++ b/ext/image_coders/webp/webp_module.c @@ -15,7 +15,7 @@ #include "psx_image_io.h" #include "psx_color_convert.h" #if defined(WIN32) && defined(_MSC_VER) -#include + #include #endif #include "webp/decode.h" #include "webp/encode.h" @@ -25,7 +25,7 @@ struct webp_image_ctx { WebPDecoderConfig dconfig; WebPDecBuffer* output_buffer; uint8_t* data; - size_t data_len; + size_t data_len; // write WebPConfig econfig; WebPPicture pic; @@ -43,12 +43,12 @@ static int read_webp_info(const ps_byte* data, size_t len, psx_image_header* hea if (!ctx) { return -1; // out of memory. } - + if (!WebPInitDecoderConfig(&ctx->dconfig)) { free(ctx); return -1; } - + if (ctx->dconfig.input.has_animation) { free(ctx);// not support animation return -1; @@ -62,7 +62,7 @@ static int read_webp_info(const ps_byte* data, size_t len, psx_image_header* hea free(ctx); return -1; } - + if (bitstream->has_alpha) { color_mode = MODE_RGBA; } else { @@ -95,7 +95,7 @@ static int decode_webp_data(psx_image_header* header, const psx_image* image, ps if (status != VP8_STATUS_OK) { return -1; } - + stride = ctx->output_buffer->u.RGBA.stride; for (y = 0; y < ctx->output_buffer->height; y++) { @@ -118,38 +118,38 @@ static int release_read_webp_info(psx_image_header* header) static int get_bpp(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 4; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 3; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 2; - default: - return 4; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 4; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 3; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 2; + default: + return 4; } } static int get_depth(ps_color_format fmt) { switch (fmt) { - case COLOR_FORMAT_RGBA: - case COLOR_FORMAT_BGRA: - case COLOR_FORMAT_ARGB: - case COLOR_FORMAT_ABGR: - return 32; - case COLOR_FORMAT_RGB: - case COLOR_FORMAT_BGR: - return 24; - case COLOR_FORMAT_RGB565: - case COLOR_FORMAT_RGB555: - return 16; - default: - return 32; + case COLOR_FORMAT_RGBA: + case COLOR_FORMAT_BGRA: + case COLOR_FORMAT_ARGB: + case COLOR_FORMAT_ABGR: + return 32; + case COLOR_FORMAT_RGB: + case COLOR_FORMAT_BGR: + return 24; + case COLOR_FORMAT_RGB565: + case COLOR_FORMAT_RGB555: + return 16; + default: + return 32; } } @@ -207,8 +207,9 @@ static int webp_convert_32bit(psx_image_header* header, const ps_byte* buffer, s struct webp_image_ctx* ctx = (struct webp_image_ctx*)header->priv; - if (!WebPPictureAlloc(&ctx->pic)) + if (!WebPPictureAlloc(&ctx->pic)) { return 0; + } if (is_argb) { for (y = 0; y < height; ++y) { @@ -248,26 +249,27 @@ static int webp_convert_16bit(psx_image_header* header, const ps_byte* buffer, s struct webp_image_ctx* ctx = (struct webp_image_ctx*)header->priv; - if (!WebPPictureAlloc(&ctx->pic)) + if (!WebPPictureAlloc(&ctx->pic)) { return 0; + } for (y = 0; y < height; ++y) { uint32_t* const dst = &ctx->pic.argb[y * ctx->pic.argb_stride]; const int offset = y * header->pitch; - for (x = 0; x < width; ++x) { - uint8_t cbuf[4] = {0}; - uint8_t* argb = (uint8_t*)dst + x * 4; - const uint8_t* pix = buffer + offset + x * header->bpp; - if (is_rgb565) { - _rgb565_to_rgb(cbuf, pix, 1); - } else { - _rgb555_to_rgb(cbuf, pix, 1); - } - argb[3] = 0xFF; - argb[2] = cbuf[0]; - argb[1] = cbuf[1]; - argb[0] = cbuf[2]; + for (x = 0; x < width; ++x) { + uint8_t cbuf[4] = {0}; + uint8_t* argb = (uint8_t*)dst + x * 4; + const uint8_t* pix = buffer + offset + x * header->bpp; + if (is_rgb565) { + _rgb565_to_rgb(cbuf, pix, 1); + } else { + _rgb555_to_rgb(cbuf, pix, 1); } + argb[3] = 0xFF; + argb[2] = cbuf[0]; + argb[1] = cbuf[1]; + argb[0] = cbuf[2]; + } } return 1; } @@ -304,13 +306,13 @@ static int encode_webp_data(psx_image_header* header, const psx_image* image, ps ok = webp_convert_16bit(header, buffer, buffer_len, 0); // 1 is rgb555 break; default: - if (ret) *ret = S_NOT_SUPPORT; + if (ret) { *ret = S_NOT_SUPPORT; } return -1; } ok = WebPEncode(&ctx->econfig, &ctx->pic); if (!ok) { - if (ret) *ret = S_OUT_OF_MEMORY; + if (ret) { *ret = S_OUT_OF_MEMORY; } return -1; } return 0; @@ -324,7 +326,7 @@ static int release_write_webp_info(psx_image_header* header) return 0; } -static psx_image_operator * webp_coder = NULL; +static psx_image_operator* webp_coder = NULL; static module_handle lib_image = INVALID_HANDLE; typedef int (*register_func)(const char*, const ps_byte*, size_t, size_t, psx_priority_level, psx_image_operator*); @@ -334,11 +336,12 @@ typedef int (*unregister_func)(psx_image_operator*); static wchar_t g_path[MAX_PATH]; static wchar_t* get_library_path(void) { - wchar_t *p = 0; + wchar_t* p = 0; memset(g_path, 0, sizeof(wchar_t) * MAX_PATH); GetModuleFileName(NULL, g_path, MAX_PATH); p = wcsrchr(g_path, '\\'); - p++; *p = 0; + p++; + *p = 0; lstrcat(g_path, L"psx_image.dll"); return g_path; } @@ -353,16 +356,19 @@ void psx_image_module_init(void) #else lib_image = _module_load("libpsx_image.so"); #endif - if (lib_image == INVALID_HANDLE) + if (lib_image == INVALID_HANDLE) { return; + } func = _module_get_symbol(lib_image, "psx_image_register_operator"); - if (!func) + if (!func) { return; + } webp_coder = (psx_image_operator*)calloc(1, sizeof(psx_image_operator)); - if (!webp_coder) + if (!webp_coder) { return; + } webp_coder->read_header_info = read_webp_info; webp_coder->decode_image_data = decode_webp_data; @@ -387,8 +393,9 @@ void psx_image_module_shutdown(void) } } - if (lib_image != INVALID_HANDLE) + if (lib_image != INVALID_HANDLE) { _module_unload(lib_image); + } } const char* psx_image_module_get_string(int idx) diff --git a/tools/code_format.sh b/tools/code_format.sh index 9964649..29b7b0b 100755 --- a/tools/code_format.sh +++ b/tools/code_format.sh @@ -1,5 +1,5 @@ #!/bin/sh -astyle --options=code_style.ini --recursive "../src/*.cpp,*.h" -astyle --options=code_style.ini --recursive "../ext/*.cpp,*.h" +astyle --options=code_style.ini --recursive "../src/*.cpp,*.c,*.h" +astyle --options=code_style.ini --recursive "../ext/*.cpp,*.c,*.h" astyle --options=code_style.ini --recursive "../include/*.h"