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, const ImageInfo *image_info ) {
343 rect.w = (unsigned int) w; /* /10 */
344 rect.h = (unsigned int) h; /* /10 */
347 type = ddjvu_page_get_type(lc->page);
349 /* stride of this temporary buffer: */
350 stride = (type == DDJVU_PAGETYPE_BITONAL)?
351 (image->columns + 7)/8 : image->columns *3;
353 q = (unsigned char *) AcquireQuantumMemory(image->rows,stride);
354 if (q == (unsigned char *) NULL)
357 format = ddjvu_format_create(
358 (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24,
359 /* DDJVU_FORMAT_RGB24
360 * DDJVU_FORMAT_RGBMASK32*/
361 /* DDJVU_FORMAT_RGBMASK32 */
365 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */
369 /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */
373 ddjvu_format_set_row_order(format, 1);
374 ddjvu_format_set_y_direction(format, 1);
376 ret = ddjvu_page_render(page,
377 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 if (indexes != (IndexPacket *) NULL)
410 SetIndexPixelComponent(indexes+x,(IndexPacket) (((byte & 0x01) != 0) ? 0x00 : 0x01));
416 if (SyncAuthenticPixels(image,&image->exception) == MagickFalse)
423 printf("%s: expanding PHOTO page/image\n", __FUNCTION__);
425 /* now transfer line-wise: */
431 register PixelPacket *r;
435 for (i = 0;i< (ssize_t) image->rows; i++)
438 if (i % 1000 == 0) printf("%d\n",i);
440 r = QueueAuthenticPixels(image,0,i,image->columns,1,&image->exception);
441 if (r == (PixelPacket *) NULL)
443 for (x=0; x < (ssize_t) image->columns; x++)
445 SetRedPixelComponent(r,ScaleCharToQuantum(*s++));
446 SetGreenPixelComponent(r,ScaleCharToQuantum(*s++));
447 SetBluePixelComponent(r,ScaleCharToQuantum(*s++));
451 SyncAuthenticPixels(image,&image->exception);
454 q=(unsigned char *) RelinquishMagickMemory(q);
458 #if defined(MAGICKCORE_DJVU_DELEGATE)
462 get_page_line(LoadContext *lc, int row, QuantumInfo* quantum_info)
476 ddjvu_rect_t rect, pagerect;
479 rect.w = lc->image->columns; /* /10 */
480 rect.h = 1; /* /10 */
484 pagerect.w = lc->image->columns;
485 pagerect.h = lc->image->rows;
488 format = ddjvu_format_create(
496 ddjvu_format_set_row_order(format, 1);
497 ddjvu_format_set_y_direction(format, 1);
503 q = (unsigned char *) AcquireQuantumMemory(lc->image->columns,stride);
505 ret = ddjvu_page_render(lc->page,
506 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
510 pagerect.w * 3, /* ?? */
513 ImportQuantumPixels(lc->image,
521 ,q,&lc->image->exception);
522 q=(unsigned char *) RelinquishMagickMemory(q);
523 ddjvu_format_release(format);
530 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
534 % R e a d O n e D J V U I m a g e %
538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540 % ReadOneDJVUImage() reads a Portable Network Graphics (DJVU) image file
541 % (minus the 8-byte signature) and returns it. It allocates the memory
542 % necessary for the new Image structure and returns a pointer to the new
545 % The format of the ReadOneDJVUImage method is:
547 % Image *ReadOneDJVUImage(MngInfo *mng_info, const ImageInfo *image_info,
548 % ExceptionInfo *exception)
550 % A description of each parameter follows:
552 % o mng_info: Specifies a pointer to a MngInfo structure.
554 % o image_info: the image info.
556 % o exception: return any errors or warnings in this structure.
560 static inline double MagickMax(const double x,const double y)
567 static Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
568 const ImageInfo *image_info,ExceptionInfo *exception)
573 ddjvu_pageinfo_t info;
574 ddjvu_message_t *message;
579 /* so, we know that the page is there! Get its dimension, and */
581 /* Read one DJVU image */
584 /* register PixelPacket *q; */
586 logging=LogMagickEvent(CoderEvent,GetMagickModule(), " enter ReadOneDJVUImage()");
590 printf("==== Loading the page %d\n", pagenum);
592 lc->page = ddjvu_page_create_by_pageno(lc->document, pagenum); /* 0? */
594 /* pump data untill the page is ready for rendering. */
597 while ((message = ddjvu_message_peek(lc->context)))
599 tag=process_message(message);
601 ddjvu_message_pop(lc->context);
603 /* fixme: maybe exit? */
604 /* if (lc->error) break; */
606 message = pump_data_until_message(lc,image);
609 tag=process_message(message);
611 ddjvu_message_pop(lc->context);
612 } while ((message = ddjvu_message_peek(lc->context)));
613 } while (!ddjvu_page_decoding_done(lc->page));
615 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
617 image->x_resolution = (float) info.dpi;
618 image->y_resolution =(float) info.dpi;
619 if (image_info->density != (char *) NULL)
628 Set rendering resolution.
630 flags=ParseGeometry(image_info->density,&geometry_info);
631 image->x_resolution=geometry_info.rho;
632 image->y_resolution=geometry_info.sigma;
633 if ((flags & SigmaValue) == 0)
634 image->y_resolution=image->x_resolution;
635 info.width*=image->x_resolution/info.dpi;
636 info.height*=image->y_resolution/info.dpi;
637 info.dpi=(ssize_t) MagickMax(image->x_resolution,image->y_resolution);
639 type = ddjvu_page_get_type(lc->page);
641 /* double -> float! */
642 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
644 /* mmc: set image->depth */
645 /* mmc: This from the type */
647 image->columns=(size_t) info.width;
648 image->rows=(size_t) info.height;
650 /* mmc: bitonal should be palettized, and compressed! */
651 if (type == DDJVU_PAGETYPE_BITONAL){
652 image->colorspace = GRAYColorspace;
653 image->storage_class = PseudoClass;
654 image->depth = 8UL; /* i only support that? */
656 if (AcquireImageColormap(image,image->colors) == MagickFalse)
657 ThrowReaderException(ResourceLimitError,
658 "MemoryAllocationFailed");
660 image->colorspace = RGBColorspace;
661 image->storage_class = DirectClass;
662 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
663 image->depth = 8UL; /* i only support that? */
665 image->matte = MagickTrue;
666 /* is this useful? */
669 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
676 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
677 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);
698 /* indexes=GetAuthenticIndexQueue(image); */
699 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
700 /* image->colors = */
702 /* how is the line padding / stride? */
705 ddjvu_page_release(lc->page);
709 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
711 image=DestroyImage(image);
713 /* end of reading one DJVU page/image */
718 if (AcquireImageColormap(image,2) == MagickFalse)
719 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
721 Monochrome colormap. mmc: this the default!
723 image->colormap[0].red=QuantumRange;
724 image->colormap[0].green=QuantumRange;
725 image->colormap[0].blue=QuantumRange;
726 image->colormap[1].red=0;
727 image->colormap[1].green=0;
728 image->colormap[1].blue=0;
731 static void djvu_close_lc(LoadContext* lc)
734 ddjvu_document_release(lc->document);
736 ddjvu_context_release(lc->context);
738 ddjvu_page_release(lc->page);
739 RelinquishMagickMemory(lc);
742 static Image *ReadDJVUImage(const ImageInfo *image_info,
743 ExceptionInfo *exception)
771 assert(image_info != (const ImageInfo *) NULL);
772 assert(image_info->signature == MagickSignature);
775 if (image_info->debug != MagickFalse)
776 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
778 assert(exception != (ExceptionInfo *) NULL);
779 assert(exception->signature == MagickSignature);
782 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
785 image = AcquireImage(image_info); /* mmc: ?? */
788 lc = (LoadContext *) NULL;
789 status = OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
790 if (status == MagickFalse)
791 ThrowReaderException(FileOpenError,"UnableToOpenFile");
793 Verify DJVU signature.
796 count = ReadBlob(image,8,(unsigned char *) magic_number);
798 /* IsDJVU(const unsigned char *magick,const size_t length) */
799 if (memcmp(magic_number,"AT&TFORM",8) != 0)
800 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
805 * Allocate a LoadContext structure.
807 lc = (LoadContext *) AcquireMagickMemory(sizeof(*lc));
809 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
813 * Initialize members of the MngInfo structure.
815 (void) ResetMagickMemory(lc,0,sizeof(LoadContext));
819 lc->context = ddjvu_context_create("ImageMagick djvu loader"); /* g_program_name */
821 ddjvu_cache_set_size(lc->context, 1); /* right? */
823 /* document: here we don't have a filename, but, for the sake of generality, a FILE* ! */
824 url="http://www.imagemagick.org/fake.djvu";
825 lc->document = ddjvu_document_create(lc->context, url, use_cache); /* don't cache */
826 ddjvu_document_set_user_data(lc->document, lc);
829 /* now we wait the message-request for data: */
830 message = ddjvu_message_wait(lc->context);
832 if (message->m_any.tag != DDJVU_NEWSTREAM) {
833 /* fixme: the djvu context, document! */
835 ddjvu_document_release(lc->document);
836 ddjvu_context_release(lc->context);
838 RelinquishMagickMemory(lc);
840 ThrowReaderException(ResourceLimitError,"Djvu initial message: unexpected type");
841 return NULL; /* error! */
844 lc->streamid = message->m_newstream.streamid;
845 ddjvu_message_pop(lc->context);
847 message = pump_data_until_message(lc,image);
848 /* now process the messages: */
852 process_message(message);
853 ddjvu_message_pop(lc->context);
854 } while ((message = ddjvu_message_peek(lc->context)));
856 /* fixme: i hope we have not read any messages pertinent(?) related to the page itself! */
858 while (lc->pages == 0) {
859 message = ddjvu_message_wait(lc->context);
860 process_message(message);
861 ddjvu_message_pop(lc->context);
864 images=NewImageList();
866 if (image_info->number_scenes != 0)
868 for ( ; i < (ssize_t) lc->pages; i++)
870 image=ReadOneDJVUImage(lc,i,image_info,exception);
871 if (image == (Image *) NULL)
874 AppendImageToList(&images,CloneImageList(image,exception));
875 if (image_info->number_scenes != 0)
876 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
880 (void) CloseBlob(images);
881 if (image != (Image *) NULL)
882 image=DestroyImageList(image);
885 if ((image->page.width == 0) && (image->page.height == 0))
887 image->page.width = image->columns+image->page.x;
888 image->page.height = image->rows+image->page.y;
890 if (image->columns == 0 || image->rows == 0)
892 if (logging != MagickFalse)
893 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
894 "exit ReadDJVUImage() with error.");
895 ThrowReaderException(CorruptImageError,"CorruptImage");
898 if (logging != MagickFalse)
899 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadDJVUImage()");
903 return(GetFirstImageInList(images));
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 % R e g i s t e r D J V U I m a g e %
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % RegisterDJVUImage() adds attributes for the DJVU image format to
919 % the list of supported formats. The attributes include the image format
920 % tag, a method to read and/or write the format, whether the format
921 % supports the saving of more than one frame to the same file or blob,
922 % whether the format supports native in-memory I/O, and a brief
923 % description of the format.
925 % The format of the RegisterDJVUImage method is:
927 % size_t RegisterDJVUImage(void)
930 ModuleExport size_t RegisterDJVUImage(void)
933 version[MaxTextExtent];
941 "See http://www.djvuzone.org/ for details about the DJVU format. The\n"
942 "DJVU 1.2 specification is available there and at\n"
943 "ftp://swrinde.nde.swri.edu/pub/djvu/documents/."
947 #if defined(DJVU_LIBDJVU_VER_STRING)
948 (void) ConcatenateMagickString(version,"libdjvu ",MaxTextExtent);
949 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MaxTextExtent);
951 entry=SetMagickInfo("DJVU");
952 #if defined(MAGICKCORE_DJVU_DELEGATE)
953 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
955 entry->raw=MagickTrue;
956 entry->magick=(IsImageFormatHandler *) IsDJVU;
957 entry->adjoin=MagickFalse;
958 entry->thread_support=MagickTrue;
959 entry->description=AcquireString("Déjà vu");
960 entry->module=AcquireString("DJVU");
961 if (*version != '\0')
962 entry->version=AcquireString(version);
963 entry->note=AcquireString(DJVUNote);
964 (void) RegisterMagickInfo(entry);
965 return(MagickImageCoderSignature);
969 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
973 % U n r e g i s t e r D J V U I m a g e %
977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 % UnregisterDJVUImage() removes format registrations made by the
980 % DJVU module from the list of supported formats.
982 % The format of the UnregisterDJVUImage method is:
984 % UnregisterDJVUImage(void)
987 ModuleExport void UnregisterDJVUImage(void)
989 (void) UnregisterMagickInfo("DJVU");