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