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 */
385 ddjvu_format_release(format);
388 if (type == DDJVU_PAGETYPE_BITONAL) {
391 printf("%s: expanding BITONAL page/image\n", __FUNCTION__);
393 register IndexPacket *indexes;
396 for (y=0; y < (ssize_t) image->rows; y++)
398 PixelPacket * o = QueueAuthenticPixels(image,0,y,image->columns,1,&image->exception);
399 if (o == (PixelPacket *) NULL)
401 indexes=GetAuthenticIndexQueue(image);
405 /* fixme: the non-aligned, last =<7 bits ! that's ok!!!*/
406 for (x= 0; x < (ssize_t) image->columns; x++)
408 if (bit == 0) byte= (size_t) q[(y * stride) + (x / 8)];
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;
434 for (i = 0;i< (ssize_t) lc->image->rows; i++)
437 if (i % 1000 == 0) printf("%d\n",i);
439 r = QueueAuthenticPixels(lc->image,0,i,lc->image->columns,1,&image->exception);
440 if (r == (PixelPacket *) NULL)
443 ImportQuantumPixels(lc->image,
446 RGBQuantum, /*GrayQuantum*/
447 q+i*stride,&image->exception);
448 SyncAuthenticPixels(lc->image,&image->exception);
451 q=(unsigned char *) RelinquishMagickMemory(q);
455 #if defined(MAGICKCORE_DJVU_DELEGATE)
459 get_page_line(LoadContext *lc, int row, QuantumInfo* quantum_info)
473 ddjvu_rect_t rect, pagerect;
476 rect.w = lc->image->columns; /* /10 */
477 rect.h = 1; /* /10 */
481 pagerect.w = lc->image->columns;
482 pagerect.h = lc->image->rows;
485 format = ddjvu_format_create(
493 ddjvu_format_set_row_order(format, 1);
494 ddjvu_format_set_y_direction(format, 1);
500 q = (unsigned char *) AcquireQuantumMemory(lc->image->columns,stride);
502 ret = ddjvu_page_render(lc->page,
503 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
507 pagerect.w * 3, /* ?? */
510 ImportQuantumPixels(lc->image,
518 ,q,&lc->image->exception);
519 q=(unsigned char *) RelinquishMagickMemory(q);
520 ddjvu_format_release(format);
527 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
531 % R e a d O n e D J V U I m a g e %
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 % ReadOneDJVUImage() reads a Portable Network Graphics (DJVU) image file
538 % (minus the 8-byte signature) and returns it. It allocates the memory
539 % necessary for the new Image structure and returns a pointer to the new
542 % The format of the ReadOneDJVUImage method is:
544 % Image *ReadOneDJVUImage(MngInfo *mng_info, const ImageInfo *image_info,
545 % ExceptionInfo *exception)
547 % A description of each parameter follows:
549 % o mng_info: Specifies a pointer to a MngInfo structure.
551 % o image_info: the image info.
553 % o exception: return any errors or warnings in this structure.
557 static inline double MagickMax(const double x,const double y)
564 static Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
565 const ImageInfo *image_info,ExceptionInfo *exception)
570 ddjvu_pageinfo_t info;
571 QuantumInfo *quantum_info;
572 ddjvu_message_t *message;
577 /* so, we know that the page is there! Get its dimension, and */
579 /* Read one DJVU image */
582 /* register PixelPacket *q; */
584 logging=LogMagickEvent(CoderEvent,GetMagickModule(), " enter ReadOneDJVUImage()");
588 printf("==== Loading the page %d\n", pagenum);
590 lc->page = ddjvu_page_create_by_pageno(lc->document, pagenum); /* 0? */
592 /* pump data untill the page is ready for rendering. */
595 while ((message = ddjvu_message_peek(lc->context)))
597 tag=process_message(message);
599 ddjvu_message_pop(lc->context);
601 /* fixme: maybe exit? */
602 /* if (lc->error) break; */
604 message = pump_data_until_message(lc,image);
607 tag=process_message(message);
609 ddjvu_message_pop(lc->context);
610 } while ((message = ddjvu_message_peek(lc->context)));
611 } while (!ddjvu_page_decoding_done(lc->page));
613 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
615 image->x_resolution = (float) info.dpi;
616 image->y_resolution =(float) info.dpi;
617 if (image_info->density != (char *) NULL)
626 Set rendering resolution.
628 flags=ParseGeometry(image_info->density,&geometry_info);
629 image->x_resolution=geometry_info.rho;
630 image->y_resolution=geometry_info.sigma;
631 if ((flags & SigmaValue) == 0)
632 image->y_resolution=image->x_resolution;
633 info.width*=image->x_resolution/info.dpi;
634 info.height*=image->y_resolution/info.dpi;
635 info.dpi=(ssize_t) MagickMax(image->x_resolution,image->y_resolution);
637 type = ddjvu_page_get_type(lc->page);
639 /* double -> float! */
640 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
642 /* mmc: set image->depth */
643 /* mmc: This from the type */
645 image->columns=(size_t) info.width;
646 image->rows=(size_t) info.height;
648 /* mmc: bitonal should be palettized, and compressed! */
649 if (type == DDJVU_PAGETYPE_BITONAL){
650 image->colorspace = GRAYColorspace;
651 image->storage_class = PseudoClass;
652 image->depth = 8UL; /* i only support that? */
654 if (AcquireImageColormap(image,image->colors) == MagickFalse)
655 ThrowReaderException(ResourceLimitError,
656 "MemoryAllocationFailed");
658 image->colorspace = RGBColorspace;
659 image->storage_class = DirectClass;
660 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
661 image->depth = 8UL; /* i only support that? */
663 image->matte = MagickTrue;
664 /* is this useful? */
667 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
673 quantum_info=AcquireQuantumInfo(image_info,image);
674 if (quantum_info == (QuantumInfo *) NULL)
675 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
677 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
678 get_page_image(lc, lc->page, 0, 0, info.width, info.height, quantum_info);
681 for (i = 0;i< image->rows; i++)
684 q = QueueAuthenticPixels(image,0,i,image->columns,1);
685 get_page_line(lc, i, quantum_info);
686 SyncAuthenticPixels(image);
689 #endif /* per_line */
693 printf("END: finished filling %.20g x %.20g\n",(double) image->columns,
694 (double) image->rows);
699 quantum_info=DestroyQuantumInfo(quantum_info);
700 /* indexes=GetAuthenticIndexQueue(image); */
701 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
702 /* image->colors = */
704 /* how is the line padding / stride? */
707 ddjvu_page_release(lc->page);
711 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
713 image=DestroyImage(image);
715 /* end of reading one DJVU page/image */
720 if (AcquireImageColormap(image,2) == MagickFalse)
721 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
723 Monochrome colormap. mmc: this the default!
725 image->colormap[0].red=QuantumRange;
726 image->colormap[0].green=QuantumRange;
727 image->colormap[0].blue=QuantumRange;
728 image->colormap[1].red=0;
729 image->colormap[1].green=0;
730 image->colormap[1].blue=0;
733 static void djvu_close_lc(LoadContext* lc)
736 ddjvu_document_release(lc->document);
738 ddjvu_context_release(lc->context);
740 ddjvu_page_release(lc->page);
741 RelinquishMagickMemory(lc);
744 static Image *ReadDJVUImage(const ImageInfo *image_info,
745 ExceptionInfo *exception)
773 assert(image_info != (const ImageInfo *) NULL);
774 assert(image_info->signature == MagickSignature);
777 if (image_info->debug != MagickFalse)
778 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
780 assert(exception != (ExceptionInfo *) NULL);
781 assert(exception->signature == MagickSignature);
784 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
787 image = AcquireImage(image_info); /* mmc: ?? */
790 lc = (LoadContext *) NULL;
791 status = OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
792 if (status == MagickFalse)
793 ThrowReaderException(FileOpenError,"UnableToOpenFile");
795 Verify DJVU signature.
798 count = ReadBlob(image,8,(unsigned char *) magic_number);
800 /* IsDJVU(const unsigned char *magick,const size_t length) */
801 if (memcmp(magic_number,"AT&TFORM",8) != 0)
802 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
807 * Allocate a LoadContext structure.
809 lc = (LoadContext *) AcquireMagickMemory(sizeof(*lc));
811 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
815 * Initialize members of the MngInfo structure.
817 (void) ResetMagickMemory(lc,0,sizeof(LoadContext));
821 lc->context = ddjvu_context_create("ImageMagick djvu loader"); /* g_program_name */
823 ddjvu_cache_set_size(lc->context, 1); /* right? */
825 /* document: here we don't have a filename, but, for the sake of generality, a FILE* ! */
826 url="http://www.imagemagick.org/fake.djvu";
827 lc->document = ddjvu_document_create(lc->context, url, use_cache); /* don't cache */
828 ddjvu_document_set_user_data(lc->document, lc);
831 /* now we wait the message-request for data: */
832 message = ddjvu_message_wait(lc->context);
834 if (message->m_any.tag != DDJVU_NEWSTREAM) {
835 /* fixme: the djvu context, document! */
837 ddjvu_document_release(lc->document);
838 ddjvu_context_release(lc->context);
840 RelinquishMagickMemory(lc);
842 ThrowReaderException(ResourceLimitError,"Djvu initial message: unexpected type");
843 return NULL; /* error! */
846 lc->streamid = message->m_newstream.streamid;
847 ddjvu_message_pop(lc->context);
849 message = pump_data_until_message(lc,image);
850 /* now process the messages: */
854 process_message(message);
855 ddjvu_message_pop(lc->context);
856 } while ((message = ddjvu_message_peek(lc->context)));
858 /* fixme: i hope we have not read any messages pertinent(?) related to the page itself! */
860 while (lc->pages == 0) {
861 message = ddjvu_message_wait(lc->context);
862 process_message(message);
863 ddjvu_message_pop(lc->context);
866 images=NewImageList();
868 if (image_info->number_scenes != 0)
870 for ( ; i < (ssize_t) lc->pages; i++)
872 image=ReadOneDJVUImage(lc,i,image_info,exception);
873 if (image == (Image *) NULL)
876 AppendImageToList(&images,CloneImageList(image,exception));
877 if (image_info->number_scenes != 0)
878 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
882 (void) CloseBlob(images);
883 if (image != (Image *) NULL)
884 image=DestroyImageList(image);
887 if ((image->page.width == 0) && (image->page.height == 0))
889 image->page.width = image->columns+image->page.x;
890 image->page.height = image->rows+image->page.y;
892 if (image->columns == 0 || image->rows == 0)
894 if (logging != MagickFalse)
895 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
896 "exit ReadDJVUImage() with error.");
897 ThrowReaderException(CorruptImageError,"CorruptImage");
900 if (logging != MagickFalse)
901 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadDJVUImage()");
905 return(GetFirstImageInList(images));
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
914 % R e g i s t e r D J V U I m a g e %
918 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
920 % RegisterDJVUImage() adds attributes for the DJVU image format to
921 % the list of supported formats. The attributes include the image format
922 % tag, a method to read and/or write the format, whether the format
923 % supports the saving of more than one frame to the same file or blob,
924 % whether the format supports native in-memory I/O, and a brief
925 % description of the format.
927 % The format of the RegisterDJVUImage method is:
929 % size_t RegisterDJVUImage(void)
932 ModuleExport size_t RegisterDJVUImage(void)
935 version[MaxTextExtent];
943 "See http://www.djvuzone.org/ for details about the DJVU format. The\n"
944 "DJVU 1.2 specification is available there and at\n"
945 "ftp://swrinde.nde.swri.edu/pub/djvu/documents/."
949 #if defined(DJVU_LIBDJVU_VER_STRING)
950 (void) ConcatenateMagickString(version,"libdjvu ",MaxTextExtent);
951 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MaxTextExtent);
953 entry=SetMagickInfo("DJVU");
954 #if defined(MAGICKCORE_DJVU_DELEGATE)
955 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
957 entry->raw=MagickTrue;
958 entry->magick=(IsImageFormatHandler *) IsDJVU;
959 entry->adjoin=MagickFalse;
960 entry->thread_support=MagickTrue;
961 entry->description=AcquireString("Déjà vu");
962 entry->module=AcquireString("DJVU");
963 if (*version != '\0')
964 entry->version=AcquireString(version);
965 entry->note=AcquireString(DJVUNote);
966 (void) RegisterMagickInfo(entry);
967 return(MagickImageCoderSignature);
971 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % U n r e g i s t e r D J V U I m a g e %
979 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
981 % UnregisterDJVUImage() removes format registrations made by the
982 % DJVU module from the list of supported formats.
984 % The format of the UnregisterDJVUImage method is:
986 % UnregisterDJVUImage(void)
989 ModuleExport void UnregisterDJVUImage(void)
991 (void) UnregisterMagickInfo("DJVU");