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

Last change on this file since 214 was 214, checked in by Eugene Romanenko, 14 years ago

Quality scaling in JPEG plugin (still buggy)

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