added png to and from memory operations

This commit is contained in:
2025-06-11 18:53:09 -04:00
parent 91746a838c
commit 68c0d5e460
34 changed files with 720 additions and 7 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

291
dependencies/linux64/include/jerror.h vendored Normal file
View File

@ -0,0 +1,291 @@
/*
* jerror.h
*
* Copyright (C) 1994-1997, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file defines the error and message codes for the JPEG library.
* Edit this file to add new codes, or to translate the message strings to
* some other language.
* A set of error-reporting macros are defined too. Some applications using
* the JPEG library may wish to include this file to get the error codes
* and/or the macros.
*/
/*
* To define the enum list of message codes, include this file without
* defining macro JMESSAGE. To create a message string table, include it
* again with a suitable JMESSAGE definition (see jerror.c for an example).
*/
#ifndef JMESSAGE
#ifndef JERROR_H
/* First time through, define the enum list */
#define JMAKE_ENUM_LIST
#else
/* Repeated inclusions of this file are no-ops unless JMESSAGE is defined */
#define JMESSAGE(code,string)
#endif /* JERROR_H */
#endif /* JMESSAGE */
#ifdef JMAKE_ENUM_LIST
typedef enum {
#define JMESSAGE(code,string) code ,
#endif /* JMAKE_ENUM_LIST */
JMESSAGE(JMSG_NOMESSAGE, "Bogus message code %d") /* Must be first entry! */
/* For maintenance convenience, list is alphabetical by message code name */
JMESSAGE(JERR_ARITH_NOTIMPL,
"Sorry, there are legal restrictions on arithmetic coding")
JMESSAGE(JERR_BAD_ALIGN_TYPE, "ALIGN_TYPE is wrong, please fix")
JMESSAGE(JERR_BAD_ALLOC_CHUNK, "MAX_ALLOC_CHUNK is wrong, please fix")
JMESSAGE(JERR_BAD_BUFFER_MODE, "Bogus buffer control mode")
JMESSAGE(JERR_BAD_COMPONENT_ID, "Invalid component ID %d in SOS")
JMESSAGE(JERR_BAD_DCT_COEF, "DCT coefficient out of range")
JMESSAGE(JERR_BAD_DCTSIZE, "IDCT output block size %d not supported")
JMESSAGE(JERR_BAD_HUFF_TABLE, "Bogus Huffman table definition")
JMESSAGE(JERR_BAD_IN_COLORSPACE, "Bogus input colorspace")
JMESSAGE(JERR_BAD_J_COLORSPACE, "Bogus JPEG colorspace")
JMESSAGE(JERR_BAD_LENGTH, "Bogus marker length")
JMESSAGE(JERR_BAD_LIB_VERSION,
"Wrong JPEG library version: library is %d, caller expects %d")
JMESSAGE(JERR_BAD_MCU_SIZE, "Sampling factors too large for interleaved scan")
JMESSAGE(JERR_BAD_POOL_ID, "Invalid memory pool code %d")
JMESSAGE(JERR_BAD_PRECISION, "Unsupported JPEG data precision %d")
JMESSAGE(JERR_BAD_PROGRESSION,
"Invalid progressive parameters Ss=%d Se=%d Ah=%d Al=%d")
JMESSAGE(JERR_BAD_PROG_SCRIPT,
"Invalid progressive parameters at scan script entry %d")
JMESSAGE(JERR_BAD_SAMPLING, "Bogus sampling factors")
JMESSAGE(JERR_BAD_SCAN_SCRIPT, "Invalid scan script at entry %d")
JMESSAGE(JERR_BAD_STATE, "Improper call to JPEG library in state %d")
JMESSAGE(JERR_BAD_STRUCT_SIZE,
"JPEG parameter struct mismatch: library thinks size is %u, caller expects %u")
JMESSAGE(JERR_BAD_VIRTUAL_ACCESS, "Bogus virtual array access")
JMESSAGE(JERR_BUFFER_SIZE, "Buffer passed to JPEG library is too small")
JMESSAGE(JERR_CANT_SUSPEND, "Suspension not allowed here")
JMESSAGE(JERR_CCIR601_NOTIMPL, "CCIR601 sampling not implemented yet")
JMESSAGE(JERR_COMPONENT_COUNT, "Too many color components: %d, max %d")
JMESSAGE(JERR_CONVERSION_NOTIMPL, "Unsupported color conversion request")
JMESSAGE(JERR_DAC_INDEX, "Bogus DAC index %d")
JMESSAGE(JERR_DAC_VALUE, "Bogus DAC value 0x%x")
JMESSAGE(JERR_DHT_INDEX, "Bogus DHT index %d")
JMESSAGE(JERR_DQT_INDEX, "Bogus DQT index %d")
JMESSAGE(JERR_EMPTY_IMAGE, "Empty JPEG image (DNL not supported)")
JMESSAGE(JERR_EMS_READ, "Read from EMS failed")
JMESSAGE(JERR_EMS_WRITE, "Write to EMS failed")
JMESSAGE(JERR_EOI_EXPECTED, "Didn't expect more than one scan")
JMESSAGE(JERR_FILE_READ, "Input file read error")
JMESSAGE(JERR_FILE_WRITE, "Output file write error --- out of disk space?")
JMESSAGE(JERR_FRACT_SAMPLE_NOTIMPL, "Fractional sampling not implemented yet")
JMESSAGE(JERR_HUFF_CLEN_OVERFLOW, "Huffman code size table overflow")
JMESSAGE(JERR_HUFF_MISSING_CODE, "Missing Huffman code table entry")
JMESSAGE(JERR_IMAGE_TOO_BIG, "Maximum supported image dimension is %u pixels")
JMESSAGE(JERR_INPUT_EMPTY, "Empty input file")
JMESSAGE(JERR_INPUT_EOF, "Premature end of input file")
JMESSAGE(JERR_MISMATCHED_QUANT_TABLE,
"Cannot transcode due to multiple use of quantization table %d")
JMESSAGE(JERR_MISSING_DATA, "Scan script does not transmit all data")
JMESSAGE(JERR_MODE_CHANGE, "Invalid color quantization mode change")
JMESSAGE(JERR_NOTIMPL, "Not implemented yet")
JMESSAGE(JERR_NOT_COMPILED, "Requested feature was omitted at compile time")
JMESSAGE(JERR_NO_BACKING_STORE, "Backing store not supported")
JMESSAGE(JERR_NO_HUFF_TABLE, "Huffman table 0x%02x was not defined")
JMESSAGE(JERR_NO_IMAGE, "JPEG datastream contains no image")
JMESSAGE(JERR_NO_QUANT_TABLE, "Quantization table 0x%02x was not defined")
JMESSAGE(JERR_NO_SOI, "Not a JPEG file: starts with 0x%02x 0x%02x")
JMESSAGE(JERR_OUT_OF_MEMORY, "Insufficient memory (case %d)")
JMESSAGE(JERR_QUANT_COMPONENTS,
"Cannot quantize more than %d color components")
JMESSAGE(JERR_QUANT_FEW_COLORS, "Cannot quantize to fewer than %d colors")
JMESSAGE(JERR_QUANT_MANY_COLORS, "Cannot quantize to more than %d colors")
JMESSAGE(JERR_SOF_DUPLICATE, "Invalid JPEG file structure: two SOF markers")
JMESSAGE(JERR_SOF_NO_SOS, "Invalid JPEG file structure: missing SOS marker")
JMESSAGE(JERR_SOF_UNSUPPORTED, "Unsupported JPEG process: SOF type 0x%02x")
JMESSAGE(JERR_SOI_DUPLICATE, "Invalid JPEG file structure: two SOI markers")
JMESSAGE(JERR_SOS_NO_SOF, "Invalid JPEG file structure: SOS before SOF")
JMESSAGE(JERR_TFILE_CREATE, "Failed to create temporary file %s")
JMESSAGE(JERR_TFILE_READ, "Read failed on temporary file")
JMESSAGE(JERR_TFILE_SEEK, "Seek failed on temporary file")
JMESSAGE(JERR_TFILE_WRITE,
"Write failed on temporary file --- out of disk space?")
JMESSAGE(JERR_TOO_LITTLE_DATA, "Application transferred too few scanlines")
JMESSAGE(JERR_UNKNOWN_MARKER, "Unsupported marker type 0x%02x")
JMESSAGE(JERR_VIRTUAL_BUG, "Virtual array controller messed up")
JMESSAGE(JERR_WIDTH_OVERFLOW, "Image too wide for this implementation")
JMESSAGE(JERR_XMS_READ, "Read from XMS failed")
JMESSAGE(JERR_XMS_WRITE, "Write to XMS failed")
JMESSAGE(JMSG_COPYRIGHT, JCOPYRIGHT)
JMESSAGE(JMSG_VERSION, JVERSION)
JMESSAGE(JTRC_16BIT_TABLES,
"Caution: quantization tables are too coarse for baseline JPEG")
JMESSAGE(JTRC_ADOBE,
"Adobe APP14 marker: version %d, flags 0x%04x 0x%04x, transform %d")
JMESSAGE(JTRC_APP0, "Unknown APP0 marker (not JFIF), length %u")
JMESSAGE(JTRC_APP14, "Unknown APP14 marker (not Adobe), length %u")
JMESSAGE(JTRC_DAC, "Define Arithmetic Table 0x%02x: 0x%02x")
JMESSAGE(JTRC_DHT, "Define Huffman Table 0x%02x")
JMESSAGE(JTRC_DQT, "Define Quantization Table %d precision %d")
JMESSAGE(JTRC_DRI, "Define Restart Interval %u")
JMESSAGE(JTRC_EMS_CLOSE, "Freed EMS handle %u")
JMESSAGE(JTRC_EMS_OPEN, "Obtained EMS handle %u")
JMESSAGE(JTRC_EOI, "End Of Image")
JMESSAGE(JTRC_HUFFBITS, " %3d %3d %3d %3d %3d %3d %3d %3d")
JMESSAGE(JTRC_JFIF, "JFIF APP0 marker: version %d.%02d, density %dx%d %d")
JMESSAGE(JTRC_JFIF_BADTHUMBNAILSIZE,
"Warning: thumbnail image size does not match data length %u")
JMESSAGE(JTRC_JFIF_EXTENSION,
"JFIF extension marker: type 0x%02x, length %u")
JMESSAGE(JTRC_JFIF_THUMBNAIL, " with %d x %d thumbnail image")
JMESSAGE(JTRC_MISC_MARKER, "Miscellaneous marker 0x%02x, length %u")
JMESSAGE(JTRC_PARMLESS_MARKER, "Unexpected marker 0x%02x")
JMESSAGE(JTRC_QUANTVALS, " %4u %4u %4u %4u %4u %4u %4u %4u")
JMESSAGE(JTRC_QUANT_3_NCOLORS, "Quantizing to %d = %d*%d*%d colors")
JMESSAGE(JTRC_QUANT_NCOLORS, "Quantizing to %d colors")
JMESSAGE(JTRC_QUANT_SELECTED, "Selected %d colors for quantization")
JMESSAGE(JTRC_RECOVERY_ACTION, "At marker 0x%02x, recovery action %d")
JMESSAGE(JTRC_RST, "RST%d")
JMESSAGE(JTRC_SMOOTH_NOTIMPL,
"Smoothing not supported with nonstandard sampling ratios")
JMESSAGE(JTRC_SOF, "Start Of Frame 0x%02x: width=%u, height=%u, components=%d")
JMESSAGE(JTRC_SOF_COMPONENT, " Component %d: %dhx%dv q=%d")
JMESSAGE(JTRC_SOI, "Start of Image")
JMESSAGE(JTRC_SOS, "Start Of Scan: %d components")
JMESSAGE(JTRC_SOS_COMPONENT, " Component %d: dc=%d ac=%d")
JMESSAGE(JTRC_SOS_PARAMS, " Ss=%d, Se=%d, Ah=%d, Al=%d")
JMESSAGE(JTRC_TFILE_CLOSE, "Closed temporary file %s")
JMESSAGE(JTRC_TFILE_OPEN, "Opened temporary file %s")
JMESSAGE(JTRC_THUMB_JPEG,
"JFIF extension marker: JPEG-compressed thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_PALETTE,
"JFIF extension marker: palette thumbnail image, length %u")
JMESSAGE(JTRC_THUMB_RGB,
"JFIF extension marker: RGB thumbnail image, length %u")
JMESSAGE(JTRC_UNKNOWN_IDS,
"Unrecognized component IDs %d %d %d, assuming YCbCr")
JMESSAGE(JTRC_XMS_CLOSE, "Freed XMS handle %u")
JMESSAGE(JTRC_XMS_OPEN, "Obtained XMS handle %u")
JMESSAGE(JWRN_ADOBE_XFORM, "Unknown Adobe color transform code %d")
JMESSAGE(JWRN_BOGUS_PROGRESSION,
"Inconsistent progression sequence for component %d coefficient %d")
JMESSAGE(JWRN_EXTRANEOUS_DATA,
"Corrupt JPEG data: %u extraneous bytes before marker 0x%02x")
JMESSAGE(JWRN_HIT_MARKER, "Corrupt JPEG data: premature end of data segment")
JMESSAGE(JWRN_HUFF_BAD_CODE, "Corrupt JPEG data: bad Huffman code")
JMESSAGE(JWRN_JFIF_MAJOR, "Warning: unknown JFIF revision number %d.%02d")
JMESSAGE(JWRN_JPEG_EOF, "Premature end of JPEG file")
JMESSAGE(JWRN_MUST_RESYNC,
"Corrupt JPEG data: found marker 0x%02x instead of RST%d")
JMESSAGE(JWRN_NOT_SEQUENTIAL, "Invalid SOS parameters for sequential JPEG")
JMESSAGE(JWRN_TOO_MUCH_DATA, "Application transferred too many scanlines")
#ifdef JMAKE_ENUM_LIST
JMSG_LASTMSGCODE
} J_MESSAGE_CODE;
#undef JMAKE_ENUM_LIST
#endif /* JMAKE_ENUM_LIST */
/* Zap JMESSAGE macro so that future re-inclusions do nothing by default */
#undef JMESSAGE
#ifndef JERROR_H
#define JERROR_H
/* Macros to simplify using the error and trace message stuff */
/* The first parameter is either type of cinfo pointer */
/* Fatal errors (print message and exit) */
#define ERREXIT(cinfo,code) \
((cinfo)->err->msg_code = (code), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT1(cinfo,code,p1) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT2(cinfo,code,p1,p2) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(cinfo)->err->msg_parm.i[1] = (p2), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT3(cinfo,code,p1,p2,p3) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(cinfo)->err->msg_parm.i[1] = (p2), \
(cinfo)->err->msg_parm.i[2] = (p3), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXIT4(cinfo,code,p1,p2,p3,p4) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(cinfo)->err->msg_parm.i[1] = (p2), \
(cinfo)->err->msg_parm.i[2] = (p3), \
(cinfo)->err->msg_parm.i[3] = (p4), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define ERREXITS(cinfo,code,str) \
((cinfo)->err->msg_code = (code), \
strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
(*(cinfo)->err->error_exit) ((j_common_ptr) (cinfo)))
#define MAKESTMT(stuff) do { stuff } while (0)
/* Nonfatal errors (we can keep going, but the data is probably corrupt) */
#define WARNMS(cinfo,code) \
((cinfo)->err->msg_code = (code), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS1(cinfo,code,p1) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
#define WARNMS2(cinfo,code,p1,p2) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(cinfo)->err->msg_parm.i[1] = (p2), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), -1))
/* Informational/debugging messages */
#define TRACEMS(cinfo,lvl,code) \
((cinfo)->err->msg_code = (code), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS1(cinfo,lvl,code,p1) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS2(cinfo,lvl,code,p1,p2) \
((cinfo)->err->msg_code = (code), \
(cinfo)->err->msg_parm.i[0] = (p1), \
(cinfo)->err->msg_parm.i[1] = (p2), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#define TRACEMS3(cinfo,lvl,code,p1,p2,p3) \
MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
_mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); \
(cinfo)->err->msg_code = (code); \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS4(cinfo,lvl,code,p1,p2,p3,p4) \
MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
_mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
(cinfo)->err->msg_code = (code); \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS5(cinfo,lvl,code,p1,p2,p3,p4,p5) \
MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
_mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
_mp[4] = (p5); \
(cinfo)->err->msg_code = (code); \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMS8(cinfo,lvl,code,p1,p2,p3,p4,p5,p6,p7,p8) \
MAKESTMT(int * _mp = (cinfo)->err->msg_parm.i; \
_mp[0] = (p1); _mp[1] = (p2); _mp[2] = (p3); _mp[3] = (p4); \
_mp[4] = (p5); _mp[5] = (p6); _mp[6] = (p7); _mp[7] = (p8); \
(cinfo)->err->msg_code = (code); \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)); )
#define TRACEMSS(cinfo,lvl,code,str) \
((cinfo)->err->msg_code = (code), \
strncpy((cinfo)->err->msg_parm.s, (str), JMSG_STR_PARM_MAX), \
(*(cinfo)->err->emit_message) ((j_common_ptr) (cinfo), (lvl)))
#endif /* JERROR_H */

View File

@ -118,7 +118,13 @@ AMSCIMGLIB4_API void amscimglib4_writeimage_png(const char *fname, amscimglib4_i
AMSCIMGLIB4_API void amscimglib4_readimage_jpeg(const char *fname, amscimglib4_image *img);
AMSCIMGLIB4_API void amscimglib4_writeimage_jpeg(const char *fname, amscimglib4_image *img, int quality);
//Read and write PNG files from memory buffers
AMSCIMGLIB4_API int amscimglib4_pngbytebuffer_to_img(amscimglib4_image *img, unsigned char *bytebuffer, long nbytes);
//returns length required for a bytebuffer to store img as a png
AMSCIMGLIB4_API long amscimglib4_pngbytebufferlen(amscimglib4_image *img);
AMSCIMGLIB4_API int amscimglib4_pngbytebuffer_from_img(amscimglib4_image *img, unsigned char *bytebuffer, long bufflen, long *nbytes);
#ifdef __cplusplus

View File

@ -14,7 +14,12 @@ extern "C" {
#endif
//returns the size of a file using ANSI C
unsigned int amscimglib4_filesize(FILE *fp);
unsigned long amscimglib4_filesize(FILE *fp);
int amscimglib4_readbinfile(const char *fname, unsigned char *bytes, long bufflen, long *bytesread);
int amscimglib4_writebinfile(const char *fname, unsigned char *bytes, long bufflen);
#ifdef __cplusplus
}; //end extern "C"

View File

@ -9,6 +9,9 @@ AMSCIMGLIB4_API void amscimglib4_test_init();
AMSCIMGLIB4_API void amscimglib4_test_loadsave();
AMSCIMGLIB4_API void amscimglib4_test_binreadwrite();
#ifdef __cplusplus
}; //end extern "C"
#endif

View File

@ -473,7 +473,7 @@ AMSCIMGLIB4_API void amscimglib4_writeimage(const char *fname, amscimglib4_image
}
else if(strcmp(ext,".bmp")==0)
{
amscimglib4_writeimage_select(fname,img,amscimglib4_type_png);
amscimglib4_writeimage_select(fname,img,amscimglib4_type_bmp);
}
else if(strcmp(ext,".jpg")==0||strcmp(ext,".jpeg")==0)
{

View File

@ -136,6 +136,272 @@ AMSCIMGLIB4_API void amscimglib4_writeimage_png(const char *fname, amscimglib4_i
}
//Nevermind, there is a int png_image_begin_read_from_memory(png_imagep image, png_const_voidp memory, size_t size)
// int png_image_write_to_memory (png_imagep image, void *memory,
// png_alloc_size_t * PNG_RESTRICT memory_bytes,
// int convert_to_8_bit, const void *buffer, ptrdiff_t row_stride,
// const void *colormap));
//Reads and writes png data to and from a bytebuffer in memory (for compression)
AMSCIMGLIB4_API int amscimglib4_pngbytebuffer_to_img(amscimglib4_image *img, unsigned char *bytebuffer, long nbytes)
{
int ret = amscimglib4_success;
int res;
png_image image; //control structure used by libpng
unsigned char *imgbuffer = NULL;
long z;
unsigned char R,G,B,A;
long I,J;
if(img==NULL || bytebuffer==NULL || nbytes<8)
{
ret = amscimglib4_failure;
return ret;
}
memset(&image,0,sizeof(image)); //fills memory with zeros
image.version = PNG_IMAGE_VERSION;
res = png_image_begin_read_from_memory(&image,bytebuffer,nbytes);
if(res != 0)
{
image.format = PNG_FORMAT_RGBA; //force conversion to RGBA before calling finish read
imgbuffer = (png_bytep) malloc(PNG_IMAGE_SIZE(image));
if(imgbuffer==NULL)
{
printf("amscimglib4_pngbytebuffer_to_img:: imgbuffer failed to allocate.\n");
ret = amscimglib4_failure;
png_image_free(&image);
return ret;
}
res = png_image_finish_read(
&image,
NULL, //background
imgbuffer, //buffer to fill
0, //row stride
NULL //colormap
);
if(res!=0 && imgbuffer!=NULL)
{
res = amscimglib4_image_resize(img,image.width,image.height);
if(res==amscimglib4_success)
{
for(I=0;I<image.width;I++)
{
for(J=0;J<image.height;J++)
{
z = (I+J*image.width)*4;
R = imgbuffer[z+0];
G = imgbuffer[z+1];
B = imgbuffer[z+2];
A = imgbuffer[z+3];
amscimglib4_set_pixel_RGBA(img,I,J,R,G,B,A);
}
}
}
}
if(imgbuffer!=NULL) {free(imgbuffer); imgbuffer = NULL;}
}
png_image_free(&image);
return ret;
}
AMSCIMGLIB4_API long amscimglib4_pngbytebufferlen(amscimglib4_image *img)
{
long ret = 0;
int res;
png_alloc_size_t bufflen = 0;
png_image pngimage;
pngimage.version = PNG_IMAGE_VERSION;
pngimage.format = PNG_FORMAT_RGBA;
pngimage.width = img->sizex;
pngimage.height = img->sizey;
res = png_image_write_to_memory(
&pngimage,
NULL, //memory
&bufflen, //buffer size required
0, //convert to 8 bit
img->data, //RGBA buffer
0, //row stride,
NULL //colormap
);
ret = bufflen;
return ret;
}
// int png_image_write_to_memory (png_imagep image, void *memory,
// png_alloc_size_t * PNG_RESTRICT memory_bytes,
// int convert_to_8_bit, const void *buffer, ptrdiff_t row_stride,
// const void *colormap));
AMSCIMGLIB4_API int amscimglib4_pngbytebuffer_from_img(amscimglib4_image *img, unsigned char *bytebuffer, long bufflen, long *nbyteswritten)
{
int ret = amscimglib4_success;
int res;
png_image pngimage;
png_alloc_size_t bufflen2 = bufflen;
pngimage.version = PNG_IMAGE_VERSION;
pngimage.format = PNG_FORMAT_RGBA;
pngimage.width = img->sizex;
pngimage.height = img->sizey;
res = png_image_write_to_memory(
&pngimage,
bytebuffer, //memory
&bufflen2, //buffer size
0, //convert to 8 bit
img->data, //RGBA buffer
0, //row stride,
NULL //colormap
);
if(nbyteswritten!=NULL) *nbyteswritten = bufflen2;
return ret;
}
#ifdef __cplusplus
}; //end extern "C"
#endif
#endif
// Old Code //
// Structure to hold our buffer data
// typedef struct amscimglib4_png_buffer_data {
// const unsigned char *data;
// size_t size;
// size_t offset;
// } amscimglib4_png_buffer_data;
// //fills data bytes with data from png_ptr
// void amscimglib4_pngbytes_readfn(
// png_structp png_ptr,
// png_bytep data,
// png_size_t length
// )
// {
// amscimglib4_png_buffer_data *buff = (amscimglib4_png_buffer_data*) png_get_io_ptr(png_ptr);
// if(buff->offset + length > buff->size)
// {
// //not enough data in buffer;
// return;
// }
// memcpy(data,buff->data + buff->offset, length);
// buff->offset += length;
// return;
// }
// //Reads and writes png data to and from a bytebuffer in memory (for compression)
// AMSCIMGLIB4_API int amscimglib4_pngbytebuffer_to_img(amscimglib4_image *img, unsigned char *bytebuffer, long nbytes)
// {
// // Following tutorial https://pulsarengine.com/2009/01/reading-png-images-from-memory/
// int ret = amscimglib4_success;
// if(img==NULL || bytebuffer==NULL)
// {
// ret = amscimglib4_failure;
// return ret;
// }
// if(nbytes<8)
// {
// ret = amscimglib4_failure;
// return ret;
// }
// if(!png_check_sig(bytebuffer,8))
// {
// printf("amscimglib4_pngbytebuffer_to_img - bytes do not have PNG signature.\n");
// ret = amscimglib4_failure;
// return ret;
// }
// amscimglib4_png_buffer_data buffer2;
// buffer2.data = bytebuffer;
// buffer2.size = nbytes;
// buffer2.offset = 0;
// // get PNG file info struct (memory is allocated by libpng)
// png_structp png_ptr = NULL;
// png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
// if(png_ptr == NULL)
// {
// printf("amscimglib4_pngbytebuffer_to_img - png pointer failed to allocate.\n");
// ret = amscimglib4_failure;
// return ret;
// }
// // get PNG image data info struct (memory is allocated by libpng)
// png_infop info_ptr = NULL;
// info_ptr = png_create_info_struct(png_ptr);
// if(info_ptr == NULL)
// {
// // libpng must free file info struct memory before we bail
// png_destroy_read_struct(&png_ptr, NULL, NULL);
// ret = amscimglib4_failure;
// return ret;
// }
// //This is where the magic happens. Now we must tell libpng to use a our own custom routine for retrieving the image data. To do this, we use the png_set_read_fn() routine:
// //png_set_read_fn(png_ptr, &inputStream, ReadDataFromInputStream);
// /*
// http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-5
// Input/Output in libpng is done through png_read() and png_write(), which currently just call fread() and fwrite(). The FILE * is stored in png_struct and is initialized via png_init_io(). If you wish to change the method of I/O, the library supplies callbacks that you can set through the function png_set_read_fn() and png_set_write_fn() at run time, instead of calling the png_init_io() function. These functions also provide a void pointer that can be retrieved via the function png_get_io_ptr(). For example:
// png_set_read_fn(png_structp read_ptr,
// voidp read_io_ptr, png_rw_ptr read_data_fn)
// The replacement I/O functions must have prototypes as follows:
// void user_read_data(png_structp png_ptr,
// png_bytep data, png_size_t length);
// void user_write_data(png_structp png_ptr,
// png_bytep data, png_size_t length);
// void user_flush_data(png_structp png_ptr);
// */
// png_set_read_fn(png_ptr, &buffer2, &amscimglib4_pngbytes_readfn);
// png_read_info(png_ptr, info_ptr);
// int w,h,bit_depth,color_type;
// w = png_get_image_width(png_ptr, info_ptr);
// h = png_get_image_height(png_ptr, info_ptr);
// bit_depth = png_get_bit_depth(png_ptr, info_ptr);
// color_type = png_get_color_type(png_ptr, info_ptr);
// return ret;
// }

View File

@ -66,6 +66,57 @@ AMSCIMGLIB4_API void amscimglib4_test_loadsave()
}
AMSCIMGLIB4_API void amscimglib4_test_binreadwrite()
{
int res;
unsigned char *bytebuff = NULL;
long bufflen = 10000000;
long nread = 0;
const char *fname1 = "../testimg/testpfp0.png";
const char *fname2 = "../testimg/testpfp0_bwrite.png";
const char *fname3 = "../testimg/testpfp0_bwrite2.png";
bytebuff = (unsigned char *) malloc(sizeof(unsigned char)*bufflen);
if(bytebuff==NULL)
{
printf("bytebuff failed to allocate.\n");
return;
}
amscimglib4_image *img1 = NULL;
amscimglib4_image *img2 = NULL;
amscimglib4_readbinfile(fname1,bytebuff,bufflen,&nread);
printf("Read %ld bytes into byte buffer from %s\n",(long)nread,fname1);
amscimglib4_writebinfile(fname2,bytebuff,nread);
amscimglib4_image_new(&img1,1,1);
amscimglib4_image_new(&img2,1,1);
//every so often this segfaults for some reason
res = amscimglib4_pngbytebuffer_to_img(img1,bytebuff,nread);
printf("check something: %d\n",(int)sizeof(unsigned char));
printf("Interpreted png as (%d, %d) image.\n",(int)img1->sizex,(int)img1->sizey);
memset(bytebuff,0,bufflen);
nread = amscimglib4_pngbytebufferlen(img1);
printf("To write the image to a bytebuffer, I need %ld bytes\n",nread);
res = amscimglib4_pngbytebuffer_from_img(img1,bytebuff,bufflen,&nread);
printf("Wrote %ld bytes to buffer\n.",nread);
amscimglib4_writebinfile(fname3,bytebuff,nread);
amscimglib4_image_delete(&img1);
amscimglib4_image_delete(&img2);
free(bytebuff);
return;
}
#ifdef __cplusplus
}; //end extern "C"
#endif

View File

@ -0,0 +1,10 @@
#include <amscimglib4/amscimglib4.h>
#include <amscimglib4/amscimglib4_intl.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}; //end extern "C"
#endif

View File

@ -6,20 +6,100 @@ extern "C" {
#endif
//returns the size of a file using ANSI C
unsigned int amscimglib4_filesize(FILE *fp)
unsigned long amscimglib4_filesize(FILE *fp)
{
unsigned int ret = 0;
unsigned long ret = 0L;
fseek(fp,0L,SEEK_END);
ret = ftell(fp); //find the size of the file
fseek(fp,0L,SEEK_SET);
return ret;
}
int amscimglib4_readbinfile(const char *fname, unsigned char *bytes, long bufflen, long *bytesread)
{
FILE *fp = NULL;
int ret = amscimglib4_success;
long filesize;
long I;
static const long chunklen = 4096;
long clen;
long lres;
long res;
if(fname==NULL)
{
ret = amscimglib4_failure;
return ret;
}
fp = fopen(fname,"rb");
if(fp==NULL)
{
printf("amscimglib4_readbinfile:: could not open %s for reading.\n",fname);
ret = amscimglib4_failure;
return ret;
}
if(bytes==NULL || bufflen<=0)
{
ret = amscimglib4_failure;
return ret;
}
filesize = (long) amscimglib4_filesize(fp);
I = 0;
while(I<bufflen && I<filesize)
{
//load binary file in chunklen sized reads
clen = filesize - I;
clen = (clen>(bufflen-I)) ? bufflen-I : clen;
clen = (clen>chunklen) ? chunklen : clen;
res = fread(&(bytes[I]),sizeof(unsigned char),clen,fp);
I = I + clen;
}
if(bytesread!=NULL) *bytesread = I;
fclose(fp);
return ret;
}
int amscimglib4_writebinfile(const char *fname, unsigned char *bytes, long bufflen)
{
int ret = amscimglib4_success;
FILE *fp = NULL;
if(fname==NULL)
{
ret = amscimglib4_failure;
return ret;
}
fp = fopen(fname,"w+b");
if(fp==NULL)
{
printf("amscimglib4_writebinfile:: could not open %s for writing.\n",fname);
ret = amscimglib4_failure;
return ret;
}
fwrite(bytes,sizeof(unsigned char),bufflen,fp);
fclose(fp);
return ret;
}
AMSCIMGLIB4_API int amscimglib4_magic_type(const char *fname)
{
FILE *fp = NULL;
int ret = amscimglib4_type_unknown; //unidentified type
unsigned char magicbuff[8];
size_t res;
fp = fopen(fname,"rb");
if(fp==NULL)
@ -29,7 +109,7 @@ AMSCIMGLIB4_API int amscimglib4_magic_type(const char *fname)
else
{
fseek(fp,0L,SEEK_SET);
fread(magicbuff,8,1,fp);
res = fread(magicbuff,8,1,fp);
fseek(fp,0L,SEEK_SET);
//is this file one of PNG, BMP, or JPEG?

View File

@ -5,8 +5,9 @@ int main(int argc, char* argv[])
{
int ret = 0;
amscimglib4_test_init();
//amscimglib4_test_init();
amscimglib4_test_loadsave();
amscimglib4_test_binreadwrite();
return ret;
}

BIN
testimg/testpfp0_bwrite.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 90 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 101 KiB

After

Width:  |  Height:  |  Size: 247 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 108 KiB

After

Width:  |  Height:  |  Size: 247 KiB