xref: /haiku/src/add-ons/translators/jpeg/be_jdatadst.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2 
3 Copyright (c) 2002-2003, Marcin 'Shard' Konicki
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation and/or
13       other materials provided with the distribution.
14     * Name "Marcin Konicki", "Shard" or any combination of them,
15       must not be used to endorse or promote products derived from this
16       software without specific prior written permission from Marcin Konicki.
17 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 */
31 
32 
33 /*
34 	Modified jdatadst.c from libjpeg
35 	to use BPoistionIO instead of FILE
36 */
37 
38 
39 // Be Headers
40 #include <DataIO.h>
41 #include <stdio.h>
42 
43 /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
44 #include <jpeglib.h>
45 #include <jerror.h>
46 
47 // instead of including jinclude - thx to it translator can be compiled
48 // without sources to jpeglib
49 #define SIZEOF(object)	((size_t) sizeof(object))
50 
51 /* Expanded data destination object for stdio output */
52 
53 typedef struct {
54   struct jpeg_destination_mgr pub; /* public fields */
55 
56   BPositionIO * outfile;		/* target stream */
57   JOCTET * buffer;		/* start of buffer */
58 } be_destination_mgr;
59 
60 typedef be_destination_mgr * be_dest_ptr;
61 
62 #define OUTPUT_BUF_SIZE  4096	/* choose an efficiently fwrite'able size */
63 
64 
65 /*
66  * Initialize destination --- called by jpeg_start_compress
67  * before any data is actually written.
68  */
69 
70 METHODDEF(void)
71 be_init_destination (j_compress_ptr cinfo)
72 {
73   be_dest_ptr dest = (be_dest_ptr) cinfo->dest;
74 
75   /* Allocate the output buffer --- it will be released when done with image */
76   dest->buffer = (JOCTET *)
77       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, (OUTPUT_BUF_SIZE * SIZEOF(JOCTET)));
78 
79   dest->pub.next_output_byte = dest->buffer;
80   dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
81 }
82 
83 
84 /*
85  * Empty the output buffer --- called whenever buffer fills up.
86  *
87  * In typical applications, this should write the entire output buffer
88  * (ignoring the current state of next_output_byte & free_in_buffer),
89  * reset the pointer & count to the start of the buffer, and return TRUE
90  * indicating that the buffer has been dumped.
91  *
92  * In applications that need to be able to suspend compression due to output
93  * overrun, a FALSE return indicates that the buffer cannot be emptied now.
94  * In this situation, the compressor will return to its caller (possibly with
95  * an indication that it has not accepted all the supplied scanlines).  The
96  * application should resume compression after it has made more room in the
97  * output buffer.  Note that there are substantial restrictions on the use of
98  * suspension --- see the documentation.
99  *
100  * When suspending, the compressor will back up to a convenient restart point
101  * (typically the start of the current MCU). next_output_byte & free_in_buffer
102  * indicate where the restart point will be if the current call returns FALSE.
103  * Data beyond this point will be regenerated after resumption, so do not
104  * write it out when emptying the buffer externally.
105  */
106 
107 METHODDEF(boolean)
108 be_empty_output_buffer (j_compress_ptr cinfo)
109 {
110   be_dest_ptr dest = (be_dest_ptr) cinfo->dest;
111 
112   if (dest->outfile->Write(dest->buffer, OUTPUT_BUF_SIZE) != (ssize_t) OUTPUT_BUF_SIZE)
113     ERREXIT(cinfo, JERR_FILE_WRITE);
114 
115   dest->pub.next_output_byte = dest->buffer;
116   dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
117 
118   return TRUE;
119 }
120 
121 
122 /*
123  * Terminate destination --- called by jpeg_finish_compress
124  * after all data has been written.  Usually needs to flush buffer.
125  *
126  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
127  * application must deal with any cleanup that should happen even
128  * for error exit.
129  */
130 
131 METHODDEF(void)
132 be_term_destination (j_compress_ptr cinfo)
133 {
134   be_dest_ptr dest = (be_dest_ptr) cinfo->dest;
135   size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
136 
137   /* Write any data remaining in the buffer */
138   if (datacount > 0) {
139       if (dest->outfile->Write(dest->buffer, datacount) != (ssize_t) datacount)
140       ERREXIT(cinfo, JERR_FILE_WRITE);
141   }
142 }
143 
144 
145 /*
146  * Prepare for output to a stdio stream.
147  * The caller must have already opened the stream, and is responsible
148  * for closing it after finishing compression.
149  */
150 
151 GLOBAL(void)
152 be_jpeg_stdio_dest (j_compress_ptr cinfo, BPositionIO * outfile)
153 {
154   be_dest_ptr dest;
155 
156   /* The destination object is made permanent so that multiple JPEG images
157    * can be written to the same file without re-executing jpeg_stdio_dest.
158    * This makes it dangerous to use this manager and a different destination
159    * manager serially with the same JPEG object, because their private object
160    * sizes may be different.  Caveat programmer.
161    */
162   if (cinfo->dest == NULL) {	/* first time for this JPEG object? */
163     cinfo->dest = (struct jpeg_destination_mgr *)
164       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, SIZEOF(be_destination_mgr));
165   }
166 
167   dest = (be_dest_ptr) cinfo->dest;
168   dest->pub.init_destination = be_init_destination;
169   dest->pub.empty_output_buffer = be_empty_output_buffer;
170   dest->pub.term_destination = be_term_destination;
171   dest->outfile = outfile;
172 }
173