2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20 % Copyright 1999-2019 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 % https://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/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/monitor.h"
55 #include "MagickCore/monitor-private.h"
56 #include "MagickCore/pixel-accessor.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #if defined(MAGICKCORE_DJVU_DELEGATE)
62 #include <libdjvu/ddjvuapi.h>
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % IsDJVU() returns MagickTrue if the image format type, identified by the
77 % magick string, is DJVU.
79 % The format of the IsDJVU method is:
81 % MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
83 % A description of each parameter follows:
85 % o magick: compare image format pattern against these bytes.
87 % o length: Specifies the length of the magick string.
90 static MagickBooleanType IsDJVU(const unsigned char *magick,const size_t length)
94 if (memcmp(magick,"AT&TFORM",8) == 0)
99 #if defined(MAGICKCORE_DJVU_DELEGATE)
101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 % R e a d D J V U I m a g e %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % ReadDJVUImage() reads DJVU image and returns it. It allocates the memory
112 % necessary for the new Image structure and returns a pointer to the new
113 % image or set of images.
115 % The format of the ReadDJVUImage method is:
117 % Image *ReadDJVUImage(const ImageInfo *image_info,
118 % ExceptionInfo *exception)
120 % A description of each parameter follows:
122 % o image_info: the image info.
124 % o exception: return any errors or warnings in this structure.
128 #if defined(__cplusplus) || defined(c_plusplus)
132 typedef struct _LoadContext
137 ddjvu_context_t* context;
138 ddjvu_document_t *document;
145 #define BLOCKSIZE 65536
148 pump_data(Image *image, LoadContext* lc)
150 int blocksize = BLOCKSIZE;
151 char data[BLOCKSIZE];
154 /* i might check for a condition! */
155 while ((size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
156 ddjvu_stream_write(lc->document, lc->streamid, data, size);
159 ddjvu_stream_write(lc->document, lc->streamid, data, size);
160 ddjvu_stream_close(lc->document, lc->streamid, 0);
164 /* returns NULL only after all is delivered! */
165 static ddjvu_message_t*
166 pump_data_until_message(LoadContext *lc,Image *image) /* ddjvu_context_t *context, type ddjvu_document_type_t */
168 size_t blocksize = BLOCKSIZE;
169 unsigned char data[BLOCKSIZE];
171 ddjvu_message_t *message;
173 /* i might check for a condition! */
175 while (!(message = ddjvu_message_peek(lc->context))
176 && (size = (size_t) ReadBlob(image,(size_t) blocksize,data)) == blocksize) {
177 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
182 ddjvu_stream_write(lc->document, lc->streamid, (char *) data, size);
183 ddjvu_stream_close(lc->document, lc->streamid, 0);
189 static const char *message_tag_name(ddjvu_message_tag_t tag)
191 static char* names[] =
204 if (tag <= DDJVU_PROGRESS)
213 /* write out nice info on the message,
214 * and store in *user* data the info on progress.
217 process_message(ddjvu_message_t *message)
221 ddjvu_context_t* context= message->m_any.context;
227 printf("*** %s: %s.\n",__FUNCTION__, message_tag_name(message->m_any.tag));
231 switch (message->m_any.tag){
234 ddjvu_document_t* document= message->m_any.document;
235 /* ddjvu_document_decoding_status is set by libdjvu! */
236 /* we have some info on the document */
237 LoadContext *lc = (LoadContext *) ddjvu_document_get_user_data(document);
238 lc->pages = ddjvu_document_get_pagenum(document);
240 printf("the doc has %d pages\n", ddjvu_document_get_pagenum(document));
246 printf("the name of the chunk is: %s\n", message->m_chunk.chunkid);
255 ddjvu_page_t* page = message->m_any.page;
256 page_info* info = ddjvu_page_get_user_data(page);
258 printf("page decoding status: %d %s%s%s\n",
259 ddjvu_page_decoding_status(page),
260 status_color, status_name(ddjvu_page_decoding_status(page)), color_reset);
262 printf("the page LAYOUT changed: width x height: %d x %d @ %d dpi. Version %d, type %d\n",
263 // printf("page info:\n width x height: %d x %d @ %d dpi, version %d, type %d\n",
264 ddjvu_page_get_width(page),
265 ddjvu_page_get_height(page),
266 ddjvu_page_get_resolution(page),
267 ddjvu_page_get_version(page),
268 /* DDJVU_PAGETYPE_BITONAL */
269 ddjvu_page_get_type(page));
276 case DDJVU_REDISPLAY:
280 ddjvu_page_t* page = message->m_any.page;
281 page_info* info = ddjvu_page_get_user_data(page);
283 printf("the page can/should be REDISPLAYED\n");
291 printf("PROGRESS:\n");
295 printf("simply ERROR!\n message:\t%s\nfunction:\t%s(file %s)\nlineno:\t%d\n",
296 message->m_error.message,
297 message->m_error.function,
298 message->m_error.filename,
299 message->m_error.lineno);
303 printf("INFO: %s!\n", message->m_info.message);
307 printf("unexpected\n");
309 return(message->m_any.tag);
313 #if defined(__cplusplus) || defined(c_plusplus)
321 * DjVu advertised readiness to provide bitmap: So get it!
322 * we use the RGB format!
325 get_page_image(LoadContext *lc, ddjvu_page_t *page, int x, int y, int w, int h, ExceptionInfo *exception ) {
345 rect.w = (unsigned int) w; /* /10 */
346 rect.h = (unsigned int) h; /* /10 */
349 type = ddjvu_page_get_type(lc->page);
351 /* stride of this temporary buffer: */
352 stride = (type == DDJVU_PAGETYPE_BITONAL)?
353 (image->columns + 7)/8 : image->columns *3;
355 q = (unsigned char *) AcquireQuantumMemory(image->rows,stride);
356 if (q == (unsigned char *) NULL)
359 format = ddjvu_format_create(
360 (type == DDJVU_PAGETYPE_BITONAL)?DDJVU_FORMAT_LSBTOMSB : DDJVU_FORMAT_RGB24,
361 /* DDJVU_FORMAT_RGB24
362 * DDJVU_FORMAT_RGBMASK32*/
363 /* DDJVU_FORMAT_RGBMASK32 */
367 /* fixme: ThrowReaderException is a macro, which uses `exception' variable */
371 /* ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); */
375 ddjvu_format_set_row_order(format, 1);
376 ddjvu_format_set_y_direction(format, 1);
378 ret = ddjvu_page_render(page,
379 DDJVU_RENDER_COLOR, /* ddjvu_render_mode_t */
386 ddjvu_format_release(format);
389 if (type == DDJVU_PAGETYPE_BITONAL) {
392 printf("%s: expanding BITONAL page/image\n", __FUNCTION__);
396 for (y=0; y < (ssize_t) image->rows; y++)
398 Quantum * o = QueueAuthenticPixels(image,0,y,image->columns,1,exception);
399 if (o == (Quantum *) NULL)
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 SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x00 : 0x01),o);
414 o+=GetPixelChannels(image);
416 if (SyncAuthenticPixels(image,exception) == MagickFalse)
420 SyncImage(image,exception);
423 printf("%s: expanding PHOTO page/image\n", __FUNCTION__);
425 /* now transfer line-wise: */
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,exception);
441 if (r == (Quantum *) NULL)
443 for (x=0; x < (ssize_t) image->columns; x++)
445 SetPixelRed(image,ScaleCharToQuantum(*s++),r);
446 SetPixelGreen(image,ScaleCharToQuantum(*s++),r);
447 SetPixelBlue(image,ScaleCharToQuantum(*s++),r);
448 r+=GetPixelChannels(image);
451 (void) SyncAuthenticPixels(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 Image *ReadOneDJVUImage(LoadContext* lc,const int pagenum,
561 const ImageInfo *image_info,ExceptionInfo *exception)
566 ddjvu_pageinfo_t info;
567 ddjvu_message_t *message;
571 MagickBooleanType status;
573 /* so, we know that the page is there! Get its dimension, and */
575 /* Read one DJVU image */
578 /* register Quantum *q; */
580 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)));
608 } while (!ddjvu_page_decoding_done(lc->page));
610 ddjvu_document_get_pageinfo(lc->document, pagenum, &info);
612 image->resolution.x = (float) info.dpi;
613 image->resolution.y =(float) info.dpi;
614 if (image_info->density != (char *) NULL)
623 Set rendering resolution.
625 flags=ParseGeometry(image_info->density,&geometry_info);
626 image->resolution.x=geometry_info.rho;
627 image->resolution.y=geometry_info.sigma;
628 if ((flags & SigmaValue) == 0)
629 image->resolution.y=image->resolution.x;
630 info.width*=image->resolution.x/info.dpi;
631 info.height*=image->resolution.y/info.dpi;
632 info.dpi=(ssize_t) MagickMax(image->resolution.x,image->resolution.y);
634 type = ddjvu_page_get_type(lc->page);
636 /* double -> float! */
637 /* image->gamma = (float)ddjvu_page_get_gamma(lc->page); */
639 /* mmc: set image->depth */
640 /* mmc: This from the type */
642 image->columns=(size_t) info.width;
643 image->rows=(size_t) info.height;
645 /* mmc: bitonal should be palettized, and compressed! */
646 if (type == DDJVU_PAGETYPE_BITONAL){
647 image->colorspace = GRAYColorspace;
648 image->storage_class = PseudoClass;
649 image->depth = 8UL; /* i only support that? */
651 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
652 ThrowReaderException(ResourceLimitError,
653 "MemoryAllocationFailed");
655 image->colorspace = RGBColorspace;
656 image->storage_class = DirectClass;
657 /* fixme: MAGICKCORE_QUANTUM_DEPTH ?*/
658 image->depth = 8UL; /* i only support that? */
660 image->alpha_trait = BlendPixelTrait;
661 /* is this useful? */
663 status=SetImageExtent(image,image->columns,image->rows,exception);
664 if (status == MagickFalse)
665 return(DestroyImageList(image));
667 printf("now filling %.20g x %.20g\n",(double) image->columns,(double)
674 /* q = QueueAuthenticPixels(image,0,0,image->columns,image->rows); */
675 get_page_image(lc, lc->page, 0, 0, info.width, info.height, exception);
678 for (i = 0;i< image->rows; i++)
681 q = QueueAuthenticPixels(image,0,i,image->columns,1);
682 get_page_line(lc, i, quantum_info);
683 SyncAuthenticPixels(image);
686 #endif /* per_line */
690 printf("END: finished filling %.20g x %.20g\n",(double) image->columns,
691 (double) image->rows);
695 SyncImage(image,exception);
696 /* mmc: ??? Convert PNM pixels to runlength-encoded MIFF packets. */
697 /* image->colors = */
699 /* how is the line padding / stride? */
702 ddjvu_page_release(lc->page);
706 /* image->page.y=mng_info->y_off[mng_info->object_id]; */
708 image=DestroyImage(image);
710 /* end of reading one DJVU page/image */
715 if (AcquireImageColormap(image,2,exception) == MagickFalse)
716 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
718 Monochrome colormap. mmc: this the default!
720 image->colormap[0].red=QuantumRange;
721 image->colormap[0].green=QuantumRange;
722 image->colormap[0].blue=QuantumRange;
723 image->colormap[1].red=0;
724 image->colormap[1].green=0;
725 image->colormap[1].blue=0;
728 static void djvu_close_lc(LoadContext* lc)
731 ddjvu_document_release(lc->document);
733 ddjvu_context_release(lc->context);
735 ddjvu_page_release(lc->page);
736 RelinquishMagickMemory(lc);
739 static Image *ReadDJVUImage(const ImageInfo *image_info,
740 ExceptionInfo *exception)
768 assert(image_info != (const ImageInfo *) NULL);
769 assert(image_info->signature == MagickCoreSignature);
772 if (image_info->debug != MagickFalse)
773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", image_info->filename);
775 assert(exception != (ExceptionInfo *) NULL);
776 assert(exception->signature == MagickCoreSignature);
779 logging = LogMagickEvent(CoderEvent,GetMagickModule(),"enter ReadDJVUImage()");
782 image = AcquireImage(image_info,exception); /* 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) memset(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="https://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 images->extent=GetBlobSize(image);
873 if (image_info->number_scenes != 0)
874 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
878 if (images != (Image *) NULL)
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[MagickPathExtent];
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 ",MagickPathExtent);
948 (void) ConcatenateMagickString(version,DJVU_LIBDJVU_VER_STRING,MagickPathExtent);
950 entry=AcquireMagickInfo("DJVU","DJVU","Deja vu");
951 #if defined(MAGICKCORE_DJVU_DELEGATE)
952 entry->decoder=(DecodeImageHandler *) ReadDJVUImage;
954 entry->magick=(IsImageFormatHandler *) IsDJVU;
955 entry->flags|=CoderRawSupportFlag;
956 entry->flags^=CoderAdjoinFlag;
957 if (*version != '\0')
958 entry->version=AcquireString(version);
959 entry->note=AcquireString(DJVUNote);
960 (void) RegisterMagickInfo(entry);
961 return(MagickImageCoderSignature);
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
969 % U n r e g i s t e r D J V U I m a g e %
973 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
975 % UnregisterDJVUImage() removes format registrations made by the
976 % DJVU module from the list of supported formats.
978 % The format of the UnregisterDJVUImage method is:
980 % UnregisterDJVUImage(void)
983 ModuleExport void UnregisterDJVUImage(void)
985 (void) UnregisterMagickInfo("DJVU");