]> git.mxchange.org Git - simgear.git/blob - simgear/screen/jpgfactory.cxx
No observed_ptr needed.
[simgear.git] / simgear / screen / jpgfactory.cxx
1 // jpgfactory.cxx -- jpeg frontend for TR library
2 //
3 // Written by Norman Vine, started August 2001.
4 //
5 // Copyright (C) 2001  Norman Vine - nhv@yahoo.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include "jpgfactory.hxx"
33 #include <string.h>   
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 static void init_destination (j_compress_ptr cinfo);
40 static void term_destination (j_compress_ptr cinfo);
41 static boolean empty_output_buffer (j_compress_ptr cinfo);
42
43 #ifdef __cplusplus
44 }
45 #endif
46
47 // OSGFIME: offscreenrendering on osg - completely new context ...
48
49 typedef struct {
50     struct jpeg_destination_mgr pub; /* public fields */
51     unsigned char * outfile; /* target stream */
52     JOCTET * buffer;         /* start of buffer */
53     int numbytes;            /* num bytes used */
54     int maxsize;             /* size of outfile */
55     int error;               /* error flag */
56 } my_destination_mgr;
57
58 typedef my_destination_mgr * my_dest_ptr;
59
60 void (*jpgRenderFrame)(void) = NULL;
61
62 trJpgFactory::trJpgFactory() {
63     imageWidth = imageHeight = 0;
64     tile       = NULL;
65     buffer     = NULL;
66     IMAGE      = NULL;
67     tr         = NULL;
68     cinfo.dest = NULL;
69 }
70
71 trJpgFactory::~trJpgFactory() {
72     destroy();
73 }
74
75 /*
76  * deallocate our dynamic parts
77  */
78
79 void trJpgFactory::destroy( int error )
80 {
81     if( error )
82         printf( "!! Malloc Failure trJpgFactory ( %d )!!\n",
83                 error );
84
85     if( cinfo.dest )  jpeg_destroy_compress(&cinfo);
86     if( tr )          trDelete(tr);
87     if( IMAGE )       delete [] IMAGE;
88     if( buffer )      delete [] buffer;
89     if( tile )        delete [] tile;
90 }
91
92 /*
93  * allocate and initialize the jpeg compress struct
94  * application needs to dealocate this
95  */
96
97 int trJpgFactory::jpeg_init()
98 {
99     j_compress_ptr cinfoPtr = &cinfo;
100
101     cinfoPtr->err = jpeg_std_error(&jerr);
102     jpeg_create_compress(cinfoPtr);
103
104     /* following taken from jpeg library exaample code */
105     cinfoPtr->dest = (struct jpeg_destination_mgr *)
106                      (*cinfoPtr->mem->alloc_small)
107                      ((j_common_ptr)cinfoPtr,
108                       JPOOL_PERMANENT,
109                       sizeof(my_destination_mgr));
110
111     my_dest_ptr dest = (my_dest_ptr)cinfoPtr->dest;
112
113     if( !dest ) {
114         destroy(5);
115         return 5;
116     }
117
118     dest->pub.init_destination    = init_destination;
119     dest->pub.empty_output_buffer = empty_output_buffer;
120     dest->pub.term_destination    = term_destination;
121     dest->outfile  = NULL;
122     dest->numbytes = 0;
123     dest->maxsize  = 0;
124
125     cinfoPtr->image_width      = imageWidth;
126     cinfoPtr->image_height     = imageHeight;
127     cinfoPtr->input_components = 3;
128     cinfoPtr->in_color_space   = JCS_RGB;
129     jpeg_set_defaults(cinfoPtr);
130     jpeg_set_quality(cinfoPtr, (100 * 90) >> 8, TRUE);
131
132     return 0;
133 }
134
135
136 /*
137  * may also be used as reinit() to change image size
138  */
139
140 int trJpgFactory::init(int width, int height )
141 {
142     destroy();
143
144     if( width <= 0 || height <= 0 ) {
145         imageWidth  = DEFAULT_XS;
146         imageHeight = DEFAULT_YS;
147     } else {
148         imageWidth  = width;
149         imageHeight = height;
150     }
151
152     int bufsize = imageWidth * imageHeight * 3 * sizeof(GLubyte);
153
154     /* allocate buffer large enough to store one tile */
155     tile = new GLubyte[bufsize];
156     if (!tile) {
157         destroy(1);
158         return 1;
159     }
160
161     /* allocate buffer to hold a row of tiles */
162     buffer = new GLubyte[ bufsize ];
163     if (!buffer) {
164         destroy(2);
165         return 2;
166     }
167
168     /* this should be big enough */
169     IMAGESIZE = bufsize + 1024;
170     IMAGE = new unsigned char[ IMAGESIZE ];
171     if( !IMAGE ) {
172         destroy(3);
173         return 3;
174     }
175
176     tr = trNew();
177     if( !tr ) {
178         destroy(4);
179         return 4;
180     }
181     
182     trRowOrder(tr, TR_TOP_TO_BOTTOM);
183     trTileSize(tr, imageWidth, imageHeight, 0);
184     trImageSize(tr, imageWidth, imageHeight);
185     trTileBuffer(tr, GL_RGB, GL_UNSIGNED_BYTE, tile);
186
187     return jpeg_init();
188 }
189
190 /*
191  *compress the image
192  */
193
194 int trJpgFactory::compress()
195 {
196     JSAMPROW  row_pointer[1];
197     int       row_stride;
198
199     /* to keep track of jpeg library routines */
200     my_dest_ptr dest = (my_dest_ptr) cinfo.dest;
201
202     //printf("\tjpeg_start_compress(&cinfo, TRUE)\n");
203     jpeg_start_compress(&cinfo, TRUE);
204     if( !dest->error ) {
205         dest->outfile = IMAGE;
206         dest->maxsize = IMAGESIZE;
207         row_stride    = cinfo.image_width * 3;
208
209         while( cinfo.next_scanline < cinfo.image_height &&
210                !dest->error )
211         {
212             row_pointer[0] = buffer + (cinfo.next_scanline * row_stride);
213             jpeg_write_scanlines(&cinfo, row_pointer, 1);
214         }
215     }
216     if( !dest->error ) {
217         // printf("\n\tBEFORE: jpeg_finish_compress(&cinfo)\n");
218         jpeg_finish_compress(&cinfo);
219         // printf("\tAFTER: jpeg_finish_compress(&cinfo)\n");
220     } else {
221         printf("INTERNAL JPEG_FACTORY ERROR\n");
222         jpeg_abort_compress(&cinfo);
223         /* OK - I am paranoid */
224         dest->numbytes = 0;
225     }
226     return dest->numbytes;
227 }
228
229 /*
230  * Makes the image then calls compress()
231  */
232
233 int trJpgFactory::render()
234 {
235     if( !tr || !jpgRenderFrame ) {
236         printf("!! NO tr !!\n   trJpgFactory::render()\n");
237         return 0;
238     }
239
240     // Make sure we have SSG projection primed for current view
241     glMatrixMode(GL_MODELVIEW);
242     glLoadIdentity();
243
244     /* just to be safe... */
245     glPixelStorei(GL_PACK_ALIGNMENT, 1);
246
247     // printf("\ttrBeginTile(tr)\n");
248     trBeginTile(tr);
249     jpgRenderFrame();
250     trEndTile(tr);
251
252     /* just to be safe */
253     int curTileHeight = trGet(tr, TR_CURRENT_TILE_HEIGHT);
254     int curTileWidth  = trGet(tr, TR_CURRENT_TILE_WIDTH);
255
256     /* reverse image top to bottom */
257     int bytesPerImageRow = imageWidth * 3*sizeof(GLubyte);
258     int bytesPerTileRow  = imageWidth * 3*sizeof(GLubyte);
259     int bytesPerCurrentTileRow = (curTileWidth) * 3*sizeof(GLubyte);
260     int i;
261     for (i=0;i<imageHeight;i++) {
262         memcpy(buffer + (curTileHeight-1-i) * bytesPerImageRow, /* Dest */
263                tile + i*bytesPerTileRow,              /* Src */
264                bytesPerCurrentTileRow);               /* Byte count*/
265     }
266
267     //  printf("exit trJpgFactory::render()\n");
268     return  compress();
269 }
270
271
272 #define OUTPUT_BUF_SIZE  4096
273
274 #ifdef __cplusplus
275 extern "C" {
276 #endif
277
278 /*
279  * Initialize destination --- called by jpeg_start_compress
280  * before any data is actually written.
281  */
282
283 static void init_destination (j_compress_ptr cinfo)
284 {
285     // printf("enter init_destination()\n");
286     my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
287
288     /* following taken from jpeg library exaample code
289      * Allocate the output buffer ---
290      * it automaically will be released when done with image */
291         
292     dest->buffer = (JOCTET *)(*cinfo->mem->alloc_small)
293                    ((j_common_ptr)cinfo, JPOOL_IMAGE,
294                     OUTPUT_BUF_SIZE * sizeof(JOCTET) );
295
296     if( !dest->buffer ) {
297         printf("MALLOC FAILED jpegFactory init_destination()\n");
298         dest->error = TRUE;
299     } else {
300         dest->error = FALSE;
301     }
302     dest->pub.next_output_byte = dest->buffer;
303     dest->pub.free_in_buffer   = OUTPUT_BUF_SIZE;
304     dest->numbytes = 0;
305 }
306
307 /*
308  * Empty the output buffer --- called whenever buffer fills up.
309  */
310
311 static boolean empty_output_buffer (j_compress_ptr cinfo)
312 {
313     // printf("enter empty_output_buffer(%d)\n", OUTPUT_BUF_SIZE);
314     my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
315
316     if( (!dest->error) &&
317           ((dest->numbytes + OUTPUT_BUF_SIZE) < dest->maxsize) )
318     {
319         memcpy( dest->outfile+dest->numbytes, dest->buffer, (size_t)OUTPUT_BUF_SIZE);
320
321         dest->pub.next_output_byte = dest->buffer;
322         dest->pub.free_in_buffer   = OUTPUT_BUF_SIZE;
323
324         dest->numbytes += OUTPUT_BUF_SIZE;
325     } else {
326         printf("BUFFER OVERFLOW jpegFactory empty_output_buffer()\n");
327         dest->numbytes = 0;
328         dest->error    = TRUE;
329     }
330     return TRUE;
331 }
332
333 /*
334  * Terminate destination --- called by jpeg_finish_compress
335  * after all data has been written.  Usually needs to flush buffer.
336  *
337  * NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
338  * application must deal with any cleanup that should happen even
339  * for error exit.
340  */
341
342 static void term_destination (j_compress_ptr cinfo)
343 {
344     my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
345
346     size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
347
348     if( (!dest->error) &&
349           ((dest->numbytes + datacount) < (unsigned int)dest->maxsize) )
350     {
351         memcpy( dest->outfile+dest->numbytes, dest->buffer, datacount);
352         dest->numbytes += datacount;
353     } else {
354         printf("BUFFER OVERFLOW jpegFactory term_destination()\n");
355         dest->numbytes = 0;
356         dest->error = TRUE;
357     }
358     // printf(" term_destination(%d) total %d\n", datacount, dest->numbytes );
359 }
360
361 #ifdef __cplusplus
362 }
363 #endif
364