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

Last change on this file since 47 was 35, checked in by Eugene Romanenko, 16 years ago

added licence block to source files

File size: 7.7 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2 * Version: CDDL 1.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the COMMON DEVELOPMENT AND
5 * DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6 * this file except in compliance with the License. You may obtain a copy of
7 * the License at http://www.sun.com/cddl/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Initial Developer of the Original Code is
15 * Eugene Romanenko, netlabs.org.
16 * Portions created by the Initial Developer are Copyright (C) 2006
17 * the Initial Developer. All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * Alternatively, the contents of this file may be used under the terms of
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the LGPL are applicable instead of those
24 * above. If you wish to allow use of your version of this file only under the
25 * terms of the LGPL, and not to allow others to use your version of this file
26 * under the terms of the CDDL, indicate your decision by deleting the
27 * provisions above and replace them with the notice and other provisions
28 * required by the LGPL. If you do not delete the provisions above, a recipient
29 * may use your version of this file under the terms of any one of the CDDL
30 * or the LGPL.
31 *
32 * ***** END LICENSE BLOCK ***** */
33
34
35
36/*
37 *  This file was generated by the SOM Compiler.
38 *  Generated using:
39 *     SOM incremental update: 2.24
40 */
41
42/*
43 *  This file was generated by the SOM Compiler and Emitter Framework.
44 *  Generated using template emitter:
45 *      SOM Emitter emitxtm: 2.23.1.9
46 */
47
48#ifndef SOM_Module_lujpeg_Source
49#define SOM_Module_lujpeg_Source
50#endif
51#define LuJpegDocument_Class_Source
52
53#include "lujpeg.xih"
54
55#define HAVE_BOOLEAN
56
57#include <stdio.h>
58#include <string.h>
59#include <jpeglib.h>
60
61
62unsigned _System LibMain( unsigned hmod, unsigned termination )
63{
64    if ( termination ) {
65        /* DLL is detaching from process */
66    } else {
67        /* DLL is attaching to process */
68    }
69    return( 1 );
70}
71
72extern "C" LuDocument * _System createObject()
73{
74    return new LuJpegDocument;
75}
76
77extern "C" char * _System getSupportedExtensions()
78{
79    return "JPG;JPEG;JPE";
80}
81
82extern "C" char * _System getDescription()
83{
84    return "Jpeg plugin, based on IJG JPEG Library.";
85}
86
87
88struct JpegDocument
89{
90    LuPixbuf *pb;
91    double width;
92    double height;
93};
94
95
96SOM_Scope boolean  SOMLINK loadFile(LuJpegDocument *somSelf,
97                                     Environment *ev, string filename,
98                                    string password, string* error)
99{
100    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
101    JpegDocument *d = (JpegDocument *)somThis->data;
102
103    FILE *infile = NULL;
104    if ( ( infile = fopen( filename, "rb" ) ) == NULL ) {
105        return FALSE;
106    }
107
108    short bpp = getBpp( somSelf, ev );
109    jpeg_decompress_struct cinfo;
110    jpeg_error_mgr jerr;
111    cinfo.err = jpeg_std_error( &jerr );
112    jpeg_create_decompress( &cinfo );
113    jpeg_stdio_src( &cinfo, infile );
114    jpeg_read_header( &cinfo, TRUE );
115    cinfo.out_color_space = JCS_RGB;
116    jpeg_start_decompress( &cinfo );
117
118    d->width = cinfo.output_width;
119    d->height = cinfo.output_height;
120    d->pb = new LuPixbuf( ev, cinfo.output_width, cinfo.output_height, bpp );
121
122    int pixbuf_rowstride, pixbuf_height, pixbuf_width;
123    char *pixbuf_data, *dst, *src;
124
125    pixbuf_data = (char *)d->pb->getDataPtr( ev );
126    pixbuf_width = d->pb->getWidth( ev );
127    pixbuf_height = d->pb->getHeight( ev );
128    pixbuf_rowstride = d->pb->getRowSize(ev );
129
130    JSAMPARRAY buffer;
131    int row_stride = cinfo.output_width * cinfo.output_components;
132    buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
133
134    int i, j;
135    i = cinfo.output_height;
136    while ( cinfo.output_scanline < cinfo.output_height )
137    {
138        i--;
139        jpeg_read_scanlines( &cinfo, buffer, 1 );
140        dst = pixbuf_data + i * pixbuf_rowstride;
141        for ( j = 0; j < pixbuf_rowstride; j += bpp )
142        {
143            dst[ j ] = buffer[0][ j + 2 ];
144            dst[ j + 1 ] = buffer[0][ j + 1 ];
145            dst[ j + 2 ] = buffer[0][ j ];
146        }
147    }
148
149    jpeg_finish_decompress( &cinfo );
150    jpeg_destroy_decompress( &cinfo );
151    fclose( infile );
152
153    return TRUE;
154}
155
156SOM_Scope short  SOMLINK getBpp(LuJpegDocument *somSelf,  Environment *ev)
157{
158    return 3;
159}
160
161SOM_Scope long  SOMLINK getPageCount(LuJpegDocument *somSelf,
162                                      Environment *ev)
163{
164    return 1;
165}
166
167SOM_Scope void  SOMLINK getPageSize(LuJpegDocument *somSelf,
168                                     Environment *ev, long pagenum,
169                                    double* width, double* height)
170{
171    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
172    JpegDocument *d = (JpegDocument *)somThis->data;
173
174    if ( width != NULL ) {
175        *width = d->width;
176    }
177    if ( height != NULL ) {
178        *height = d->height;
179    }
180}
181
182SOM_Scope void  SOMLINK renderPageToPixbuf(LuJpegDocument *somSelf,
183                                            Environment *ev,
184                                           long pagenum, long src_x,
185                                           long src_y, long src_width,
186                                           long src_height, double scale,
187                                           long rotation, LuPixbuf* pixbuf)
188{
189    LuJpegDocumentData *somThis = LuJpegDocumentGetData(somSelf);
190    JpegDocument *d = (JpegDocument *)somThis->data;
191
192    //somPrintf( "src_x: %d  src_y: %d  src_width: %d  src_height: %d\n",
193    //            src_x, src_y, src_width, src_height );
194
195    short bpp = getBpp( somSelf, ev );
196    int pb_rowstride = d->pb->getRowSize( ev );
197    int pb_height = d->pb->getHeight( ev );
198    int pixbuf_rowstride = pixbuf->getRowSize( ev );
199    char *pb_data = (char *)d->pb->getDataPtr( ev );
200    char *pixbuf_data = (char *)pixbuf->getDataPtr( ev );
201    char *src, *dst;
202    int i, y;
203    for ( y = pb_height-(src_y+src_height), i = 0; i < src_height; y++, i++ )
204    {
205        src = pb_data + (y * pb_rowstride) + (src_x * bpp);
206        dst = pixbuf_data + (i * pixbuf_rowstride);
207        memcpy( dst, src, src_width * bpp );
208    }
209    //long len = __min( pixbuf->getDataLen(ev), d->pb->getDataLen(ev) );
210    //memcpy( pixbuf->getDataPtr(ev), d->pb->getDataPtr(ev), len );
211}
212
213SOM_Scope void SOMLINK somDefaultInit(LuJpegDocument *somSelf,
214                                      som3InitCtrl* ctrl)
215{
216    LuJpegDocumentData *somThis;
217    somInitCtrl globalCtrl;
218    somBooleanVector myMask;
219    LuJpegDocument_BeginInitializer_somDefaultInit;
220    LuJpegDocument_Init_LuDocument_somDefaultInit(somSelf, ctrl);
221
222    // local LuJpegDocument initialization code
223    JpegDocument *d = new JpegDocument;
224    memset( d, 0, sizeof( JpegDocument ) );
225    somThis->data = d;
226}
227
228
229SOM_Scope void SOMLINK somDestruct(LuJpegDocument *somSelf, octet doFree,
230                                   som3DestructCtrl* ctrl)
231{
232    LuJpegDocumentData *somThis;
233    somDestructCtrl globalCtrl;
234    somBooleanVector myMask;
235    LuJpegDocument_BeginDestructor;
236
237    // local LuJpegDocument deinitialization code
238    JpegDocument *d = (JpegDocument *)somThis->data;
239    if ( d->pb != NULL ) {
240            delete d->pb;
241        }
242    delete d;
243    // end of local LuJpegDocument deinitialization code
244
245    LuJpegDocument_EndDestructor;
246}
247
248
Note: See TracBrowser for help on using the repository browser.