source: branches/kmk/Lucide/plugins/lujpeg/lujpeg.cpp @ 337

Last change on this file since 337 was 284, checked in by rbri, 12 years ago

JPEG plugin: libjpeg updated to version 7

File size: 10.3 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#define HAVE_BOOLEAN
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <setjmp.h>
63#include <jpeglib.h>
64
65
66void resample( Environment *ev,
67               LuPixbuf *out, int out_x0, int out_y0, int out_x1, int out_y1,
68               LuPixbuf *in, float in_x0, float in_y0, float in_x1, float in_y1 );
69
70unsigned EXPENTRY LibMain( unsigned hmod, unsigned termination )
71{
72    if ( termination ) {
73        /* DLL is detaching from process */
74    } else {
75        /* DLL is attaching to process */
76    }
77    return( 1 );
78}
79
80
81extern "C" LuDocument * EXPENTRY createObject()
82{
83    return new LuJpegDocument;
84}
85
86extern "C" char * EXPENTRY getSupportedExtensions()
87{
88    return "JPG;JPEG;JPE";
89}
90
91static LuSignature      lsig = { 0, 0, 2, (void *)"\xff\xd8" };
92static LuSignatureList  lsl  = { 1, &lsig };
93static LuSignatureCheck lsc  = { 1, &lsl };
94
95// getSignatureCheck is optional
96extern "C" LuSignatureCheck * EXPENTRY getSignatureCheck()
97{
98    return &lsc;
99}
100
101
102extern "C" char * EXPENTRY getDescription()
103{
104    return "Jpeg plugin, based on IJG JPEG Library v7";
105}
106
107
108struct JpegDocument
109{
110    LuPixbuf *pb;
111    double width;
112    double height;
113};
114
115struct lujpeg_error_mgr
116{
117    struct jpeg_error_mgr jem;
118    jmp_buf setjmp_buffer;
119};
120
121METHODDEF(void) lujpeg_error_exit( j_common_ptr cinfo )
122{
123    lujpeg_error_mgr *err = (lujpeg_error_mgr *)cinfo->err;
124    char buffer[ JMSG_LENGTH_MAX ];
125    (*cinfo->err->format_message)( cinfo, buffer );
126    fprintf( stderr, "JPEG decoding error:\n%s\n", buffer );
127    longjmp( err->setjmp_buffer, 1 );
128}
129
130
131
132SOM_Scope boolean  SOMLINK loadFile(LuJpegDocument *somSelf,
133                                     Environment *ev, string filename,
134                                    string password, long* errorCode,
135                                    string* error)
136{
137    if ( errorCode != NULL ) {
138        *errorCode = LU_LDERR_NO_ERROR;
139    }
140
141    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
142    JpegDocument *d = (JpegDocument *)somThis->data;
143
144    FILE *infile = NULL;
145    if ( ( infile = fopen( filename, "rb" ) ) == NULL ) {
146        if ( errorCode != NULL ) {
147            *errorCode = LU_LDERR_OPEN_ERROR;
148        }
149        return FALSE;
150    }
151
152    short bpp = getBpp( somSelf, ev );
153    jpeg_decompress_struct cinfo;
154    lujpeg_error_mgr ljerr;
155    cinfo.err = jpeg_std_error( &ljerr.jem );
156    ljerr.jem.error_exit = lujpeg_error_exit;
157    if ( setjmp( ljerr.setjmp_buffer ) ) {
158        // If we get here, the JPEG code has signaled an error.
159        jpeg_destroy_decompress( &cinfo );
160        fclose( infile );
161        if ( errorCode != NULL ) {
162            *errorCode = LU_LDERR_WRONG_FORMAT;
163        }
164        return FALSE;
165    }
166
167    jpeg_create_decompress( &cinfo );
168    jpeg_stdio_src( &cinfo, infile );
169    jpeg_read_header( &cinfo, TRUE );
170    cinfo.out_color_space = JCS_RGB;
171    jpeg_start_decompress( &cinfo );
172
173    d->width = cinfo.output_width;
174    d->height = cinfo.output_height;
175    d->pb = new LuPixbuf( ev, cinfo.output_width, cinfo.output_height, bpp );
176
177    int pixbuf_rowstride, pixbuf_height, pixbuf_width;
178    char *pixbuf_data, *dst, *src;
179
180    pixbuf_data = (char *)d->pb->getDataPtr( ev );
181    pixbuf_width = d->pb->getWidth( ev );
182    pixbuf_height = d->pb->getHeight( ev );
183    pixbuf_rowstride = d->pb->getRowSize(ev );
184
185    JSAMPARRAY buffer;
186    int row_stride = cinfo.output_width * cinfo.output_components;
187    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
188
189    int i, j;
190    i = cinfo.output_height;
191    while ( cinfo.output_scanline < cinfo.output_height )
192    {
193        i--;
194        jpeg_read_scanlines( &cinfo, buffer, 1 );
195        dst = pixbuf_data + ( i * pixbuf_rowstride );
196        int len = pixbuf_width * bpp;
197        for ( j = 0; j < len; j += bpp )
198        {
199            dst[ j ] = buffer[0][ j + 2 ];
200            dst[ j + 1 ] = buffer[0][ j + 1 ];
201            dst[ j + 2 ] = buffer[0][ j ];
202        }
203    }
204
205    jpeg_finish_decompress( &cinfo );
206    jpeg_destroy_decompress( &cinfo );
207    fclose( infile );
208
209    return TRUE;
210}
211
212
213SOM_Scope short  SOMLINK getBpp(LuJpegDocument *somSelf,  Environment *ev)
214{
215    return 3;
216}
217
218
219SOM_Scope boolean  SOMLINK isScalable(LuJpegDocument *somSelf,
220                                       Environment *ev)
221{
222    return TRUE;
223}
224
225SOM_Scope long  SOMLINK getPageCount(LuJpegDocument *somSelf,
226                                      Environment *ev)
227{
228    return 1;
229}
230
231
232SOM_Scope void  SOMLINK getPageSize(LuJpegDocument *somSelf,
233                                     Environment *ev, long pagenum,
234                                    double* width, double* height)
235{
236    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
237    JpegDocument *d = (JpegDocument *)somThis->data;
238
239    if ( width != NULL ) {
240        *width = d->width;
241    }
242    if ( height != NULL ) {
243        *height = d->height;
244    }
245}
246
247
248SOM_Scope boolean  SOMLINK renderPageToPixbuf(LuJpegDocument *somSelf,
249                                               Environment *ev,
250                                              long pagenum, long src_x,
251                                              long src_y, long src_width,
252                                              long src_height,
253                                              double scale, long rotation,
254                                              LuPixbuf* pixbuf,
255                                              long* errorCode,
256                                              string* error)
257{
258    if ( errorCode != NULL ) {
259        *errorCode = LU_RERR_NO_ERROR;
260    }
261
262    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
263    JpegDocument *d = (JpegDocument *)somThis->data;
264
265    //somPrintf( "src_x: %d  src_y: %d  src_width: %d  src_height: %d  scale: %f\n",
266    //            src_x, src_y, src_width, src_height, scale );
267
268    float real_src_x = (float)src_x / scale;
269    float real_src_y = (float)src_y / scale;
270    float real_src_width = (float)src_width / scale;
271    float real_src_height = (float)src_height / scale;
272
273    //somPrintf( "real_src_x: %f  real_src_y: %f  real_src_width: %f  real_src_height: %f\n",
274    //            real_src_x, real_src_y, real_src_width, real_src_height );
275
276    short bpp = getBpp( somSelf, ev );
277
278    int pb_rowstride = d->pb->getRowSize( ev );
279    int pb_width     = d->pb->getWidth( ev );
280    int pb_height    = d->pb->getHeight( ev );
281    char *pb_data    = (char *)d->pb->getDataPtr( ev );
282
283    if ( scale == 1.0 )
284    {
285        int pixbuf_rowstride = pixbuf->getRowSize( ev );
286        char *pixbuf_data = (char *)pixbuf->getDataPtr( ev );
287        char *src, *dst;
288        int i, y;
289        for ( y = pb_height-(src_y+src_height), i = 0; i < src_height; y++, i++ )
290        {
291            src = pb_data + (y * pb_rowstride) + (src_x * bpp);
292            dst = pixbuf_data + (i * pixbuf_rowstride);
293            memcpy( dst, src, src_width * bpp );
294        }
295    }
296    else
297    {
298        float y = (float)pb_height - ( real_src_y + real_src_height );
299        resample( ev, pixbuf, 0, 0, src_width - 1, src_height - 1,
300                  d->pb, real_src_x, y, real_src_width + real_src_x, real_src_height + y );
301    }
302
303    return TRUE;
304}
305
306
307SOM_Scope void SOMLINK somDefaultInit(LuJpegDocument *somSelf,
308                                      som3InitCtrl* ctrl)
309{
310    LuJpegDocumentData *somThis;
311    somInitCtrl globalCtrl;
312    somBooleanVector myMask;
313    LuJpegDocument_BeginInitializer_somDefaultInit;
314    LuJpegDocument_Init_LuDocument_somDefaultInit(somSelf, ctrl);
315
316    // local LuJpegDocument initialization code
317    JpegDocument *d = new JpegDocument;
318    memset( d, 0, sizeof( JpegDocument ) );
319    somThis->data = d;
320}
321
322
323SOM_Scope void SOMLINK somDestruct(LuJpegDocument *somSelf, octet doFree,
324                                   som3DestructCtrl* ctrl)
325{
326    LuJpegDocumentData *somThis;
327    somDestructCtrl globalCtrl;
328    somBooleanVector myMask;
329    LuJpegDocument_BeginDestructor;
330
331    // local LuJpegDocument deinitialization code
332    JpegDocument *d = (JpegDocument *)somThis->data;
333    if ( d->pb != NULL ) {
334        delete d->pb;
335    }
336    delete d;
337    // end of local LuJpegDocument deinitialization code
338
339    LuJpegDocument_EndDestructor;
340}
341
342
Note: See TracBrowser for help on using the repository browser.