source: trunk/Lucide/plugins/lujpeg/lujpeg.cpp @ 470

Last change on this file since 470 was 470, checked in by Silvan Scherrer, 11 years ago

poppler, jpeg, freetype lib updates

File size: 10.6 KB
Line 
1
2/*
3 *  This file was generated by the SOM Compiler.
4 *  Generated using:
5 *     SOM incremental update: 2.24
6 */
7
8
9/* ***** BEGIN LICENSE BLOCK *****
10 * Version: CDDL 1.0/LGPL 2.1
11 *
12 * The contents of this file are subject to the COMMON DEVELOPMENT AND
13 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
14 * this file except in compliance with the License. You may obtain a copy of
15 * the License at http://www.sun.com/cddl/
16 *
17 * Software distributed under the License is distributed on an "AS IS" basis,
18 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
19 * for the specific language governing rights and limitations under the
20 * License.
21 *
22 * The Initial Developer of the Original Code is
23 * Eugene Romanenko, netlabs.org.
24 * Portions created by the Initial Developer are Copyright (C) 2006
25 * the Initial Developer. All Rights Reserved.
26 *
27 * Contributor(s):
28 *
29 * Alternatively, the contents of this file may be used under the terms of
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the LGPL are applicable instead of those
32 * above. If you wish to allow use of your version of this file only under the
33 * terms of the LGPL, and not to allow others to use your version of this file
34 * under the terms of the CDDL, indicate your decision by deleting the
35 * provisions above and replace them with the notice and other provisions
36 * required by the LGPL. If you do not delete the provisions above, a recipient
37 * may use your version of this file under the terms of any one of the CDDL
38 * or the LGPL.
39 *
40 * ***** END LICENSE BLOCK ***** */
41
42
43
44/*
45 *  This file was generated by the SOM Compiler and Emitter Framework.
46 *  Generated using template emitter:
47 *      SOM Emitter emitxtm: 2.23.1.9
48 */
49
50#ifndef SOM_Module_lujpeg_Source
51#define SOM_Module_lujpeg_Source
52#endif
53#define LuJpegDocument_Class_Source
54
55#include "lujpeg.xih"
56
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <setjmp.h>
61#include "jversion.h"
62
63// jpeglib defines boolean as int while SOM defines it as unsigned char,
64// work this around (note that you need to use jpeg_boolean in this source if
65// you need to refer to the jpeglib's definition of it)
66#define boolean jpeg_boolean
67#include <jpeglib.h>
68#undef boolean
69
70
71void resample( Environment *ev,
72               LuPixbuf *out, int out_x0, int out_y0, int out_x1, int out_y1,
73               LuPixbuf *in, float in_x0, float in_y0, float in_x1, float in_y1 );
74
75unsigned EXPENTRY LibMain( unsigned hmod, unsigned termination )
76{
77    if ( termination ) {
78        /* DLL is detaching from process */
79    } else {
80        /* DLL is attaching to process */
81    }
82    return( 1 );
83}
84
85
86extern "C" LuDocument * EXPENTRY createObject()
87{
88    return new LuJpegDocument;
89}
90
91extern "C" char * EXPENTRY getSupportedExtensions()
92{
93    return "JPG;JPEG;JPE";
94}
95
96static LuSignature      lsig = { 0, 0, 2, (void *)"\xff\xd8" };
97static LuSignatureList  lsl  = { 1, &lsig };
98static LuSignatureCheck lsc  = { 1, &lsl };
99
100// getSignatureCheck is optional
101extern "C" LuSignatureCheck * EXPENTRY getSignatureCheck()
102{
103    return &lsc;
104}
105
106static char version[256];
107extern "C" char * EXPENTRY getDescription()
108{
109   strcpy(version, "Jpeg plugin, based on IJG JPEG library ");
110   strcat(version, JVERSION);
111   return version;
112}
113
114
115struct JpegDocument
116{
117    LuPixbuf *pb;
118    double width;
119    double height;
120};
121
122struct lujpeg_error_mgr
123{
124    struct jpeg_error_mgr jem;
125    jmp_buf setjmp_buffer;
126};
127
128METHODDEF(void) lujpeg_error_exit( j_common_ptr cinfo )
129{
130    lujpeg_error_mgr *err = (lujpeg_error_mgr *)cinfo->err;
131    char buffer[ JMSG_LENGTH_MAX ];
132    (*cinfo->err->format_message)( cinfo, buffer );
133    fprintf( stderr, "JPEG decoding error:\n%s\n", buffer );
134    longjmp( err->setjmp_buffer, 1 );
135}
136
137
138
139SOM_Scope boolean  SOMLINK loadFile(LuJpegDocument *somSelf,
140                                     Environment *ev, string filename,
141                                    string password, long* errorCode,
142                                    string* error)
143{
144    if ( errorCode != NULL ) {
145        *errorCode = LU_LDERR_NO_ERROR;
146    }
147
148    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
149    JpegDocument *d = (JpegDocument *)somThis->data;
150
151    FILE *infile = NULL;
152    if ( ( infile = fopen( filename, "rb" ) ) == NULL ) {
153        if ( errorCode != NULL ) {
154            *errorCode = LU_LDERR_OPEN_ERROR;
155        }
156        return FALSE;
157    }
158
159    short bpp = getBpp( somSelf, ev );
160    jpeg_decompress_struct cinfo;
161    lujpeg_error_mgr ljerr;
162    cinfo.err = jpeg_std_error( &ljerr.jem );
163    ljerr.jem.error_exit = lujpeg_error_exit;
164    if ( setjmp( ljerr.setjmp_buffer ) ) {
165        // If we get here, the JPEG code has signaled an error.
166        jpeg_destroy_decompress( &cinfo );
167        fclose( infile );
168        if ( errorCode != NULL ) {
169            *errorCode = LU_LDERR_WRONG_FORMAT;
170        }
171        return FALSE;
172    }
173
174    jpeg_create_decompress( &cinfo );
175    jpeg_stdio_src( &cinfo, infile );
176    jpeg_read_header( &cinfo, TRUE );
177    cinfo.out_color_space = JCS_RGB;
178    jpeg_start_decompress( &cinfo );
179
180    d->width = cinfo.output_width;
181    d->height = cinfo.output_height;
182    d->pb = new LuPixbuf( ev, cinfo.output_width, cinfo.output_height, bpp );
183
184    int pixbuf_rowstride, pixbuf_height, pixbuf_width;
185    char *pixbuf_data, *dst, *src;
186
187    pixbuf_data = (char *)d->pb->getDataPtr( ev );
188    pixbuf_width = d->pb->getWidth( ev );
189    pixbuf_height = d->pb->getHeight( ev );
190    pixbuf_rowstride = d->pb->getRowSize(ev );
191
192    JSAMPARRAY buffer;
193    int row_stride = cinfo.output_width * cinfo.output_components;
194    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
195
196    int i, j;
197    i = cinfo.output_height;
198    while ( cinfo.output_scanline < cinfo.output_height )
199    {
200        i--;
201        jpeg_read_scanlines( &cinfo, buffer, 1 );
202        dst = pixbuf_data + ( i * pixbuf_rowstride );
203        int len = pixbuf_width * bpp;
204        for ( j = 0; j < len; j += bpp )
205        {
206            dst[ j ] = buffer[0][ j + 2 ];
207            dst[ j + 1 ] = buffer[0][ j + 1 ];
208            dst[ j + 2 ] = buffer[0][ j ];
209        }
210    }
211
212    jpeg_finish_decompress( &cinfo );
213    jpeg_destroy_decompress( &cinfo );
214    fclose( infile );
215
216    return TRUE;
217}
218
219
220SOM_Scope short  SOMLINK getBpp(LuJpegDocument *somSelf,  Environment *ev)
221{
222    return 3;
223}
224
225
226SOM_Scope boolean  SOMLINK isScalable(LuJpegDocument *somSelf,
227                                       Environment *ev)
228{
229    return TRUE;
230}
231
232SOM_Scope long  SOMLINK getPageCount(LuJpegDocument *somSelf,
233                                      Environment *ev)
234{
235    return 1;
236}
237
238
239SOM_Scope void  SOMLINK getPageSize(LuJpegDocument *somSelf,
240                                     Environment *ev, long pagenum,
241                                    double* width, double* height)
242{
243    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
244    JpegDocument *d = (JpegDocument *)somThis->data;
245
246    if ( width != NULL ) {
247        *width = d->width;
248    }
249    if ( height != NULL ) {
250        *height = d->height;
251    }
252}
253
254
255SOM_Scope boolean  SOMLINK renderPageToPixbuf(LuJpegDocument *somSelf,
256                                               Environment *ev,
257                                              long pagenum, long src_x,
258                                              long src_y, long src_width,
259                                              long src_height,
260                                              double scale, long rotation,
261                                              LuPixbuf* pixbuf,
262                                              long* errorCode,
263                                              string* error)
264{
265    if ( errorCode != NULL ) {
266        *errorCode = LU_RERR_NO_ERROR;
267    }
268
269    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
270    JpegDocument *d = (JpegDocument *)somThis->data;
271
272    //somPrintf( "src_x: %d  src_y: %d  src_width: %d  src_height: %d  scale: %f\n",
273    //            src_x, src_y, src_width, src_height, scale );
274
275    float real_src_x = (float)src_x / scale;
276    float real_src_y = (float)src_y / scale;
277    float real_src_width = (float)src_width / scale;
278    float real_src_height = (float)src_height / scale;
279
280    //somPrintf( "real_src_x: %f  real_src_y: %f  real_src_width: %f  real_src_height: %f\n",
281    //            real_src_x, real_src_y, real_src_width, real_src_height );
282
283    short bpp = getBpp( somSelf, ev );
284
285    int pb_rowstride = d->pb->getRowSize( ev );
286    int pb_width     = d->pb->getWidth( ev );
287    int pb_height    = d->pb->getHeight( ev );
288    char *pb_data    = (char *)d->pb->getDataPtr( ev );
289
290    if ( scale == 1.0 )
291    {
292        int pixbuf_rowstride = pixbuf->getRowSize( ev );
293        char *pixbuf_data = (char *)pixbuf->getDataPtr( ev );
294        char *src, *dst;
295        int i, y;
296        for ( y = pb_height-(src_y+src_height), i = 0; i < src_height; y++, i++ )
297        {
298            src = pb_data + (y * pb_rowstride) + (src_x * bpp);
299            dst = pixbuf_data + (i * pixbuf_rowstride);
300            memcpy( dst, src, src_width * bpp );
301        }
302    }
303    else
304    {
305        float y = (float)pb_height - ( real_src_y + real_src_height );
306        resample( ev, pixbuf, 0, 0, src_width - 1, src_height - 1,
307                  d->pb, real_src_x, y, real_src_width + real_src_x, real_src_height + y );
308    }
309
310    return TRUE;
311}
312
313
314SOM_Scope void SOMLINK somDefaultInit(LuJpegDocument *somSelf,
315                                      som3InitCtrl* ctrl)
316{
317    LuJpegDocumentData *somThis;
318    somInitCtrl globalCtrl;
319    somBooleanVector myMask;
320    LuJpegDocument_BeginInitializer_somDefaultInit;
321    LuJpegDocument_Init_LuDocument_somDefaultInit(somSelf, ctrl);
322
323    // local LuJpegDocument initialization code
324    JpegDocument *d = new JpegDocument;
325    memset( d, 0, sizeof( JpegDocument ) );
326    somThis->data = d;
327}
328
329
330SOM_Scope void SOMLINK somDestruct(LuJpegDocument *somSelf, octet doFree,
331                                   som3DestructCtrl* ctrl)
332{
333    LuJpegDocumentData *somThis;
334    somDestructCtrl globalCtrl;
335    somBooleanVector myMask;
336    LuJpegDocument_BeginDestructor;
337
338    // local LuJpegDocument deinitialization code
339    JpegDocument *d = (JpegDocument *)somThis->data;
340    if ( d->pb != NULL ) {
341        delete d->pb;
342    }
343    delete d;
344    // end of local LuJpegDocument deinitialization code
345
346    LuJpegDocument_EndDestructor;
347}
348
349
Note: See TracBrowser for help on using the repository browser.