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 "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/blob-private.h"
45 #include "MagickCore/cache.h"
46 #include "MagickCore/colormap.h"
47 #include "MagickCore/constitute.h"
48 #include "MagickCore/exception.h"
49 #include "MagickCore/exception-private.h"
50 #include "MagickCore/list.h"
51 #include "MagickCore/magick.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/monitor.h"
54 #include "MagickCore/monitor-private.h"
55 #include "MagickCore/pixel-accessor.h"
56 #include "MagickCore/quantum-private.h"
57 #include "MagickCore/static.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/module.h"
60 #if defined(MAGICKCORE_DJVU_DELEGATE)
61 #include <libdjvu/ddjvuapi.h>
65 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % IsDJVU() returns MagickTrue if the image format type, identified by the
76 % magick string, is DJVU.
78 % The format of the IsDJVU method is:
80 % MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
82 % A description of each parameter follows:
84 % o magick: compare image format pattern against these bytes.
86 % o length: Specifies the length of the magick string.
89 static MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
93 if (memcmp(magick,"AT&TFORM",8) == 0)
98 #if defined(MAGICKCORE_DJVU_DELEGATE)
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 % R e a d D J V U I m a g e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % ReadDJVUImage() reads DJVU image and returns it. It allocates the memory
111 % necessary for the new Image structure and returns a pointer to the new
112 % image or set of images.
114 % The format of the ReadDJVUImage method is:
116 % Image *ReadDJVUImage(const ImageInfo *image_info,
117 % ExceptionInfo *exception)
119 % A description of each parameter follows:
121 % o image_info: the image info.
123 % o exception: return any errors or warnings in this structure.
127 #if defined(__cplusplus) || defined(c_plusplus)
131 typedef struct _LoadContext
136 ddjvu_context_t* context;
137 ddjvu_document_t *document;
144 #define BLOCKSIZE 65536
147 pump_data(Image *image, LoadContext* lc)
149 int blocksize = BLOCKSIZE;
150 char data[BLOCKSIZE];
153 /* i might check for a condition! */
154 while ((size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
155 ddjvu_stream_write(lc->document, lc->streamid, data, size);
158 ddjvu_stream_write(lc->document, lc->streamid, data, size);
159 ddjvu_stream_close(lc->document, lc->streamid, 0);
163 /* returns NULL only after all is delivered! */
164 static ddjvu_message_t*
165 pump_data_until_message(LoadContext *lc,Image *image) /* ddjvu_context_t *context, type ddjvu_document_type_t */
167 size_t blocksize = BLOCKSIZE;
168 unsigned char data[BLOCKSIZE];
170 ddjvu_message_t *message;
172 /* i might check for a condition! */
173 while (!(message = ddjvu_message_peek(lc->context))
174 && (size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
175 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
180 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
181 ddjvu_stream_close(lc->document, lc->streamid, 0);
188 message_tag_name(ddjvu_message_tag_t tag)
190 static char* names[] =
203 if (tag <= DDJVU_PROGRESS)
212 /* write out nice info on the message,
213 * and store in *user* data the info on progress.
216 process_message(ddjvu_message_t *message)
220 ddjvu_context_t* context= message->m_any.context;
226 printf("*** %s: %s.\n",__FUNCTION__, message_tag_name(message->m_any.tag));
230 switch (message->m_any.tag){
233 ddjvu_document_t* document= message->m_any.document;
234 /* ddjvu_document_decoding_status is set by libdjvu! */
235 /* we have some info on the document */
236 LoadContext *lc = (LoadContext *) ddjvu_document_get_user_data(document);
237 lc->pages = ddjvu_document_get_pagenum(document);
239 printf("the doc has %d pages\n", ddjvu_document_get_pagenum(document));
245 printf("the name of the chunk is: %s\n", message->m_chunk.chunkid);
254 ddjvu_page_t* page = message->m_any.page;
255 page_info* info = ddjvu_page_get_user_data(page);
257 printf("page decoding status: %d %s%s%s\n",
258 ddjvu_page_decoding_status(page),
259 status_color, status_name(ddjvu_page_decoding_status(page)), color_reset);
261 printf("the page LAYOUT changed: width x height: %d x %d @ %d dpi. Version %d, type %d\n",
262 // printf("page info:\n width x height: %d x %d @ %d dpi, version %d, type %d\n",
263 ddjvu_page_get_width(page),
264 ddjvu_page_get_height(page),
265 ddjvu_page_get_resolution(page),
266 ddjvu_page_get_version(page),
267 /* DDJVU_PAGETYPE_BITONAL */
268 ddjvu_page_get_type(page));
275 case DDJVU_REDISPLAY:
279 ddjvu_page_t* page = message->m_any.page;
280 page_info* info = ddjvu_page_get_user_data(page);
282 printf("the page can/should be REDISPLAYED\n");
290 printf("PROGRESS:\n");
294 printf("simply ERROR!\n message:\t%s\nfunction:\t%s(file %s)\nlineno:\t%d\n",
295 message->m_error.message,
296 message->m_error.function,
297 message->m_error.filename,
298 message->m_error.lineno);
302 printf("INFO: %s!\n", message->m_info.message);
306 printf("unexpected\n");
308 return(message->m_any.tag);
312 #if defined(__cplusplus) || defined(c_plusplus)
320 * DjVu advertised readiness to provide bitmap: So get it!
321 * we use the RGB format!
324 get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, const ImageInfo *image_info, ExceptionInfo *exception ) {
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 (image->columns + 7)/8 : image->columns *3;
354 q = (unsigned char *) AcquireQuantumMemory(image->rows,stride);
355 if (q == (unsigned char *) NULL)
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 */
385 ddjvu_format_release(format);
388 if (type == DDJVU_PAGETYPE_BITONAL) {
391 printf("%s: expanding BITONAL page/image\n", __FUNCTION__);
395 for (y=0; y < (ssize_t) image->rows; y++)
397 Quantum * o = QueueAuthenticPixels(image,0,y,image->columns,1,exception);
398 if (o == (Quantum *) NULL)
403 /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/
404 for (x= 0; x < (ssize_t) image->columns; x++)
406 if (bit == 0) byte= (size_t) q[(y * stride) + (x / 8)];
408 SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x00 : 0x01),o);
413 o+=GetPixelChannels(image);
415 if (SyncAuthenticPixels(image,exception) == MagickFalse)
419 SyncImage(image,exception);
422 printf("%s: expanding PHOTO page/image\n", __FUNCTION__);
424 /* now transfer line-wise: */
434 for (i = 0;i< (ssize_t) image->rows; i++)
437 if (i % 1000 == 0) printf("%d\n",i);
439 r = QueueAuthenticPixels(image,0,i,image->columns,1,exception);
440 if (r == (Quantum *) NULL)
442 for (x=0; x < (ssize_t) image->columns; x++)
444 SetPixelRed(image,ScaleCharToQuantum(*s++),r);
445 SetPixelGreen(image,ScaleCharToQuantum(*s++),r);
446 SetPixelBlue(image,ScaleCharToQuantum(*s++),r);
447 r+=GetPixelChannels(image);
450 SyncAuthenticPixels(image,exception);
453 q=(unsigned char *) RelinquishMagickMemory(q);
457 #if defined(MAGICKCORE_DJVU_DELEGATE)
461 get_page_line(LoadContext *lc, int row, QuantumInfo* quantum_info)
475 ddjvu_rect_t rect, pagerect;
478 rect.w = lc->image->columns; /* /10 */
479 rect.h = 1; /* /10 */
483 pagerect.w = lc->image->columns;
484 pagerect.h = lc->image->rows;
487 format = ddjvu_format_create(
495 ddjvu_format_set_row_order(format, 1);
496 ddjvu_format_set_y_direction(format, 1);
502 q = (unsigned char *) AcquireQuantumMemory(lc->image->columns,stride);
504 ret = ddjvu_page_render(lc->page,
505 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
509 pagerect.w * 3, /* ?? */
512 ImportQuantumPixels(lc->image,
520 ,q,&lc->image->exception);
521 q=(unsigned char *) RelinquishMagickMemory(q);
522 ddjvu_format_release(format);
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 % R e a d O n e D J V U I m a g e %
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % ReadOneDJVUImage() reads a Portable Network Graphics (DJVU) image file
540 % (minus the 8-byte signature) and returns it. It allocates the memory
541 % necessary for the new Image structure and returns a pointer to the new
544 % The format of the ReadOneDJVUImage method is:
546 % Image *ReadOneDJVUImage(MngInfo *mng_info, const ImageInfo *image_info,
547 % ExceptionInfo *exception)
549 % A description of each parameter follows:
551 % o mng_info: Specifies a pointer to a MngInfo structure.
553 % o image_info: the image info.
555 % o exception: return any errors or warnings in this structure.
559 static inline double MagickMax(const double x,const double y)
566 static Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
567 const ImageInfo *image_info,ExceptionInfo *exception)
572 ddjvu_pageinfo_t info;
573 ddjvu_message_t *message;
578 /* so, we know that the page is there! Get its dimension, and */
580 /* Read one DJVU image */
583 /* register Quantum *q; */
585 logging=LogMagickEvent(CoderEvent,GetMagickModule(), " enter ReadOneDJVUImage()");
589 printf("==== Loading the page %d\n", pagenum);
591 lc->page = ddjvu_page_create_by_pageno(lc->document, pagenum); /* 0? */
593 /* pump data untill the page is ready for rendering. */
596 while ((message = ddjvu_message_peek(lc->context)))
598 tag=process_message(message);
600 ddjvu_message_pop(lc->context);
602 /* fixme: maybe exit? */
603 /* if (lc->error) break; */
605 message = pump_data_until_message(lc,image);
608 tag=process_message(message);
610 ddjvu_message_pop(lc->context);
611 } while ((message = ddjvu_message_peek(lc->context)));
612 } while (!ddjvu_page_decoding_done(lc->page));
614 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
616 image->resolution.x = (float) info.dpi;
617 image->resolution.y =(float) info.dpi;
618 if (image_info->density != (char *) NULL)
627 Set rendering resolution.
629 flags=ParseGeometry(image_info->density,&geometry_info);
630 image->resolution.x=geometry_info.rho;
631 image->resolution.y=geometry_info.sigma;
632 if ((flags & SigmaValue) == 0)
633 image->resolution.y=image->resolution.x;
634 info.width*=image->resolution.x/info.dpi;
635 info.height*=image->resolution.y/info.dpi;
636 info.dpi=(ssize_t) MagickMax(image->resolution.x,image->resolution.y);
638 type = ddjvu_page_get_type(lc->page);
640 /* double -> float! */
641 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
643 /* mmc: set image->depth */
644 /* mmc: This from the type */
646 image->columns=(size_t) info.width;
647 image->rows=(size_t) info.height;
649 /* mmc: bitonal should be palettized, and compressed! */
650 if (type == DDJVU_PAGETYPE_BITONAL){
651 image->colorspace = GRAYColorspace;
652 image->storage_class = PseudoClass;
653 image->depth = 8UL; /* i only support that? */
655 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
656 ThrowReaderException(ResourceLimitError,
657 "MemoryAllocationFailed");
659 image->colorspace = RGBColorspace;
660 image->storage_class = DirectClass;
661 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
662 image->depth = 8UL; /* i only support that? */
664 image->matte = MagickTrue;
665 /* is this useful? */
668 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
675 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
676 get_page_image(lc, lc->page, 0, 0, info.width, info.height, image_info,
680 for (i = 0;i< image->rows; i++)
683 q = QueueAuthenticPixels(image,0,i,image->columns,1);
684 get_page_line(lc, i, quantum_info);
685 SyncAuthenticPixels(image);
688 #endif /* per_line */
692 printf("END: finished filling %.20g x %.20g\n",(double) image->columns,
693 (double) image->rows);
697 SyncImage(image,exception);
698 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
699 /* image->colors = */
701 /* how is the line padding / stride? */
704 ddjvu_page_release(lc->page);
708 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
710 image=DestroyImage(image);
712 /* end of reading one DJVU page/image */
717 if (AcquireImageColormap(image,2,exception) == MagickFalse)
718 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
720 Monochrome colormap. mmc: this the default!
722 image->colormap[0].red=QuantumRange;
723 image->colormap[0].green=QuantumRange;
724 image->colormap[0].blue=QuantumRange;
725 image->colormap[1].red=0;
726 image->colormap[1].green=0;
727 image->colormap[1].blue=0;
730 static void djvu_close_lc(LoadContext* lc)
733 ddjvu_document_release(lc->document);
735 ddjvu_context_release(lc->context);
737 ddjvu_page_release(lc->page);
738 RelinquishMagickMemory(lc);
741 static Image *ReadDJVUImage(const ImageInfo *image_info,
742 ExceptionInfo *exception)
770 assert(image_info != (const ImageInfo *) NULL);
771 assert(image_info->signature == MagickSignature);
774 if (image_info->debug != MagickFalse)
775 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
777 assert(exception != (ExceptionInfo *) NULL);
778 assert(exception->signature == MagickSignature);
781 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
784 image = AcquireImage(image_info,exception); /* mmc: ?? */
787 lc = (LoadContext *) NULL;
788 status = OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
789 if (status == MagickFalse)
790 ThrowReaderException(FileOpenError,"UnableToOpenFile");
792 Verify DJVU signature.
795 count = ReadBlob(image,8,(unsigned char *) magic_number);
797 /* IsDJVU(const unsigned char *magick,const size_t length) */
798 if (memcmp(magic_number,"AT&TFORM",8) != 0)
799 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
804 * Allocate a LoadContext structure.
806 lc = (LoadContext *) AcquireMagickMemory(sizeof(*lc));
808 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
812 * Initialize members of the MngInfo structure.
814 (void) ResetMagickMemory(lc,0,sizeof(LoadContext));
818 lc->context = ddjvu_context_create("ImageMagick djvu loader"); /* g_program_name */
820 ddjvu_cache_set_size(lc->context, 1); /* right? */
822 /* document: here we don't have a filename, but, for the sake of generality, a FILE* ! */
823 url="http://www.imagemagick.org/fake.djvu";
824 lc->document = ddjvu_document_create(lc->context, url, use_cache); /* don't cache */
825 ddjvu_document_set_user_data(lc->document, lc);
828 /* now we wait the message-request for data: */
829 message = ddjvu_message_wait(lc->context);
831 if (message->m_any.tag != DDJVU_NEWSTREAM) {
832 /* fixme: the djvu context, document! */
834 ddjvu_document_release(lc->document);
835 ddjvu_context_release(lc->context);
837 RelinquishMagickMemory(lc);
839 ThrowReaderException(ResourceLimitError,"Djvu initial message: unexpected type");
840 return NULL; /* error! */
843 lc->streamid = message->m_newstream.streamid;
844 ddjvu_message_pop(lc->context);
846 message = pump_data_until_message(lc,image);
847 /* now process the messages: */
851 process_message(message);
852 ddjvu_message_pop(lc->context);
853 } while ((message = ddjvu_message_peek(lc->context)));
855 /* fixme: i hope we have not read any messages pertinent(?) related to the page itself! */
857 while (lc->pages == 0) {
858 message = ddjvu_message_wait(lc->context);
859 process_message(message);
860 ddjvu_message_pop(lc->context);
863 images=NewImageList();
865 if (image_info->number_scenes != 0)
867 for ( ; i < (ssize_t) lc->pages; i++)
869 image=ReadOneDJVUImage(lc,i,image_info,exception);
870 if (image == (Image *) NULL)
873 AppendImageToList(&images,CloneImageList(image,exception));
874 if (image_info->number_scenes != 0)
875 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
879 (void) CloseBlob(images);
880 if (image != (Image *) NULL)
881 image=DestroyImageList(image);
884 if ((image->page.width == 0) && (image->page.height == 0))
886 image->page.width = image->columns+image->page.x;
887 image->page.height = image->rows+image->page.y;
889 if (image->columns == 0 || image->rows == 0)
891 if (logging != MagickFalse)
892 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
893 "exit ReadDJVUImage() with error.");
894 ThrowReaderException(CorruptImageError,"CorruptImage");
897 if (logging != MagickFalse)
898 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadDJVUImage()");
902 return(GetFirstImageInList(images));
907 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
911 % R e g i s t e r D J V U I m a g e %
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 % RegisterDJVUImage() adds attributes for the DJVU image format to
918 % the list of supported formats. The attributes include the image format
919 % tag, a method to read and/or write the format, whether the format
920 % supports the saving of more than one frame to the same file or blob,
921 % whether the format supports native in-memory I/O, and a brief
922 % description of the format.
924 % The format of the RegisterDJVUImage method is:
926 % size_t RegisterDJVUImage(void)
929 ModuleExport size_t RegisterDJVUImage(void)
932 version[MaxTextExtent];
940 "See http://www.djvuzone.org/ for details about the DJVU format. The\n"
941 "DJVU 1.2 specification is available there and at\n"
942 "ftp://swrinde.nde.swri.edu/pub/djvu/documents/."
946 #if defined(DJVU_LIBDJVU_VER_STRING)
947 (void) ConcatenateMagickString(version,"libdjvu ",MaxTextExtent);
948 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MaxTextExtent);
950 entry=SetMagickInfo("DJVU");
951 #if defined(MAGICKCORE_DJVU_DELEGATE)
952 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
954 entry->raw=MagickTrue;
955 entry->magick=(IsImageFormatHandler *) IsDJVU;
956 entry->adjoin=MagickFalse;
957 entry->thread_support=MagickTrue;
958 entry->description=AcquireString("Déjà vu");
959 entry->module=AcquireString("DJVU");
960 if (*version != '\0')
961 entry->version=AcquireString(version);
962 entry->note=AcquireString(DJVUNote);
963 (void) RegisterMagickInfo(entry);
964 return(MagickImageCoderSignature);
968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972 % U n r e g i s t e r D J V U I m a g e %
976 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 % UnregisterDJVUImage() removes format registrations made by the
979 % DJVU module from the list of supported formats.
981 % The format of the UnregisterDJVUImage method is:
983 % UnregisterDJVUImage(void)
986 ModuleExport void UnregisterDJVUImage(void)
988 (void) UnregisterMagickInfo("DJVU");