Tizen 2.1 base
[platform/upstream/hplip.git] / prnt / hpcups / jdatadbf.c
1 /*
2  * jdatadbf.c
3  *
4  * Copyright (C) 2001, Dorian Goldstein, Thomas G. Lane.
5  *
6  * This file contains compression data destination routines for the case of
7  * This file is identicle with IJG's built in destination file manager in
8  * every respect but 1... instead fo emmiting data to a file
9  * it facilitates definition of a callback function.
10  */
11
12 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
13 #include "jinclude.h"
14 #include "jpeglib.h"
15 #include "jerror.h"
16
17
18 /* Expanded data destination object for stdio output */
19
20 typedef struct {
21   struct jpeg_destination_mgr pub; /* public fields */
22
23   JOCTET * outbuff;             // target output buffer
24   JOCTET * buffer;                // start of internal buffer
25   UINT16   size_outbuff;        // current size of target output buffer
26   JMETHOD (void, flush_output_buffer_callback, (JOCTET *outbuf, JOCTET* buffer, size_t size));
27 } my_destination_mgr;
28
29 typedef my_destination_mgr * my_dest_ptr;
30
31 #define OUTPUT_BUF_SIZE  4096    /* choose an efficiently fwrite'able size */
32
33
34 /*
35  * Initialize destination --- called by jpeg_start_compress
36  * before any data is actually written.
37  */
38
39 METHODDEF(void)
40 init_destination (j_compress_ptr cinfo)
41 {
42   my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
43
44   /* Allocate the output buffer --- it will be released when done with image */
45   dest->buffer = (JOCTET *)
46       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
47                   OUTPUT_BUF_SIZE * SIZEOF(JOCTET));
48
49   dest->pub.next_output_byte = dest->buffer;
50   dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
51 }
52
53
54 /*
55  * Empty the output buffer --- called whenever buffer fills up.
56  *
57  * In typical applications, this should write the entire output buffer
58  * (ignoring the current state of next_output_byte & free_in_buffer),
59  * reset the pointer & count to the start of the buffer, and return TRUE
60  * indicating that the buffer has been dumped.
61  *
62  * In applications that need to be able to suspend compression due to output
63  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
64  * In this situation, the compressor will return to its caller (possibly with
65  * an indication that it has not accepted all the supplied scanlines).  The
66  * application should resume compression after it has made more room in the
67  * output buffer.  Note that there are substantial restrictions on the use of
68  * suspension --- see the documentation.
69  *
70  * When suspending, the compressor will back up to a convenient restart point
71  * (typically the start of the current MCU). next_output_byte & free_in_buffer
72  * indicate where the restart point will be if the current call returns FALSE.
73  * Data beyond this point will be regenerated after resumption, so do not
74  * write it out when emptying the buffer externally.
75  */
76
77 METHODDEF(boolean)
78 empty_output_buffer (j_compress_ptr cinfo)
79 {
80   my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
81
82 // DG Temp   if (dest->flush_output_buffer_callback == NULL) {
83 // DG Temp     if (dest->outbuff) {
84 // DG Temp       MEMCOPY(dest->outbuff + dest->size_outbuff, dest->buffer, OUTPUT_BUF_SIZE);
85 // DG Temp       dest->size_outbuff += OUTPUT_BUF_SIZE;
86 // DG Temp     }
87 // DG Temp   } else {
88 // DG Temp     (*dest->flush_output_buffer_callback)(dest->buffer, OUTPUT_BUF_SIZE);
89 // DG Temp   }
90
91   (*dest->flush_output_buffer_callback)(dest->outbuff, dest->buffer, OUTPUT_BUF_SIZE);
92
93
94   dest->pub.next_output_byte = dest->buffer;
95   dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
96
97   return TRUE;
98 }
99
100
101 /*
102  * Terminate destination --- called by jpeg_finish_compress
103  * after all data has been written.  Usually needs to flush buffer.
104  *
105  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
106  * application must deal with any cleanup that should happen even
107  * for error exit.
108  */
109
110 METHODDEF(void)
111 term_destination (j_compress_ptr cinfo)
112 {
113   my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
114   size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
115
116   /* Write any data remaining in the buffer */
117   if (datacount > 0) {
118 // DG Temp     if (dest->flush_output_buffer_callback == NULL) {
119 // DG Temp       if (dest->outbuff) {
120 // DG Temp         MEMCOPY(dest->outbuff + dest->size_outbuff, dest->buffer, datacount);
121 // DG Temp         dest->size_outbuff += datacount;
122 // DG Temp       }
123 // DG Temp     } else {
124 // DG Temp       (*dest->flush_output_buffer_callback)(dest->buffer, datacount);
125 // DG Temp     }
126
127     (*dest->flush_output_buffer_callback)(dest->outbuff, dest->buffer, datacount);
128   }
129 }
130
131
132 /*
133  * Prepare for output to a stdio stream.
134  * The caller must have already opened the stream, and is responsible
135  * for closing it after finishing compression.
136  */
137
138 GLOBAL(void)
139 jpeg_buffer_dest (j_compress_ptr cinfo, JOCTET* outbuff, void* flush_output_buffer_callback)
140 {
141   my_dest_ptr dest;
142
143   /* The destination object is made permanent so that multiple JPEG images
144    * can be written to the same file without re-executing jpeg_stdio_dest.
145    * This makes it dangerous to use this manager and a different destination
146    * manager serially with the same JPEG object, because their private object
147    * sizes may be different.  Caveat programmer.
148    */
149   if (cinfo->dest == NULL) {    /* first time for this JPEG object? */
150     cinfo->dest = (struct jpeg_destination_mgr *)
151       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
152                   SIZEOF(my_destination_mgr));
153   }
154
155   dest = (my_dest_ptr) cinfo->dest;
156   dest->pub.init_destination    = init_destination;
157   dest->pub.empty_output_buffer = empty_output_buffer;
158   dest->pub.term_destination    = term_destination;
159
160   dest->outbuff      = outbuff;
161   dest->size_outbuff = 0;
162   dest->flush_output_buffer_callback = flush_output_buffer_callback;
163 //(*dest->flush_output_buffer_callback)(-1 , -1);
164 }
165
166 GLOBAL(long)
167 jpeg_buffer_size_dest (j_compress_ptr cinfo)
168 {
169   my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
170
171   return dest->size_outbuff;
172 }
173