1 | |
---|
2 | /* |
---|
3 | * This file was generated by the SOM Compiler. |
---|
4 | * Generated using: |
---|
5 | * SOM incremental update: 2.24 |
---|
6 | */ |
---|
7 | |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2006, Eugene Romanenko, netlabs.org |
---|
11 | * |
---|
12 | *---------------------------------------------------------------------- |
---|
13 | * This file is part of djvu plugin for Lucide (ludjvu). |
---|
14 | * |
---|
15 | * ludjvu is free software; you can redistribute it and/or modify |
---|
16 | * it under the terms of the GNU General Public License as published by |
---|
17 | * the Free Software Foundation; either version 2 of the License, or |
---|
18 | * (at your option) any later version. |
---|
19 | * |
---|
20 | * ludjvu is distributed in the hope that it will be useful, |
---|
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
23 | * GNU General Public License for more details. |
---|
24 | *---------------------------------------------------------------------- |
---|
25 | */ |
---|
26 | |
---|
27 | |
---|
28 | /* |
---|
29 | * This file was generated by the SOM Compiler and Emitter Framework. |
---|
30 | * Generated using template emitter: |
---|
31 | * SOM Emitter emitxtm: 2.23.1.9 |
---|
32 | */ |
---|
33 | |
---|
34 | #ifndef SOM_Module_evdjvu_Source |
---|
35 | #define SOM_Module_evdjvu_Source |
---|
36 | #endif |
---|
37 | #define LuDjvuDocument_Class_Source |
---|
38 | |
---|
39 | #include "ludjvu.xih" |
---|
40 | #ifdef __WATCOMC__ |
---|
41 | #include <ddjvuapiw.h> |
---|
42 | #else |
---|
43 | #include <ddjvuapi.h> |
---|
44 | #endif |
---|
45 | #ifdef __GNUC__ |
---|
46 | #define __min(a,b) (((a) < (b)) ? (a) : (b)) |
---|
47 | #endif |
---|
48 | |
---|
49 | #define SCALE_FACTOR 0.2 |
---|
50 | |
---|
51 | |
---|
52 | struct DjvuDocument |
---|
53 | { |
---|
54 | ddjvu_context_t *d_context; |
---|
55 | ddjvu_document_t *d_document; |
---|
56 | ddjvu_format_t *d_format; |
---|
57 | }; |
---|
58 | |
---|
59 | |
---|
60 | unsigned _System LibMain( unsigned hmod, unsigned termination ) |
---|
61 | { |
---|
62 | if ( termination ) { |
---|
63 | /* DLL is detaching from process */ |
---|
64 | } else { |
---|
65 | /* DLL is attaching to process */ |
---|
66 | } |
---|
67 | return( 1 ); |
---|
68 | } |
---|
69 | |
---|
70 | extern "C" LuDocument * _System createObject() |
---|
71 | { |
---|
72 | return new LuDjvuDocument; |
---|
73 | } |
---|
74 | |
---|
75 | extern "C" char * _System getSupportedExtensions() |
---|
76 | { |
---|
77 | return "DJVU;DJV"; |
---|
78 | } |
---|
79 | |
---|
80 | extern "C" char * _System getDescription() |
---|
81 | { |
---|
82 | return "DjVu plugin, based on DjVuLibre v3.5.17"; |
---|
83 | } |
---|
84 | |
---|
85 | |
---|
86 | static void djvu_handle_events( ddjvu_context_t *ctx ) |
---|
87 | { |
---|
88 | const ddjvu_message_t *msg; |
---|
89 | |
---|
90 | if ( ctx == NULL ) { |
---|
91 | return; |
---|
92 | } |
---|
93 | |
---|
94 | msg = ddjvu_message_wait( ctx ); |
---|
95 | while ( ( msg = ddjvu_message_peek( ctx ) ) ) |
---|
96 | { |
---|
97 | switch ( msg->m_any.tag ) |
---|
98 | { |
---|
99 | case DDJVU_ERROR: |
---|
100 | // TODO: error message |
---|
101 | default: |
---|
102 | break; |
---|
103 | } |
---|
104 | ddjvu_message_pop( ctx ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | |
---|
109 | SOM_Scope boolean SOMLINK loadFile(LuDjvuDocument *somSelf, |
---|
110 | Environment *ev, string filename, |
---|
111 | string password, long* errorCode, |
---|
112 | string* error) |
---|
113 | { |
---|
114 | if ( errorCode != NULL ) { |
---|
115 | *errorCode = LU_LDERR_NO_ERROR; |
---|
116 | } |
---|
117 | |
---|
118 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
119 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
120 | ddjvu_document_t *doc; |
---|
121 | |
---|
122 | doc = ddjvu_document_create_by_filename( d->d_context, filename, TRUE ); |
---|
123 | |
---|
124 | if ( !doc ) { |
---|
125 | return FALSE; |
---|
126 | } |
---|
127 | |
---|
128 | if ( d->d_document ) { |
---|
129 | ddjvu_document_release( d->d_document ); |
---|
130 | } |
---|
131 | |
---|
132 | d->d_document = doc; |
---|
133 | |
---|
134 | while ( !ddjvu_document_decoding_done( d->d_document ) ) { |
---|
135 | djvu_handle_events( d->d_context ); |
---|
136 | } |
---|
137 | |
---|
138 | return TRUE; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | SOM_Scope short SOMLINK getBpp(LuDjvuDocument *somSelf, Environment *ev) |
---|
143 | { |
---|
144 | return 3; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | SOM_Scope boolean SOMLINK isScalable(LuDjvuDocument *somSelf, |
---|
149 | Environment *ev) |
---|
150 | { |
---|
151 | return TRUE; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | SOM_Scope long SOMLINK getPageCount(LuDjvuDocument *somSelf, |
---|
156 | Environment *ev) |
---|
157 | { |
---|
158 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
159 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
160 | return ddjvu_document_get_pagenum( d->d_document ); |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | SOM_Scope void SOMLINK getPageSize(LuDjvuDocument *somSelf, |
---|
165 | Environment *ev, long pagenum, |
---|
166 | double* width, double* height) |
---|
167 | { |
---|
168 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
169 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
170 | |
---|
171 | ddjvu_pageinfo_t info = { 0 }; |
---|
172 | |
---|
173 | while ( ddjvu_document_get_pageinfo( d->d_document, pagenum, &info ) < DDJVU_JOB_OK ) { |
---|
174 | djvu_handle_events( d->d_context ); |
---|
175 | } |
---|
176 | |
---|
177 | if ( width != NULL ) { |
---|
178 | *width = info.width * SCALE_FACTOR; |
---|
179 | } |
---|
180 | if ( height != NULL ) { |
---|
181 | *height = info.height * SCALE_FACTOR; |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | SOM_Scope boolean SOMLINK renderPageToPixbuf(LuDjvuDocument *somSelf, |
---|
187 | Environment *ev, |
---|
188 | long pagenum, long src_x, |
---|
189 | long src_y, long src_width, |
---|
190 | long src_height, |
---|
191 | double scale, long rotation, |
---|
192 | LuPixbuf* pixbuf, |
---|
193 | long* errorCode, |
---|
194 | string* error) |
---|
195 | { |
---|
196 | if ( errorCode != NULL ) { |
---|
197 | *errorCode = LU_RERR_NO_ERROR; |
---|
198 | } |
---|
199 | |
---|
200 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
201 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
202 | |
---|
203 | //somPrintf( "src_x: %d src_y: %d src_width: %d src_height: %d\n", |
---|
204 | // src_x, src_y, src_width, src_height ); |
---|
205 | |
---|
206 | ddjvu_rect_t rrect; |
---|
207 | ddjvu_rect_t prect; |
---|
208 | ddjvu_page_t *d_page; |
---|
209 | |
---|
210 | double page_width, page_height; |
---|
211 | |
---|
212 | d_page = ddjvu_page_create_by_pageno( d->d_document, pagenum ); |
---|
213 | |
---|
214 | while ( !ddjvu_page_decoding_done( d_page ) ) { |
---|
215 | djvu_handle_events( d->d_context ); |
---|
216 | } |
---|
217 | |
---|
218 | page_width = ddjvu_page_get_width( d_page ) * scale * SCALE_FACTOR; |
---|
219 | page_height = ddjvu_page_get_height( d_page ) * scale * SCALE_FACTOR; |
---|
220 | |
---|
221 | prect.x = 0; |
---|
222 | prect.y = 0; |
---|
223 | prect.w = (int)( page_width + 0.5 ); |
---|
224 | prect.h = (int)( page_height + 0.5 ); |
---|
225 | rrect.x = src_x; |
---|
226 | rrect.y = ( prect.h - src_y ) - src_height; |
---|
227 | rrect.w = src_width; |
---|
228 | rrect.h = src_height; |
---|
229 | |
---|
230 | long pixbuf_rowsize = pixbuf->getRowSize( ev ); |
---|
231 | long pixbuf_height = pixbuf->getHeight( ev ); |
---|
232 | short bpp = getBpp( somSelf, ev ); |
---|
233 | |
---|
234 | LuPixbuf *pb = new LuPixbuf( ev, prect.w, prect.h, bpp ); |
---|
235 | long pb_rowsize = pb->getRowSize( ev ); |
---|
236 | long pb_height = pb->getHeight( ev ); |
---|
237 | char *pbbuf = (char *)pb->getDataPtr( ev ); |
---|
238 | ddjvu_page_render( d_page, DDJVU_RENDER_COLOR, |
---|
239 | &prect, &rrect, d->d_format, |
---|
240 | pb_rowsize, pbbuf ); |
---|
241 | |
---|
242 | char *pixbuf_data = (char *)pixbuf->getDataPtr( ev ); |
---|
243 | char *src, *dst; |
---|
244 | int i, y; |
---|
245 | int rowsize = __min( pb_rowsize, pixbuf_rowsize ); |
---|
246 | int height = __min( pb_height, pixbuf_height ); |
---|
247 | for ( y = (height-1), i = 0; i < height; y--, i++ ) |
---|
248 | { |
---|
249 | src = pbbuf + (y * pb_rowsize); |
---|
250 | dst = pixbuf_data + (i * pixbuf_rowsize); |
---|
251 | memcpy( dst, src, rowsize ); |
---|
252 | } |
---|
253 | delete pb; |
---|
254 | |
---|
255 | return TRUE; |
---|
256 | } |
---|
257 | |
---|
258 | |
---|
259 | SOM_Scope boolean SOMLINK getThumbnailSize(LuDjvuDocument *somSelf, |
---|
260 | Environment *ev, |
---|
261 | long pagenum, |
---|
262 | short suggested_width, |
---|
263 | short* width, short* height) |
---|
264 | { |
---|
265 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
266 | |
---|
267 | double page_ratio; |
---|
268 | double p_width = 0, p_height = 0; |
---|
269 | getPageSize( somSelf, ev, pagenum, &p_width, &p_height ); |
---|
270 | |
---|
271 | page_ratio = p_height / p_width; |
---|
272 | *width = suggested_width; |
---|
273 | *height = (short)( suggested_width * page_ratio ); |
---|
274 | |
---|
275 | return TRUE; |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | SOM_Scope LuPixbuf* SOMLINK getThumbnail(LuDjvuDocument *somSelf, |
---|
280 | Environment *ev, long pagenum, |
---|
281 | short suggested_width) |
---|
282 | { |
---|
283 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
284 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
285 | |
---|
286 | short thumb_width = 0, thumb_height = 0; |
---|
287 | getThumbnailSize( somSelf, ev, pagenum, suggested_width, &thumb_width, &thumb_height ); |
---|
288 | |
---|
289 | while ( ddjvu_thumbnail_status( d->d_document, pagenum, 1 ) < DDJVU_JOB_OK ) { |
---|
290 | djvu_handle_events( d->d_context ); |
---|
291 | } |
---|
292 | |
---|
293 | int t_width = thumb_width; |
---|
294 | int t_height = thumb_height; |
---|
295 | short bpp = getBpp( somSelf, ev ); |
---|
296 | LuPixbuf *tmppb = new LuPixbuf( ev, thumb_width, thumb_height, bpp ); |
---|
297 | char *tmppb_data = (char *)tmppb->getDataPtr( ev ); |
---|
298 | int tmppb_rowstride = tmppb->getRowSize( ev ); |
---|
299 | |
---|
300 | ddjvu_thumbnail_render( d->d_document, pagenum, &t_width, &t_height, |
---|
301 | d->d_format, tmppb_rowstride, tmppb_data ); |
---|
302 | |
---|
303 | LuPixbuf *pixbuf = new LuPixbuf( ev, thumb_width, thumb_height, bpp ); |
---|
304 | char *pixbuf_data = (char *)pixbuf->getDataPtr( ev ); |
---|
305 | int pixbuf_rowstride = pixbuf->getRowSize( ev ); |
---|
306 | char *src, *dst; |
---|
307 | int i, j; |
---|
308 | for ( i = 0, j = ( thumb_height - 1 ); i < thumb_height; i++, j-- ) |
---|
309 | { |
---|
310 | src = tmppb_data + ( j * tmppb_rowstride ); |
---|
311 | dst = pixbuf_data + (i * pixbuf_rowstride); |
---|
312 | memcpy( dst, src, pixbuf_rowstride ); |
---|
313 | } |
---|
314 | delete tmppb; |
---|
315 | |
---|
316 | return pixbuf; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | SOM_Scope boolean SOMLINK isSaveable(LuDjvuDocument *somSelf, |
---|
321 | Environment *ev) |
---|
322 | { |
---|
323 | #ifdef __GNUC__ |
---|
324 | return TRUE; |
---|
325 | #else |
---|
326 | return FALSE; |
---|
327 | #endif |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | SOM_Scope boolean SOMLINK saveAs(LuDjvuDocument *somSelf, Environment *ev, |
---|
332 | string filename) |
---|
333 | { |
---|
334 | #ifdef __GNUC__ |
---|
335 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
336 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
337 | |
---|
338 | FILE *f = NULL; |
---|
339 | if ( ( f = fopen( filename, "wb" ) ) == NULL ) { |
---|
340 | return FALSE; |
---|
341 | } |
---|
342 | |
---|
343 | ddjvu_job_t *job = ddjvu_document_save( d->d_document, f, 0, NULL ); |
---|
344 | while ( !ddjvu_job_done( job ) ) { |
---|
345 | djvu_handle_events( d->d_context ); |
---|
346 | } |
---|
347 | fclose( f ); |
---|
348 | |
---|
349 | return TRUE; |
---|
350 | #else |
---|
351 | return FALSE; |
---|
352 | #endif |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | SOM_Scope boolean SOMLINK isPostScriptExportable(LuDjvuDocument *somSelf, |
---|
357 | Environment *ev) |
---|
358 | { |
---|
359 | #ifdef __GNUC__ |
---|
360 | return TRUE; |
---|
361 | #else |
---|
362 | return FALSE; |
---|
363 | #endif |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | SOM_Scope boolean SOMLINK exportToPostScript(LuDjvuDocument *somSelf, |
---|
368 | Environment *ev, string filename, |
---|
369 | long first_page, long last_page, |
---|
370 | double width, double height, |
---|
371 | boolean* brkExport) |
---|
372 | { |
---|
373 | #ifdef __GNUC__ |
---|
374 | LuDjvuDocumentData *somThis = LuDjvuDocumentGetData(somSelf); |
---|
375 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
376 | |
---|
377 | FILE *f = NULL; |
---|
378 | if ( ( f = fopen( filename, "wb" ) ) == NULL ) { |
---|
379 | return FALSE; |
---|
380 | } |
---|
381 | |
---|
382 | char pgbuf[ 20 ]; |
---|
383 | snprintf( pgbuf, 20, "-page=%d-%d", first_page + 1, last_page + 1 ); |
---|
384 | const char *optv[] = { pgbuf }; |
---|
385 | |
---|
386 | ddjvu_job_t *job = ddjvu_document_print( d->d_document, f, 1, optv ); |
---|
387 | while ( !ddjvu_job_done( job ) && !(*brkExport) ) { |
---|
388 | djvu_handle_events( d->d_context ); |
---|
389 | } |
---|
390 | fclose( f ); |
---|
391 | |
---|
392 | return TRUE; |
---|
393 | #else |
---|
394 | return FALSE; |
---|
395 | #endif |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | SOM_Scope boolean SOMLINK isFixedImage(LuDjvuDocument *somSelf, |
---|
400 | Environment *ev) |
---|
401 | { |
---|
402 | return FALSE; |
---|
403 | } |
---|
404 | |
---|
405 | |
---|
406 | SOM_Scope boolean SOMLINK isCreateFileThumbnail(LuDjvuDocument *somSelf, |
---|
407 | Environment *ev) |
---|
408 | { |
---|
409 | return TRUE; |
---|
410 | } |
---|
411 | |
---|
412 | |
---|
413 | SOM_Scope void SOMLINK somDefaultInit(LuDjvuDocument *somSelf, |
---|
414 | som3InitCtrl* ctrl) |
---|
415 | { |
---|
416 | LuDjvuDocumentData *somThis; |
---|
417 | somInitCtrl globalCtrl; |
---|
418 | somBooleanVector myMask; |
---|
419 | LuDjvuDocument_BeginInitializer_somDefaultInit; |
---|
420 | LuDjvuDocument_Init_LuDocument_somDefaultInit(somSelf, ctrl); |
---|
421 | |
---|
422 | // local LuDjvuDocument initialization code |
---|
423 | DjvuDocument *d = new DjvuDocument; |
---|
424 | memset( d, 0, sizeof( DjvuDocument ) ); |
---|
425 | d->d_context = ddjvu_context_create("Lucide"); |
---|
426 | d->d_format = ddjvu_format_create( DDJVU_FORMAT_BGR24, 0, 0 ); |
---|
427 | ddjvu_format_set_row_order( d->d_format, 1 ); |
---|
428 | d->d_document = NULL; |
---|
429 | |
---|
430 | somThis->data = d; |
---|
431 | } |
---|
432 | |
---|
433 | |
---|
434 | SOM_Scope void SOMLINK somDestruct(LuDjvuDocument *somSelf, octet doFree, |
---|
435 | som3DestructCtrl* ctrl) |
---|
436 | { |
---|
437 | LuDjvuDocumentData *somThis; // set in BeginDestructor |
---|
438 | somDestructCtrl globalCtrl; |
---|
439 | somBooleanVector myMask; |
---|
440 | LuDjvuDocument_BeginDestructor; |
---|
441 | |
---|
442 | // local LuDjvuDocument deinitialization code |
---|
443 | DjvuDocument *d = (DjvuDocument *)somThis->data; |
---|
444 | if ( d->d_document ) { |
---|
445 | ddjvu_document_release( d->d_document ); |
---|
446 | } |
---|
447 | ddjvu_context_release( d->d_context ); |
---|
448 | ddjvu_format_release( d->d_format ); |
---|
449 | delete d; |
---|
450 | // end of local LuDjvuDocument deinitialization code |
---|
451 | |
---|
452 | LuDjvuDocument_EndDestructor; |
---|
453 | } |
---|
454 | |
---|
455 | |
---|