2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.h"
47 #include "magick/constitute.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/list.h"
51 #include "magick/magick.h"
52 #include "magick/memory_.h"
53 #include "magick/monitor.h"
54 #include "magick/monitor-private.h"
55 #include "magick/quantum-private.h"
56 #include "magick/static.h"
57 #include "magick/string_.h"
58 #include "magick/module.h"
59 #if defined(MAGICKCORE_DJVU_DELEGATE)
60 #include <libdjvu/ddjvuapi.h>
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % IsDJVU() returns MagickTrue if the image format type, identified by the
75 % magick string, is DJVU.
77 % The format of the IsDJVU method is:
79 % MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
81 % A description of each parameter follows:
83 % o magick: compare image format pattern against these bytes.
85 % o length: Specifies the length of the magick string.
88 static MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
92 if (memcmp(magick,"AT&TFORM",8) == 0)
97 #if defined(MAGICKCORE_DJVU_DELEGATE)
99 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % R e a d D J V U I m a g e %
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % ReadDJVUImage() reads DJVU image and returns it. It allocates the memory
110 % necessary for the new Image structure and returns a pointer to the new
111 % image or set of images.
113 % The format of the ReadDJVUImage method is:
115 % Image *ReadDJVUImage(const ImageInfo *image_info,
116 % ExceptionInfo *exception)
118 % A description of each parameter follows:
120 % o image_info: the image info.
122 % o exception: return any errors or warnings in this structure.
126 #if defined(__cplusplus) || defined(c_plusplus)
130 typedef struct _LoadContext
135 ddjvu_context_t* context;
136 ddjvu_document_t *document;
143 #define BLOCKSIZE 65536
146 pump_data(Image *image, LoadContext* lc)
148 int blocksize = BLOCKSIZE;
149 char data[BLOCKSIZE];
152 /* i might check for a condition! */
153 while ((size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
154 ddjvu_stream_write(lc->document, lc->streamid, data, size);
157 ddjvu_stream_write(lc->document, lc->streamid, data, size);
158 ddjvu_stream_close(lc->document, lc->streamid, 0);
162 /* returns NULL only after all is delivered! */
163 static ddjvu_message_t*
164 pump_data_until_message(LoadContext *lc,Image *image) /* ddjvu_context_t *context, type ddjvu_document_type_t */
166 size_t blocksize = BLOCKSIZE;
167 unsigned char data[BLOCKSIZE];
169 ddjvu_message_t *message;
171 /* i might check for a condition! */
172 while (!(message = ddjvu_message_peek(lc->context))
173 && (size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
174 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
179 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
180 ddjvu_stream_close(lc->document, lc->streamid, 0);
187 message_tag_name(ddjvu_message_tag_t tag)
189 static char* names[] =
202 if (tag <= DDJVU_PROGRESS)
211 /* write out nice info on the message,
212 * and store in *user* data the info on progress.
215 process_message(ddjvu_message_t *message)
219 ddjvu_context_t* context= message->m_any.context;
225 printf("*** %s: %s.\n",__FUNCTION__, message_tag_name(message->m_any.tag));
229 switch (message->m_any.tag){
232 ddjvu_document_t* document= message->m_any.document;
233 /* ddjvu_document_decoding_status is set by libdjvu! */
234 /* we have some info on the document */
235 LoadContext *lc = (LoadContext *) ddjvu_document_get_user_data(document);
236 lc->pages = ddjvu_document_get_pagenum(document);
238 printf("the doc has %d pages\n", ddjvu_document_get_pagenum(document));
244 printf("the name of the chunk is: %s\n", message->m_chunk.chunkid);
253 ddjvu_page_t* page = message->m_any.page;
254 page_info* info = ddjvu_page_get_user_data(page);
256 printf("page decoding status: %d %s%s%s\n",
257 ddjvu_page_decoding_status(page),
258 status_color, status_name(ddjvu_page_decoding_status(page)), color_reset);
260 printf("the page LAYOUT changed: width x height: %d x %d @ %d dpi. Version %d, type %d\n",
261 // printf("page info:\n width x height: %d x %d @ %d dpi, version %d, type %d\n",
262 ddjvu_page_get_width(page),
263 ddjvu_page_get_height(page),
264 ddjvu_page_get_resolution(page),
265 ddjvu_page_get_version(page),
266 /* DDJVU_PAGETYPE_BITONAL */
267 ddjvu_page_get_type(page));
274 case DDJVU_REDISPLAY:
278 ddjvu_page_t* page = message->m_any.page;
279 page_info* info = ddjvu_page_get_user_data(page);
281 printf("the page can/should be REDISPLAYED\n");
289 printf("PROGRESS:\n");
293 printf("simply ERROR!\n message:\t%s\nfunction:\t%s(file %s)\nlineno:\t%d\n",
294 message->m_error.message,
295 message->m_error.function,
296 message->m_error.filename,
297 message->m_error.lineno);
301 printf("INFO: %s!\n", message->m_info.message);
305 printf("unexpected\n");
307 return(message->m_any.tag);
311 #if defined(__cplusplus) || defined(c_plusplus)
319 * DjVu advertised readiness to provide bitmap: So get it!
320 * we use the RGB format!
323 get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, QuantumInfo* quantum_info)
344 rect.w = (unsigned int) w; /* /10 */
345 rect.h = (unsigned int) h; /* /10 */
348 type = ddjvu_page_get_type(lc->page);
350 /* stride of this temporary buffer: */
351 stride = (type == DDJVU_PAGETYPE_BITONAL)?
352 (lc->image->columns + 7)/8:
353 lc->image->columns *3;
355 q = (unsigned char *) AcquireQuantumMemory(lc->image->rows,stride);
358 format = ddjvu_format_create(
359 (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24,
360 /* DDJVU_FORMAT_RGB24
361 * DDJVU_FORMAT_RGBMASK32*/
362 /* DDJVU_FORMAT_RGBMASK32 */
366 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */
370 /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */
374 ddjvu_format_set_row_order(format, 1);
375 ddjvu_format_set_y_direction(format, 1);
377 ret = ddjvu_page_render(page,
378 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
384 ddjvu_format_release(format);
387 if (type == DDJVU_PAGETYPE_BITONAL) {
390 printf("%s: expanding BITONAL page/image\n", __FUNCTION__);
392 register IndexPacket *indexes;
395 for (y=0; y < (ssize_t) image->rows; y++)
397 PixelPacket * o = QueueAuthenticPixels(image,0,y,image->columns,1,&image->exception);
398 if (o == (PixelPacket *) NULL)
400 indexes=GetAuthenticIndexQueue(image);
404 /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/
405 for (x= 0; x < (ssize_t) image->columns; x++)
407 if (bit == 0) byte= (size_t) q[(y * stride) + (x / 8)];
409 indexes[x]=(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01);
415 if (SyncAuthenticPixels(image,&image->exception) == MagickFalse)
422 printf("%s: expanding PHOTO page/image\n", __FUNCTION__);
424 /* now transfer line-wise: */
430 register PixelPacket *r;
433 for (i = 0;i< (ssize_t) lc->image->rows; i++)
436 if (i % 1000 == 0) printf("%d\n",i);
438 r = QueueAuthenticPixels(lc->image,0,i,lc->image->columns,1,&image->exception);
440 ImportQuantumPixels(lc->image,
443 RGBQuantum, /*GrayQuantum*/
444 q+i*stride,&image->exception);
445 SyncAuthenticPixels(lc->image,&image->exception);
448 q=(unsigned char *) RelinquishMagickMemory(q);
452 #if defined(MAGICKCORE_DJVU_DELEGATE)
456 get_page_line(LoadContext *lc, int row, QuantumInfo* quantum_info)
470 ddjvu_rect_t rect, pagerect;
473 rect.w = lc->image->columns; /* /10 */
474 rect.h = 1; /* /10 */
478 pagerect.w = lc->image->columns;
479 pagerect.h = lc->image->rows;
482 format = ddjvu_format_create(
490 ddjvu_format_set_row_order(format, 1);
491 ddjvu_format_set_y_direction(format, 1);
497 q = (unsigned char *) AcquireQuantumMemory(lc->image->columns,stride);
499 ret = ddjvu_page_render(lc->page,
500 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
504 pagerect.w * 3, /* ?? */
507 ImportQuantumPixels(lc->image,
515 ,q,&lc->image->exception);
516 q=(unsigned char *) RelinquishMagickMemory(q);
517 ddjvu_format_release(format);
524 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
528 % R e a d O n e D J V U I m a g e %
532 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % ReadOneDJVUImage() reads a Portable Network Graphics (DJVU) image file
535 % (minus the 8-byte signature) and returns it. It allocates the memory
536 % necessary for the new Image structure and returns a pointer to the new
539 % The format of the ReadOneDJVUImage method is:
541 % Image *ReadOneDJVUImage(MngInfo *mng_info, const ImageInfo *image_info,
542 % ExceptionInfo *exception)
544 % A description of each parameter follows:
546 % o mng_info: Specifies a pointer to a MngInfo structure.
548 % o image_info: the image info.
550 % o exception: return any errors or warnings in this structure.
554 static inline double MagickMax(const double x,const double y)
561 static Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
562 const ImageInfo *image_info,ExceptionInfo *exception)
567 ddjvu_pageinfo_t info;
568 QuantumInfo *quantum_info;
569 ddjvu_message_t *message;
574 /* so, we know that the page is there! Get its dimension, and */
576 /* Read one DJVU image */
579 /* register PixelPacket *q; */
581 logging=LogMagickEvent(CoderEvent,GetMagickModule(), " enter ReadOneDJVUImage()");
584 printf("==== Loading the page %d\n", pagenum);
586 lc->page = ddjvu_page_create_by_pageno(lc->document, pagenum); /* 0? */
588 /* pump data untill the page is ready for rendering. */
591 while ((message = ddjvu_message_peek(lc->context)))
593 tag=process_message(message);
595 ddjvu_message_pop(lc->context);
597 /* fixme: maybe exit? */
598 /* if (lc->error) break; */
600 message = pump_data_until_message(lc,image);
603 tag=process_message(message);
605 ddjvu_message_pop(lc->context);
606 } while ((message = ddjvu_message_peek(lc->context)));
607 } while (!ddjvu_page_decoding_done(lc->page));
609 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
611 image->x_resolution = (float) info.dpi;
612 image->y_resolution =(float) info.dpi;
613 if (image_info->density != (char *) NULL)
622 Set rendering resolution.
624 flags=ParseGeometry(image_info->density,&geometry_info);
625 image->x_resolution=geometry_info.rho;
626 image->y_resolution=geometry_info.sigma;
627 if ((flags & SigmaValue) == 0)
628 image->y_resolution=image->x_resolution;
629 info.width*=image->x_resolution/info.dpi;
630 info.height*=image->y_resolution/info.dpi;
631 info.dpi=(ssize_t) MagickMax(image->x_resolution,image->y_resolution);
633 type = ddjvu_page_get_type(lc->page);
635 /* double -> float! */
636 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
638 /* mmc: set image->depth */
639 /* mmc: This from the type */
641 image->columns=(size_t) info.width;
642 image->rows=(size_t) info.height;
644 /* mmc: bitonal should be palettized, and compressed! */
645 if (type == DDJVU_PAGETYPE_BITONAL){
646 image->colorspace = GRAYColorspace;
647 image->storage_class = PseudoClass;
648 image->depth = 8UL; /* i only support that? */
650 if (AcquireImageColormap(image,image->colors) == MagickFalse)
651 ThrowReaderException(ResourceLimitError,
652 "MemoryAllocationFailed");
654 image->colorspace = RGBColorspace;
655 image->storage_class = DirectClass;
656 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
657 image->depth = 8UL; /* i only support that? */
659 image->matte = MagickTrue;
660 /* is this useful? */
663 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
669 quantum_info=AcquireQuantumInfo(image_info,image);
670 if (quantum_info == (QuantumInfo *) NULL)
671 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
673 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
674 get_page_image(lc, lc->page, 0, 0, info.width, info.height, quantum_info);
677 for (i = 0;i< image->rows; i++)
680 q = QueueAuthenticPixels(image,0,i,image->columns,1);
681 get_page_line(lc, i, quantum_info);
682 SyncAuthenticPixels(image);
685 #endif /* per_line */
689 printf("END: finished filling %.20g x %.20g\n",(double) image->columns,
690 (double) image->rows);
695 quantum_info=DestroyQuantumInfo(quantum_info);
696 /* indexes=GetAuthenticIndexQueue(image); */
697 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
698 /* image->colors = */
700 /* how is the line padding / stride? */
703 ddjvu_page_release(lc->page);
707 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
709 image=DestroyImage(image);
711 /* end of reading one DJVU page/image */
716 if (AcquireImageColormap(image,2) == MagickFalse)
717 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
719 Monochrome colormap. mmc: this the default!
721 image->colormap[0].red=QuantumRange;
722 image->colormap[0].green=QuantumRange;
723 image->colormap[0].blue=QuantumRange;
724 image->colormap[1].red=0;
725 image->colormap[1].green=0;
726 image->colormap[1].blue=0;
729 static void djvu_close_lc(LoadContext* lc)
732 ddjvu_document_release(lc->document);
734 ddjvu_context_release(lc->context);
736 ddjvu_page_release(lc->page);
737 RelinquishMagickMemory(lc);
740 static Image *ReadDJVUImage(const ImageInfo *image_info,
741 ExceptionInfo *exception)
769 assert(image_info != (const ImageInfo *) NULL);
770 assert(image_info->signature == MagickSignature);
773 if (image_info->debug != MagickFalse)
774 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
776 assert(exception != (ExceptionInfo *) NULL);
777 assert(exception->signature == MagickSignature);
780 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
782 image = AcquireImage(image_info); /* mmc: ?? */
785 lc = (LoadContext *) NULL;
786 status = OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
787 if (status == MagickFalse)
788 ThrowReaderException(FileOpenError,"UnableToOpenFile");
790 Verify DJVU signature.
793 count = ReadBlob(image,8,(unsigned char *) magic_number);
795 /* IsDJVU(const unsigned char *magick,const size_t length) */
796 if (memcmp(magic_number,"AT&TFORM",8) != 0)
797 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
802 * Allocate a LoadContext structure.
804 lc = (LoadContext *) AcquireMagickMemory(sizeof(*lc));
806 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
810 * Initialize members of the MngInfo structure.
812 (void) ResetMagickMemory(lc,0,sizeof(LoadContext));
816 lc->context = ddjvu_context_create("ImageMagick djvu loader"); /* g_program_name */
818 ddjvu_cache_set_size(lc->context, 1); /* right? */
820 /* document: here we don't have a filename, but, for the sake of generality, a FILE* ! */
821 url="http://www.imagemagick.org/fake.djvu";
822 lc->document = ddjvu_document_create(lc->context, url, use_cache); /* don't cache */
823 ddjvu_document_set_user_data(lc->document, lc);
826 /* now we wait the message-request for data: */
827 message = ddjvu_message_wait(lc->context);
829 if (message->m_any.tag != DDJVU_NEWSTREAM) {
830 /* fixme: the djvu context, document! */
832 ddjvu_document_release(lc->document);
833 ddjvu_context_release(lc->context);
835 RelinquishMagickMemory(lc);
837 ThrowReaderException(ResourceLimitError,"Djvu initial message: unexpected type");
838 return NULL; /* error! */
841 lc->streamid = message->m_newstream.streamid;
842 ddjvu_message_pop(lc->context);
844 message = pump_data_until_message(lc,image);
845 /* now process the messages: */
849 process_message(message);
850 ddjvu_message_pop(lc->context);
851 } while ((message = ddjvu_message_peek(lc->context)));
853 /* fixme: i hope we have not read any messages pertinent(?) related to the page itself! */
855 while (lc->pages == 0) {
856 message = ddjvu_message_wait(lc->context);
857 process_message(message);
858 ddjvu_message_pop(lc->context);
861 images=NewImageList();
863 if (image_info->number_scenes != 0)
865 for ( ; i < (ssize_t) lc->pages; i++)
867 image=ReadOneDJVUImage(lc,i,image_info,exception);
868 if (image == (Image *) NULL)
871 AppendImageToList(&images,CloneImageList(image,exception));
872 if (image_info->number_scenes != 0)
873 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
877 (void) CloseBlob(images);
878 if (image != (Image *) NULL)
879 image=DestroyImageList(image);
882 if ((image->page.width == 0) && (image->page.height == 0))
884 image->page.width = image->columns+image->page.x;
885 image->page.height = image->rows+image->page.y;
887 if (image->columns == 0 || image->rows == 0)
889 if (logging != MagickFalse)
890 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
891 "exit ReadDJVUImage() with error.");
892 ThrowReaderException(CorruptImageError,"CorruptImage");
895 if (logging != MagickFalse)
896 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadDJVUImage()");
900 return(GetFirstImageInList(images));
905 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
909 % R e g i s t e r D J V U I m a g e %
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915 % RegisterDJVUImage() adds attributes for the DJVU image format to
916 % the list of supported formats. The attributes include the image format
917 % tag, a method to read and/or write the format, whether the format
918 % supports the saving of more than one frame to the same file or blob,
919 % whether the format supports native in-memory I/O, and a brief
920 % description of the format.
922 % The format of the RegisterDJVUImage method is:
924 % size_t RegisterDJVUImage(void)
927 ModuleExport size_t RegisterDJVUImage(void)
930 version[MaxTextExtent];
938 "See http://www.djvuzone.org/ for details about the DJVU format. The\n"
939 "DJVU 1.2 specification is available there and at\n"
940 "ftp://swrinde.nde.swri.edu/pub/djvu/documents/."
944 #if defined(DJVU_LIBDJVU_VER_STRING)
945 (void) ConcatenateMagickString(version,"libdjvu ",MaxTextExtent);
946 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MaxTextExtent);
948 entry=SetMagickInfo("DJVU");
949 #if defined(MAGICKCORE_DJVU_DELEGATE)
950 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
952 entry->raw=MagickTrue;
953 entry->magick=(IsImageFormatHandler *) IsDJVU;
954 entry->adjoin=MagickFalse;
955 entry->thread_support=MagickTrue;
956 entry->description=AcquireString("Déjà vu");
957 entry->module=AcquireString("DJVU");
958 if (*version != '\0')
959 entry->version=AcquireString(version);
960 entry->note=AcquireString(DJVUNote);
961 (void) RegisterMagickInfo(entry);
962 return(MagickImageCoderSignature);
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 % U n r e g i s t e r D J V U I m a g e %
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % UnregisterDJVUImage() removes format registrations made by the
977 % DJVU module from the list of supported formats.
979 % The format of the UnregisterDJVUImage method is:
981 % UnregisterDJVUImage(void)
984 ModuleExport void UnregisterDJVUImage(void)
986 (void) UnregisterMagickInfo("DJVU");