#include "mm_util_private.h"
#define MM_UTIL_PNG_BYTES_TO_CHECK 4
-#define MM_UTIL_PROGRESSIVE_BYTES_DEFAULT 1024
#define MM_UTIL_BIT_DEPTH_8 8
#define png_infopp_NULL (png_infopp)NULL
-#define png_voidp_NULL (png_voidp)NULL
typedef struct {
void *mem;
return MM_UTIL_ERROR_NONE;
}
-static void user_info_callback(png_structp png_ptr, png_infop info)
-{
- mm_util_png_data *decoded = (mm_util_png_data *) png_get_io_ptr(png_ptr);
-
- mm_util_debug("user_info_callback");
- __dec_set_prop(decoded, png_ptr, info);
-
- /* Allocate output buffer */
- decoded->data = (void *)png_malloc(png_ptr, sizeof(png_bytep) * decoded->size);
-}
-
-static void user_endrow_callback(png_structp png_ptr, png_bytep new_row, png_uint_32 row_num, int pass)
-{
- mm_util_png_data *decoded = (mm_util_png_data *) png_get_io_ptr(png_ptr);
-
- if (new_row) {
- png_uint_32 offset = row_num * decoded->png.rowbytes;
-
- png_progressive_combine_row(png_ptr, decoded->data + offset, new_row);
- }
-}
-
-static void user_end_callback(png_structp png_ptr, png_infop info)
-{
- mm_util_debug("and we are done reading this image");
-}
-
-static int __read_png_progressive(mm_util_png_data *decoded, FILE * fp, void *memory, unsigned long long src_size)
-{
- png_structp png_ptr;
- png_infop info_ptr;
- static unsigned long long old_length = 0;
-
- mm_util_debug("__read_png_progressive");
- decoded->data = NULL;
-
- png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, __user_error_fn, __user_warning_fn);
-
- if (png_ptr == NULL) {
- mm_util_error("could not create read struct");
- mm_util_safe_fclose(fp);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- info_ptr = png_create_info_struct(png_ptr);
- if (info_ptr == NULL) {
- mm_util_error("could not create info struct");
- mm_util_safe_fclose(fp);
- png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- if (setjmp(png_jmpbuf(png_ptr))) {
- mm_util_error("setjmp called due to internal libpng error");
- if (decoded->data) {
- png_free(png_ptr, decoded->data);
- decoded->data = NULL;
- mm_util_debug("free data");
- }
- png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- mm_util_safe_fclose(fp);
- return MM_UTIL_ERROR_INVALID_OPERATION;
- }
-
- png_set_progressive_read_fn(png_ptr, decoded, user_info_callback, user_endrow_callback, user_end_callback);
-
- while (1) {
- char buf[decoded->png.progressive_bytes];
- unsigned long long bytes_read = 0;
-
- if (fp) {
- if (!(bytes_read = fread(buf, 1, decoded->png.progressive_bytes, fp)))
- break;
- } else if (memory) {
- unsigned long long length = src_size - old_length;
-
- if (length == 0)
- break;
- else if (length >= decoded->png.progressive_bytes)
- bytes_read = decoded->png.progressive_bytes;
- else
- bytes_read = length;
-
- memcpy(buf, memory + old_length, bytes_read);
-
- old_length += bytes_read;
- } else
- break;
- png_process_data(png_ptr, info_ptr, (png_bytep) buf, bytes_read);
- };
-
- png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
- mm_util_safe_fclose(fp);
-
- return MM_UTIL_ERROR_NONE;
-}
-
int mm_util_decode_from_png_file(mm_util_png_data *decoded, const char *fpath)
{
int ret = MM_UTIL_ERROR_NONE;
return ret;
}
- if (fp) {
- if (decoded->png.progressive)
- ret = __read_png_progressive(decoded, fp, NULL, 0);
- else
- ret = __read_png(decoded, fp, NULL);
- }
+ if (fp)
+ ret = __read_png(decoded, fp, NULL);
return ret;
}
int ret = MM_UTIL_ERROR_NONE;
mm_util_debug("mm_util_decode_from_memory");
- if (decoded->png.progressive)
- ret = __read_png_progressive(decoded, NULL, memory, src_size);
- else
- ret = __read_png(decoded, NULL, memory);
+
+ ret = __read_png(decoded, NULL, memory);
return ret;
}
void mm_util_init_decode_png(mm_util_png_data *data)
{
mm_util_debug("mm_util_init_decode_png");
- data->png.progressive = 0;
- data->png.progressive_bytes = MM_UTIL_PROGRESSIVE_BYTES_DEFAULT;
data->png.transform = PNG_TRANSFORM_IDENTITY;
}