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

Last change on this file since 144 was 144, checked in by Eugene Romanenko, 15 years ago

added version number of library used to plugin description

File size: 8.2 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 <string.h>
61#include <setjmp.h>
62#include <jpeglib.h>
63
64
65
66unsigned _System LibMain( unsigned hmod, unsigned termination )
67{
68    if ( termination ) {
69        /* DLL is detaching from process */
70    } else {
71        /* DLL is attaching to process */
72    }
73    return( 1 );
74}
75
76extern "C" LuDocument * _System createObject()
77{
78    return new LuJpegDocument;
79}
80
81extern "C" char * _System getSupportedExtensions()
82{
83    return "JPG;JPEG;JPE";
84}
85
86extern "C" char * _System getDescription()
87{
88    return "Jpeg plugin, based on IJG JPEG Library v6b";
89}
90
91
92struct JpegDocument
93{
94    LuPixbuf *pb;
95    double width;
96    double height;
97};
98
99struct lujpeg_error_mgr
100{
101    struct jpeg_error_mgr jem;
102    jmp_buf setjmp_buffer;
103};
104
105METHODDEF(void) lujpeg_error_exit( j_common_ptr cinfo )
106{
107    lujpeg_error_mgr *err = (lujpeg_error_mgr *)cinfo->err;
108    char buffer[ JMSG_LENGTH_MAX ];
109    (*cinfo->err->format_message)( cinfo, buffer );
110    fprintf( stderr, "JPEG decoding error:\n%s\n", buffer );
111    longjmp( err->setjmp_buffer, 1 );
112}
113
114
115SOM_Scope boolean  SOMLINK loadFile(LuJpegDocument *somSelf,
116                                     Environment *ev, string filename,
117                                    string password, string* error)
118{
119    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
120    JpegDocument *d = (JpegDocument *)somThis->data;
121
122    FILE *infile = NULL;
123    if ( ( infile = fopen( filename, "rb" ) ) == NULL ) {
124        return FALSE;
125    }
126
127    short bpp = getBpp( somSelf, ev );
128    jpeg_decompress_struct cinfo;
129    lujpeg_error_mgr ljerr;
130    cinfo.err = jpeg_std_error( &ljerr.jem );
131    ljerr.jem.error_exit = lujpeg_error_exit;
132    if ( setjmp( ljerr.setjmp_buffer ) ) {
133        // If we get here, the JPEG code has signaled an error.
134        jpeg_destroy_decompress( &cinfo );
135        fclose( infile );
136        return FALSE;
137    }
138
139    jpeg_create_decompress( &cinfo );
140    jpeg_stdio_src( &cinfo, infile );
141    jpeg_read_header( &cinfo, TRUE );
142    cinfo.out_color_space = JCS_RGB;
143    jpeg_start_decompress( &cinfo );
144
145    d->width = cinfo.output_width;
146    d->height = cinfo.output_height;
147    d->pb = new LuPixbuf( ev, cinfo.output_width, cinfo.output_height, bpp );
148
149    int pixbuf_rowstride, pixbuf_height, pixbuf_width;
150    char *pixbuf_data, *dst, *src;
151
152    pixbuf_data = (char *)d->pb->getDataPtr( ev );
153    pixbuf_width = d->pb->getWidth( ev );
154    pixbuf_height = d->pb->getHeight( ev );
155    pixbuf_rowstride = d->pb->getRowSize(ev );
156
157    JSAMPARRAY buffer;
158    int row_stride = cinfo.output_width * cinfo.output_components;
159    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
160
161    int i, j;
162    i = cinfo.output_height;
163    while ( cinfo.output_scanline < cinfo.output_height )
164    {
165        i--;
166        jpeg_read_scanlines( &cinfo, buffer, 1 );
167        dst = pixbuf_data + i * pixbuf_rowstride;
168        for ( j = 0; j < pixbuf_rowstride; j += bpp )
169        {
170            dst[ j ] = buffer[0][ j + 2 ];
171            dst[ j + 1 ] = buffer[0][ j + 1 ];
172            dst[ j + 2 ] = buffer[0][ j ];
173        }
174    }
175
176    jpeg_finish_decompress( &cinfo );
177    jpeg_destroy_decompress( &cinfo );
178    fclose( infile );
179
180    return TRUE;
181}
182
183
184SOM_Scope short  SOMLINK getBpp(LuJpegDocument *somSelf,  Environment *ev)
185{
186    return 3;
187}
188
189
190SOM_Scope long  SOMLINK getPageCount(LuJpegDocument *somSelf,
191                                      Environment *ev)
192{
193    return 1;
194}
195
196
197SOM_Scope void  SOMLINK getPageSize(LuJpegDocument *somSelf,
198                                     Environment *ev, long pagenum,
199                                    double* width, double* height)
200{
201    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
202    JpegDocument *d = (JpegDocument *)somThis->data;
203
204    if ( width != NULL ) {
205        *width = d->width;
206    }
207    if ( height != NULL ) {
208        *height = d->height;
209    }
210}
211
212
213SOM_Scope void  SOMLINK renderPageToPixbuf(LuJpegDocument *somSelf,
214                                            Environment *ev,
215                                           long pagenum, long src_x,
216                                           long src_y, long src_width,
217                                           long src_height, double scale,
218                                           long rotation, LuPixbuf* pixbuf)
219{
220    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
221    JpegDocument *d = (JpegDocument *)somThis->data;
222
223    //somPrintf( "src_x: %d  src_y: %d  src_width: %d  src_height: %d\n",
224    //            src_x, src_y, src_width, src_height );
225
226    short bpp = getBpp( somSelf, ev );
227    int pb_rowstride = d->pb->getRowSize( ev );
228    int pb_height = d->pb->getHeight( ev );
229    int pixbuf_rowstride = pixbuf->getRowSize( ev );
230    char *pb_data = (char *)d->pb->getDataPtr( ev );
231    char *pixbuf_data = (char *)pixbuf->getDataPtr( ev );
232    char *src, *dst;
233    int i, y;
234    for ( y = pb_height-(src_y+src_height), i = 0; i < src_height; y++, i++ )
235    {
236        src = pb_data + (y * pb_rowstride) + (src_x * bpp);
237        dst = pixbuf_data + (i * pixbuf_rowstride);
238        memcpy( dst, src, src_width * bpp );
239    }
240}
241
242
243SOM_Scope void SOMLINK somDefaultInit(LuJpegDocument *somSelf,
244                                      som3InitCtrl* ctrl)
245{
246    LuJpegDocumentData *somThis;
247    somInitCtrl globalCtrl;
248    somBooleanVector myMask;
249    LuJpegDocument_BeginInitializer_somDefaultInit;
250    LuJpegDocument_Init_LuDocument_somDefaultInit(somSelf, ctrl);
251
252    // local LuJpegDocument initialization code
253    JpegDocument *d = new JpegDocument;
254    memset( d, 0, sizeof( JpegDocument ) );
255    somThis->data = d;
256}
257
258
259SOM_Scope void SOMLINK somDestruct(LuJpegDocument *somSelf, octet doFree,
260                                   som3DestructCtrl* ctrl)
261{
262    LuJpegDocumentData *somThis;
263    somDestructCtrl globalCtrl;
264    somBooleanVector myMask;
265    LuJpegDocument_BeginDestructor;
266
267    // local LuJpegDocument deinitialization code
268    JpegDocument *d = (JpegDocument *)somThis->data;
269    if ( d->pb != NULL ) {
270        delete d->pb;
271    }
272    delete d;
273    // end of local LuJpegDocument deinitialization code
274
275    LuJpegDocument_EndDestructor;
276}
277
278
Note: See TracBrowser for help on using the repository browser.