read a png image in buffer - c

hi i have used libpng to convert grayscale png image to raw image using c. in that lib the function png_init_io needs file pointer to read the png. but i pass the png image as buffer is there any other alternative functions to to read png image buffer to raw image. please help me
int read_png(char *file_name,int *outWidth,int *outHeight,unsigned char **outRaw) /* We need to open the file */
{
......
/* Set up the input control if you are using standard C streams */
png_init_io(png_ptr, fp);
......
}
instead i need this like
int read_png(unsigned char *pngbuff, int pngbuffleng, int *outWidth,int *outHeight,unsigned char **outRaw) /* We need to open the file */
{
}

From the manual of png_init_io, it is apparent that you can override the read function with png_set_read_fn.
Doing this, you can fool png_init_io into thinking that it is reading from a file, while in reality you're reading from a buffer:
struct fake_file
{
unsigned int *buf;
unsigned int size;
unsigned int cur;
};
static ... fake_read(FILE *fp, ...) /* see input and output from doc */
{
struct fake_file *f = (struct fake_file *)fp;
... /* read a chunk and update f->cur */
}
struct fake_file f = { .buf = pngBuff, .size = pngbuffleng, .cur = 0 };
/* override read function with fake_read */
png_init_io(png_ptr, (FILE *)&f);

Try to use png_set_read_fn instead of png_init_io.

Related

How to allocate memory when using custom reading functions in libpng?

I'm in need of reading base64 encoded PNG image, stored as char array/null terminated string, and I'm stuck. Here is what I have found out for now:
Libpng is capable of changing it's workings, by using png_set_*_fn().
reading functions must have prototype alike this one : void user_read_data(png_structp png_ptr, png_bytep data, size_t length); and must check for EOF errors.
Original read function (which reads from png file directly) calls an fread function and dumps everything to memory pointed by data. I have no idea how libpng knows about image size.
So, here is my implementation of read function
size_t base64_to_PNG(const char *const base64_png, png_bytep out)
{
size_t encoded_size, decoded_count;
size_t decoded_size = base64_decoded_block_size(base64_png, &encoded_size);
decoded_count = base64_decode_block(base64_png, encoded_size, (char*)out);
if(decoded_count != decoded_size)
return 0;
return decoded_size;
}
void my_read_png_from_data_uri(png_structp png_ptr, png_bytep data, size_t length)
{
const char *base64_encoded_png = NULL;
size_t PNG_bytes_len;
if(png_ptr == NULL)
return;
base64_encoded_png = png_get_io_ptr(png_ptr);
PNG_bytes_len = base64_to_PNG(base64_encoded_png, data);
if(PNG_bytes_len != length)
png_error(png_ptr, "Error occured during decoding of the image data");
}
I do believe that information about the decoded image size is lost, and I'm going straight to the segfault with that, as I'll be writing to some random address, but I have no idea how to tell libpng how much memory I need. Can you please help me with that?

Function misunderstanding

#include <stdio.h>
typedef struct Forca // definining struct here
{
char palavra[TAM_PALAVRA];
char palavra_mascarada[TAM_PALAVRA];
int erros, acertos, tentativas;
} t_forca;
void salva_jogo(t_forca forca) //function that writes structure inside bin file
{
FILE* save;
save = fopen("save.bin", "w+b");
if (save == NULL)
{
printf("\nerro no arquivo\n");
}
fwrite(&forca, sizeof(forca), 1, save);
fclose(save);
}
void carrega_jogo(t_forca* forca) //function that read struct inside bin file
{
FILE* readsave;
readsave = fopen("save.bin", "r+b");
if (readsave == NULL)
{
printf("\nerro no arquivo\n");
} //printf error
fread(forca, sizeof(forca), 1, readsave);
fclose(readsave);
}
basically I'm trying to save and read a structure inside a binary file, and I'm quite lost cuz the file is being written but not read at all
In the function carrega_jogo, forca is a pointer, sizeof(forca) is the same as size of pointer, which is 4 or 8 bytes, depending on your system or compiler settings. The read function ends up reading only 4 or 8 bytes. The rest of the structure is likely uninitialized and leads to undefined behavior.
The correct version should be sizeof(t_forca)
Aside, for fwrite/fread it is enough "wb" and "rb".

how to use zpipe inflate on a binary unsigned char

I have a compressed and base64 encoded string that I want to decompress zpipe.
I did the tutorial here and it worked great. I b64 decoded the string first, saved it to a file and then used the inf() function to decompress it.
int ret;
char *b64_string = (char *)read_b64_string();
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
free(b64_string);
write_decoded_b64_to_file(my_string, my_string_len);
free(my_string);
ret = inf();
and then I changed the inf() function to hardcoded files:
int inf()
{
FILE *source;
FILE *dest;
source = fopen("/path/to/my/b64decoded_file/", "r");
dest = fopen("/path/to/my/decompressed_file/", "w");
Now I want to change the inf() function to make it work when the binary is passed as an argument.
int ret;
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
ret = inf(my_string);
I think I identified this line
strm.avail_in = fread(in, 1, CHUNK, source);
as the one where I have to read in the binary. fread is only for files though. How can I read this binary file in without a file?
Just use fmemopen() to open my_string as a file.
source = fmemopen(my_string, my_string_len, "rb");
(I put in the b in "rb" by habit. Never hurts. Can help.)

How to edit EXIF tag in C?

I have a JPG file that was taken using BlackBerry 10 Dev Alpha. This code (a slightly modified version of this example) prints the result correctly
static char* read_tag(ExifData *ed, ExifIfd eid, ExifTag tag){
static char result[1024];
ExifEntry *entry = exif_content_get_entry(ed->ifd[eid], tag);
if (entry){
char buf[1024];
exif_entry_get_value(entry, buf, sizeof(buf));
trim_spaces(buf);
if (*buf) strcpy(result, buf);
else strcpy(result, "NULL");
}
else strcpy(result, "NULL");
return result;
}
Which means the output of:
printf("Model : %s\n", read_tag(ed, EXIF_IFD_0, EXIF_TAG_MODEL));
is:
Model : BlackBerry 10 Dev Alpha
Now I wonder how to replace "BlackBerry 10 Dev Alpha" (EXIF_TAG_MODEL) with another value, e.g "Nokia 3330". I already take a look at another example . Unfortunately I found it quite hard to read. Maybe someone has a shorter/straightforward code?
libexif doesn't support directly loading JPG's in. You'll need another package to read in the JPG and extract the EXIF header (or you could write something yourself).
Note that in the example it simply creates a new exif header, then saves it to file using fwrite, and then appends the raw JPG data without exif information on the end in this part of the code here:
/* Write JPEG image data, skipping the non-EXIF header */
if (fwrite(image_jpg+image_data_offset, image_data_len, 1, f) != 1) {
fprintf(stderr, "Error writing to file %s\n", FILE_NAME);
goto errout;
}
There is an excellent Github project called exifyay that uses libexif and has two extra libs that handle reading in JPGS. It is a python project but the sources for the libraries are C. You can find exifyay here (note I am not involved in any way with exifyay or libexif)
I have just recently compiled libexif and merged sources from exifyay into a VS2010 project here. There is an example in the folder 'contrib\examples\LibexifExample'. If you don't like downloading random links here is a sample of the code I got working:
/*
* write-exif.c
*
* Placed into the public domain by Daniel Fandrich
*
* Create a new EXIF data block and write it into a JPEG image file.
*
* The JPEG image data used in this example is fixed and is guaranteed not
* to contain an EXIF tag block already, so it is easy to precompute where
* in the file the EXIF data should be. In real life, a library like
* libjpeg (included with the exif command-line tool source code) would
* be used to write to an existing JPEG file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <libexif/exif-data.h>
#include <libjpeg/jpeg-data.h>
#include <JpegEncoderEXIF/JpegEncoderEXIF.h>
/* byte order to use in the EXIF block */
#define FILE_BYTE_ORDER EXIF_BYTE_ORDER_INTEL
/* comment to write into the EXIF block */
#define FILE_COMMENT "libexif demonstration image"
/* special header required for EXIF_TAG_USER_COMMENT */
#define ASCII_COMMENT "ASCII\0\0\0"
static ExifEntry *create_tag(ExifData *exif, ExifIfd ifd, ExifTag tag, size_t len)
{
void *buf;
ExifEntry *entry;
/* Create a memory allocator to manage this ExifEntry */
ExifMem *mem = exif_mem_new_default();
assert(mem != NULL); /* catch an out of memory condition */
/* Create a new ExifEntry using our allocator */
entry = exif_entry_new_mem (mem);
assert(entry != NULL);
/* Allocate memory to use for holding the tag data */
buf = exif_mem_alloc(mem, len);
assert(buf != NULL);
/* Fill in the entry */
entry->data = (unsigned char*)buf;
entry->size = len;
entry->tag = tag;
entry->components = len;
entry->format = EXIF_FORMAT_UNDEFINED;
/* Attach the ExifEntry to an IFD */
exif_content_add_entry (exif->ifd[ifd], entry);
/* The ExifMem and ExifEntry are now owned elsewhere */
exif_mem_unref(mem);
exif_entry_unref(entry);
return entry;
}
int main(int argc, char **argv)
{
ExifEntry *entry;
//Input JPG
char mInputFilename[]="example.jpg";
//Load JPG
JPEGData * mJpegData = jpeg_data_new_from_file(mInputFilename);
//Load Exif data from JPG
ExifData * mExifData = jpeg_data_get_exif_data(mJpegData);
//Set some Exif options
exif_data_set_option(mExifData, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
exif_data_set_data_type(mExifData, EXIF_DATA_TYPE_COMPRESSED);
exif_data_set_byte_order(mExifData, FILE_BYTE_ORDER);
entry = create_tag(mExifData, EXIF_IFD_EXIF, EXIF_TAG_USER_COMMENT,
sizeof(ASCII_COMMENT) + sizeof(FILE_COMMENT) - 2);
/* Write the special header needed for a comment tag */
memcpy(entry->data, ASCII_COMMENT, sizeof(ASCII_COMMENT)-1);
/* Write the actual comment text, without the trailing NUL character */
memcpy(entry->data+8, FILE_COMMENT, sizeof(FILE_COMMENT)-1);
/* create_tag() happens to set the format and components correctly for
* EXIF_TAG_USER_COMMENT, so there is nothing more to do. */
/* Create a EXIF_TAG_SUBJECT_AREA tag */
entry = create_tag(mExifData, EXIF_IFD_EXIF, EXIF_TAG_SUBJECT_AREA,
4 * exif_format_get_size(EXIF_FORMAT_SHORT));
entry->format = EXIF_FORMAT_SHORT;
entry->components = 4;
//Write back exif data
jpeg_data_set_exif_data(mJpegData,mExifData);
//Save to JPG
jpeg_data_save_file(mJpegData,"test.jpg");
return 0;
}

store and restore struct in file in C

I need to store a struct in a file and read it back to return it then.
I would try to write it to the file like this:
void lld_tpWriteCalibration(struct cal cal) {
FIL fdst; /* file objects */
UINT bw; /* File write count */
/* Create destination file on the drive 0 */
wf_open(&fdst, "0:calibration.txt", FA_CREATE_ALWAYS | FA_WRITE);
wf_write(&fdst, cal, sizeof(cal), &bw);
wf_close(&fdst);
}
Would that work?
And how can I read it back and return it from this function?
struct cal lld_tpReadCalibration(void) {
}
The struct is:
struct cal {
float xm;
float ym;
float xn;
float yn;
};
Thanks for your help.
You can retrieve your structure the same way you stored it.
read(&fdst, &cal, sizeof(cal));
But you got to be careful, you won't be able to do this on every architecture because of endianess problem.
You'll be OK with that structure and that writing/reading technique if you only try to read the file on the same type of machine as you write it on. The data written like that is not reliably portable across different types of machines.

Resources