source: trunk/Lucide/SOURCE/plugins/lujpeg/lujpeg.cpp @ 215

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