From 1171b5cd8a723ec46f4a3784e0a7e0a9f6fcbd4f Mon Sep 17 00:00:00 2001
From: Antonio Ospite <ospite@studenti.unina.it>
Date: Sat, 7 Sep 2013 13:24:35 +0200
Subject: [PATCH] Initial import

---
 Makefile                 |  39 +++++++++++++++++
 libturbojpeg1-mem-dest.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 Makefile
 create mode 100644 libturbojpeg1-mem-dest.c

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ff2e4b6
--- /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 += -lturbojpeg
+
+libturbojpeg1-mem-dest: libturbojpeg1-mem-dest.o
+
+clean:
+	rm -rf *~ *.o *.jpg libturbojpeg1-mem-dest
+
+test:
+	valgrind --leak-check=full --show-reachable=yes ./libturbojpeg1-mem-dest
diff --git a/libturbojpeg1-mem-dest.c b/libturbojpeg1-mem-dest.c
new file mode 100644
index 0000000..0cedb8e
--- /dev/null
+++ b/libturbojpeg1-mem-dest.c
@@ -0,0 +1,108 @@
+/*
+ * libturbojpeg1-mem-dest - an example about reusing the memory buffer with libturbojpeg1
+ *
+ * Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+#include <turbojpeg.h>
+
+#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;
+	tjhandle jpeg_compressor;
+	unsigned char *out_buf;
+	unsigned long out_buf_size;
+	unsigned char *input_image;
+	int ret;
+
+	jpeg_compressor = tjInitCompress();
+	if (jpeg_compressor == NULL) {
+		fprintf(stderr, "tjInitCompress failed: %s\n", tjGetErrorStr());
+		return -1;
+	}
+
+	out_buf_size = tjBufSize(WIDTH, HEIGHT, TJSAMP_422);
+	ret = (int) out_buf_size;
+	if (ret < 0) {
+		fprintf(stderr, "tjBufSize failed: %s\n", tjGetErrorStr());
+		goto out_destroy_compressor;
+	}
+	out_buf = malloc(out_buf_size);
+
+	i = 0;
+	while(i++ < 10) {
+		input_image = new_image();
+
+		ret = tjCompress2(jpeg_compressor, input_image, WIDTH, 0, HEIGHT,
+				  TJPF_RGB, &out_buf, &out_buf_size, TJSAMP_422,
+				  80, TJFLAG_NOREALLOC | TJFLAG_FASTDCT);
+		if (ret < 0) {
+			tjGetErrorStr();
+			fprintf(stderr, "tjCompress2 failed: %s\n", tjGetErrorStr());
+			free(input_image);
+			goto out_free_buffer;
+		}
+
+		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);
+		}
+	}
+
+	ret = 0;
+
+out_free_buffer:
+	free(out_buf);
+out_destroy_compressor:
+	ret = tjDestroy(jpeg_compressor);
+	if (ret < 0)
+		fprintf(stderr, "tjDestroy failed: %s\n", tjGetErrorStr());
+
+	return ret;
+}
-- 
2.1.4