From 54800635387fbe226255359e3a78e2884deb162d Mon Sep 17 00:00:00 2001 From: Antonio Ospite Date: Sat, 23 Mar 2013 19:58:29 +0100 Subject: [PATCH 1/1] Initial import --- .gitignore | 3 ++ Makefile | 39 ++++++++++++++++ libjpeg8-mem-dest.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 libjpeg8-mem-dest.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..38c11ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.jpg +libjpeg8-mem-dest diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5307b1e --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +CFLAGS = -std=c99 -pedantic -pedantic-errors -Wall -g3 -O2 -D_ANSI_SOURCE_ +CFLAGS += -fno-common \ + -Wall \ + -Wdeclaration-after-statement \ + -Wextra \ + -Wformat=2 \ + -Winit-self \ + -Winline \ + -Wpacked \ + -Wp,-D_FORTIFY_SOURCE=2 \ + -Wpointer-arith \ + -Wlarger-than-65500 \ + -Wmissing-declarations \ + -Wmissing-format-attribute \ + -Wmissing-noreturn \ + -Wmissing-prototypes \ + -Wnested-externs \ + -Wold-style-definition \ + -Wredundant-decls \ + -Wsign-compare \ + -Wstrict-aliasing=2 \ + -Wstrict-prototypes \ + -Wswitch-enum \ + -Wundef \ + -Wunreachable-code \ + -Wunsafe-loop-optimizations \ + -Wunused-but-set-variable \ + -Wwrite-strings + + +LDLIBS += -ljpeg + +libjpeg8-mem-dest: libjpeg8-mem-dest.o + +clean: + rm -rf *~ *.o *.jpg libjpeg8-mem-dest + +test: + valgrind --leak-check=full --show-reachable=yes ./libjpeg8-mem-dest diff --git a/libjpeg8-mem-dest.c b/libjpeg8-mem-dest.c new file mode 100644 index 0000000..95e1c36 --- /dev/null +++ b/libjpeg8-mem-dest.c @@ -0,0 +1,127 @@ +/* + * libjpeg8-mem-dest - an example about reusing the memory buffer with libjpeg8 + * + * Copyright (C) 2013 Antonio Ospite + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include + +#define WIDTH 800 +#define HEIGHT 480 + +static unsigned char *new_image(void) +{ + unsigned char *image; + int i; + + image = malloc(WIDTH * HEIGHT * 3 * sizeof(*image)); + if (image == NULL) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < WIDTH * HEIGHT * 3; i++) + image[i] = rand() % 0xff; + + return image; +} + +int main(void) +{ + int i; + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + JSAMPROW row_pointer[1]; + int row_stride; + + unsigned char *out_buf; + unsigned long out_buf_size; + + unsigned char *input_image; + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + + out_buf = NULL; + out_buf_size = 0; + jpeg_mem_dest(&cinfo, &out_buf, &out_buf_size); + + cinfo.image_width = WIDTH; + cinfo.image_height = HEIGHT; + cinfo.input_components = 3; + cinfo.in_color_space = JCS_RGB; + + jpeg_set_defaults(&cinfo); + jpeg_set_quality(&cinfo, 80, TRUE); + + row_stride = WIDTH * 3; + + srand(time(NULL)); + + i = 0; + while(i++ < 10) { + input_image = new_image(); + + jpeg_start_compress(&cinfo, TRUE); + while(cinfo.next_scanline < cinfo.image_height) { + row_pointer[0] = &input_image[cinfo.next_scanline * row_stride]; + (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + jpeg_finish_compress(&cinfo); + + free(input_image); + + /* Is out_buf_size the number of bytes of the jpeg image, or the + * size of the allocated buffer? */ + fprintf(stderr, "out_buf: %p out_buf_size: %ld\n", out_buf, out_buf_size); + { + char filename[256] = { 0 }; + FILE *out_file; + + snprintf(filename, sizeof(filename), "out%03d.jpg", i); + out_file = fopen(filename, "wb"); + fwrite(out_buf, out_buf_size, 1, out_file); + fclose(out_file); + } + +#if 1 + /* + * Rewind the buffer, taken from: + * http://sourceforge.net/p/libjpeg-turbo/discussion/1086868/thread/b797ffaf/ + */ + cinfo.dest->free_in_buffer += cinfo.dest->next_output_byte - out_buf; + cinfo.dest->next_output_byte = out_buf; +#else + /* The code above avoids freeing and allocating a NEW buffer + * Isn't there a more straightforward way to recycle the old + * buffer after we consumed its content? + */ + free(out_buf); + out_buf = NULL; + out_buf_size = 0; + jpeg_mem_dest(&cinfo, &out_buf, &out_buf_size); +#endif + } + + jpeg_destroy_compress(&cinfo); + + free(out_buf); + + return 0; +} -- 2.1.4