summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/io-jpeg.c
diff options
context:
space:
mode:
authorElliot Lee <sopwith@src.gnome.org>1999-11-02 16:10:25 +0000
committerElliot Lee <sopwith@src.gnome.org>1999-11-02 16:10:25 +0000
commitbf146c95a15b93c7d4868900e6ad09237bfd0a6b (patch)
treead0f42fe8eea37691d52b4acb92a657c8478e754 /gdk-pixbuf/io-jpeg.c
parent8c361eed9cbad2b34c08807236b822132b2d764b (diff)
downloadgdk-pixbuf-bf146c95a15b93c7d4868900e6ad09237bfd0a6b.tar.gz
Reorganize gdk_pixbuf_loader_write into three functions, and eliminate
* src/gdk-pixbuf-loader.c: Reorganize gdk_pixbuf_loader_write into three functions, and eliminate duplication of code from write() and close(). Also fix bug where the 128-byte header was being written twice.
Diffstat (limited to 'gdk-pixbuf/io-jpeg.c')
-rw-r--r--gdk-pixbuf/io-jpeg.c52
1 files changed, 27 insertions, 25 deletions
diff --git a/gdk-pixbuf/io-jpeg.c b/gdk-pixbuf/io-jpeg.c
index 82954d613..89ab14ca2 100644
--- a/gdk-pixbuf/io-jpeg.c
+++ b/gdk-pixbuf/io-jpeg.c
@@ -45,6 +45,8 @@
#include <config.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <setjmp.h>
#include <jpeglib.h>
#include "gdk-pixbuf.h"
@@ -53,13 +55,14 @@
/* we are a "source manager" as far as libjpeg is concerned */
+#define JPEG_PROG_BUF_SIZE 4096
+
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
- JOCTET * buffer; /* start of buffer */
- gboolean start_of_file; /* have we gotten any data yet? */
+ JOCTET buffer[JPEG_PROG_BUF_SIZE]; /* start of buffer */
long skip_next; /* number of bytes to skip next read */
-
+
} my_source_mgr;
typedef my_source_mgr * my_src_ptr;
@@ -85,8 +88,6 @@ typedef struct {
struct error_handler_data jerr;
} JpegProgContext;
-#define JPEG_PROG_BUF_SIZE 4096
-
GdkPixbuf *image_load (FILE *f);
gpointer image_begin_load (ModulePreparedNotifyFunc func, gpointer user_data);
void image_stop_load (gpointer context);
@@ -227,7 +228,6 @@ init_source (j_decompress_ptr cinfo)
{
my_src_ptr src = (my_src_ptr) cinfo->src;
- src->start_of_file = TRUE;
src->skip_next = 0;
}
@@ -278,7 +278,7 @@ image_begin_load (ModulePreparedNotifyFunc func, gpointer user_data)
JpegProgContext *context;
my_source_mgr *src;
- context = g_new (JpegProgContext, 1);
+ context = g_new0 (JpegProgContext, 1);
context->notify_func = func;
context->notify_user_data = user_data;
context->pixbuf = NULL;
@@ -289,9 +289,8 @@ image_begin_load (ModulePreparedNotifyFunc func, gpointer user_data)
/* create libjpeg structures */
jpeg_create_decompress (&context->cinfo);
- context->cinfo.src = (struct jpeg_source_mgr *) g_new (my_source_mgr, 1);
+ context->cinfo.src = (struct jpeg_source_mgr *) g_new0 (my_source_mgr, 1);
src = (my_src_ptr) context->cinfo.src;
- src->buffer = g_malloc (JPEG_PROG_BUF_SIZE);
context->cinfo.err = jpeg_std_error (&context->jerr.pub);
@@ -316,6 +315,7 @@ void
image_stop_load (gpointer data)
{
JpegProgContext *context = (JpegProgContext *) data;
+
g_return_if_fail (context != NULL);
if (context->pixbuf)
@@ -324,8 +324,6 @@ image_stop_load (gpointer data)
if (context->cinfo.src) {
my_src_ptr src = (my_src_ptr) context->cinfo.src;
- if (src->buffer)
- g_free (src->buffer);
g_free (src);
}
@@ -352,12 +350,12 @@ image_load_increment (gpointer data, guchar *buf, guint size)
struct jpeg_decompress_struct *cinfo;
my_src_ptr src;
guint num_left, num_copy;
- guchar *nextptr;
g_return_val_if_fail (context != NULL, FALSE);
g_return_val_if_fail (buf != NULL, FALSE);
src = (my_src_ptr) context->cinfo.src;
+
cinfo = &context->cinfo;
/* skip over data if requested, handle unsigned int sizes cleanly */
@@ -376,22 +374,26 @@ image_load_increment (gpointer data, guchar *buf, guint size)
while (num_left > 0) {
/* copy as much data into buffer as possible */
- num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
- size);
- if (num_copy == 0)
- g_assert ("Buffer overflow!\n");
+ if(src->pub.bytes_in_buffer)
+ {
+ int space_used =
+ JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer;
- nextptr = src->buffer + src->pub.bytes_in_buffer;
- memcpy (nextptr, buf, num_copy);
-
- if (src->pub.next_input_byte == NULL ||
- src->pub.bytes_in_buffer == 0)
- src->pub.next_input_byte = src->buffer;
+ memmove(src->buffer, src->buffer + space_used, src->pub.bytes_in_buffer);
+ g_print("Moving %d bytes from offset %d to head\n", src->pub.bytes_in_buffer, space_used);
+ }
+ num_copy = MIN (JPEG_PROG_BUF_SIZE - src->pub.bytes_in_buffer,
+ size);
+ if (num_copy == 0)
+ g_error ("Buffer overflow!");
+ memcpy(src->buffer + src->pub.bytes_in_buffer, buf, num_copy);
+ src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer += num_copy;
-
num_left -= num_copy;
+ g_print("Copied %d bytes, now have %d in buffer, %d left\n", num_copy, src->pub.bytes_in_buffer, num_left);
+ G_BREAKPOINT();
/* try to load jpeg header */
if (!context->got_header) {
@@ -399,6 +401,7 @@ image_load_increment (gpointer data, guchar *buf, guint size)
rc = jpeg_read_header (cinfo, TRUE);
context->src_initialized = TRUE;
+
if (rc == JPEG_SUSPENDED)
continue;
@@ -418,7 +421,7 @@ image_load_increment (gpointer data, guchar *buf, guint size)
if (context->pixbuf == NULL) {
/* Failed to allocate memory */
- g_assert ("Couldn't allocate gdkpixbuf\n");
+ g_error ("Couldn't allocate gdkpixbuf");
}
/* Use pixbuf buffer to store decompressed data */
@@ -430,7 +433,6 @@ image_load_increment (gpointer data, guchar *buf, guint size)
(* context->notify_func) (context->pixbuf,
context->notify_user_data);
- src->start_of_file = FALSE;
} else if (!context->did_prescan) {
int rc;