27 // However, the following notice accompanied the original version of this |
27 // However, the following notice accompanied the original version of this |
28 // file: |
28 // file: |
29 // |
29 // |
30 // |
30 // |
31 // Little cms |
31 // Little cms |
32 // Copyright (C) 1998-2006 Marti Maria |
32 // Copyright (C) 1998-2007 Marti Maria |
33 // |
33 // |
34 // Permission is hereby granted, free of charge, to any person obtaining |
34 // Permission is hereby granted, free of charge, to any person obtaining |
35 // a copy of this software and associated documentation files (the "Software"), |
35 // a copy of this software and associated documentation files (the "Software"), |
36 // to deal in the Software without restriction, including without limitation |
36 // to deal in the Software without restriction, including without limitation |
37 // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
37 // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
60 // Memory-based stream --------------------------------------------------- |
60 // Memory-based stream --------------------------------------------------- |
61 |
61 |
62 typedef struct { |
62 typedef struct { |
63 LPBYTE Block; // Points to allocated memory |
63 LPBYTE Block; // Points to allocated memory |
64 size_t Size; // Size of allocated memory |
64 size_t Size; // Size of allocated memory |
65 int Pointer; // Points to current location |
65 size_t Pointer; // Points to current location |
66 int FreeBlockOnClose; // As title |
66 int FreeBlockOnClose; // As title |
67 |
67 |
68 } FILEMEM; |
68 } FILEMEM; |
69 |
69 |
70 static |
70 static |
71 LPVOID MemoryOpen(LPBYTE Block, size_t Size, char Mode) |
71 LPVOID MemoryOpen(LPBYTE Block, size_t Size, char Mode) |
72 { |
72 { |
73 FILEMEM* fm = (FILEMEM*) malloc(sizeof(FILEMEM)); |
73 FILEMEM* fm = (FILEMEM*) _cmsMalloc(sizeof(FILEMEM)); |
|
74 if (fm == NULL) return NULL; |
|
75 |
74 ZeroMemory(fm, sizeof(FILEMEM)); |
76 ZeroMemory(fm, sizeof(FILEMEM)); |
75 |
77 |
76 if (Mode == 'r') { |
78 if (Mode == 'r') { |
77 |
79 |
78 fm ->Block = (LPBYTE) malloc(Size); |
80 fm ->Block = (LPBYTE) _cmsMalloc(Size); |
79 if (fm ->Block == NULL) { |
81 if (fm ->Block == NULL) { |
80 free(fm); |
82 _cmsFree(fm); |
81 return NULL; |
83 return NULL; |
82 } |
84 } |
83 |
|
84 |
85 |
85 CopyMemory(fm->Block, Block, Size); |
86 CopyMemory(fm->Block, Block, Size); |
86 fm ->FreeBlockOnClose = TRUE; |
87 fm ->FreeBlockOnClose = TRUE; |
87 } |
88 } |
88 else { |
89 else { |
101 size_t MemoryRead(LPVOID buffer, size_t size, size_t count, struct _lcms_iccprofile_struct* Icc) |
102 size_t MemoryRead(LPVOID buffer, size_t size, size_t count, struct _lcms_iccprofile_struct* Icc) |
102 { |
103 { |
103 FILEMEM* ResData = (FILEMEM*) Icc ->stream; |
104 FILEMEM* ResData = (FILEMEM*) Icc ->stream; |
104 LPBYTE Ptr; |
105 LPBYTE Ptr; |
105 size_t len = size * count; |
106 size_t len = size * count; |
106 |
107 size_t extent = ResData -> Pointer + len; |
107 |
108 |
108 if (ResData -> Pointer + len > ResData -> Size){ |
109 if (len == 0) { |
|
110 return 0; |
|
111 } |
|
112 |
|
113 if (len / size != count) { |
|
114 cmsSignalError(LCMS_ERRC_ABORTED, "Read from memory error. Integer overflow with count / size."); |
|
115 return 0; |
|
116 } |
|
117 |
|
118 if (extent < len || extent < ResData -> Pointer) { |
|
119 cmsSignalError(LCMS_ERRC_ABORTED, "Read from memory error. Integer overflow with len."); |
|
120 return 0; |
|
121 } |
|
122 |
|
123 if (ResData -> Pointer + len > ResData -> Size) { |
109 |
124 |
110 len = (ResData -> Size - ResData -> Pointer); |
125 len = (ResData -> Size - ResData -> Pointer); |
111 cmsSignalError(LCMS_ERRC_WARNING, "Read from memory error. Got %d bytes, block should be of %d bytes", len * size, count * size); |
126 cmsSignalError(LCMS_ERRC_ABORTED, "Read from memory error. Got %d bytes, block should be of %d bytes", len * size, count * size); |
112 |
127 return 0; |
113 } |
128 } |
114 |
129 |
115 Ptr = ResData -> Block; |
130 Ptr = ResData -> Block; |
116 Ptr += ResData -> Pointer; |
131 Ptr += ResData -> Pointer; |
117 CopyMemory(buffer, Ptr, len); |
132 CopyMemory(buffer, Ptr, len); |
207 static |
228 static |
208 size_t FileRead(void *buffer, size_t size, size_t count, struct _lcms_iccprofile_struct* Icc) |
229 size_t FileRead(void *buffer, size_t size, size_t count, struct _lcms_iccprofile_struct* Icc) |
209 { |
230 { |
210 size_t nReaded = fread(buffer, size, count, (FILE*) Icc->stream); |
231 size_t nReaded = fread(buffer, size, count, (FILE*) Icc->stream); |
211 if (nReaded != count) { |
232 if (nReaded != count) { |
212 cmsSignalError(LCMS_ERRC_WARNING, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size); |
233 cmsSignalError(LCMS_ERRC_ABORTED, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size); |
213 return 0; |
234 return 0; |
214 } |
235 } |
215 |
236 |
216 return nReaded; |
237 return nReaded; |
217 } |
238 } |
218 |
239 |
219 |
240 |
220 static |
241 static |
221 BOOL FileSeek(struct _lcms_iccprofile_struct* Icc, size_t offset) |
242 LCMSBOOL FileSeek(struct _lcms_iccprofile_struct* Icc, size_t offset) |
222 { |
243 { |
223 if (fseek((FILE*) Icc ->stream, (long) offset, SEEK_SET) != 0) { |
244 if (fseek((FILE*) Icc ->stream, (long) offset, SEEK_SET) != 0) { |
224 |
245 |
225 cmsSignalError(LCMS_ERRC_ABORTED, "Seek error; probably corrupted file"); |
246 cmsSignalError(LCMS_ERRC_ABORTED, "Seek error; probably corrupted file"); |
226 return TRUE; |
247 return TRUE; |
352 |
373 |
353 i = _cmsSearchTag(Icc, sig, FALSE); |
374 i = _cmsSearchTag(Icc, sig, FALSE); |
354 |
375 |
355 if (i >=0) { |
376 if (i >=0) { |
356 |
377 |
357 if (Icc -> TagPtrs[i]) free(Icc -> TagPtrs[i]); |
378 if (Icc -> TagPtrs[i]) _cmsFree(Icc -> TagPtrs[i]); |
358 } |
379 } |
359 else { |
380 else { |
360 |
381 |
361 i = Icc -> TagCount; |
382 i = Icc -> TagCount; |
362 Icc -> TagCount++; |
383 Icc -> TagCount++; |
363 |
384 |
364 if (Icc ->TagCount >= MAX_TABLE_TAG) { |
385 if (Icc ->TagCount >= MAX_TABLE_TAG) { |
365 |
386 |
366 cmsSignalError(LCMS_ERRC_ABORTED, "Too many tags (%d)", MAX_TABLE_TAG); |
387 cmsSignalError(LCMS_ERRC_ABORTED, "Too many tags (%d)", MAX_TABLE_TAG); |
367 Icc ->TagCount = MAX_TABLE_TAG-1; |
388 Icc ->TagCount = MAX_TABLE_TAG-1; |
|
389 return NULL; |
368 } |
390 } |
369 } |
391 } |
370 |
392 |
371 |
393 |
372 Ptr = malloc(size); |
394 Ptr = _cmsMalloc(size); |
|
395 if (Ptr == NULL) return NULL; |
|
396 |
373 CopyMemory(Ptr, Init, size); |
397 CopyMemory(Ptr, Init, size); |
374 |
398 |
375 Icc ->TagNames[i] = sig; |
399 Icc ->TagNames[i] = sig; |
376 Icc ->TagSizes[i] = size; |
400 Icc ->TagSizes[i] = size; |
377 Icc ->TagPtrs[i] = Ptr; |
401 Icc ->TagPtrs[i] = Ptr; |
500 // ----------------------------------------------------------------------- Set/Get several struct members |
526 // ----------------------------------------------------------------------- Set/Get several struct members |
501 |
527 |
502 |
528 |
503 |
529 |
504 |
530 |
505 BOOL LCMSEXPORT cmsTakeMediaWhitePoint(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
531 LCMSBOOL LCMSEXPORT cmsTakeMediaWhitePoint(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
506 { |
532 { |
507 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
533 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
508 *Dest = Icc -> MediaWhitePoint; |
534 *Dest = Icc -> MediaWhitePoint; |
509 return TRUE; |
535 return TRUE; |
510 } |
536 } |
511 |
537 |
512 |
538 |
513 BOOL LCMSEXPORT cmsTakeMediaBlackPoint(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
539 LCMSBOOL LCMSEXPORT cmsTakeMediaBlackPoint(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
514 { |
540 { |
515 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
541 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
516 *Dest = Icc -> MediaBlackPoint; |
542 *Dest = Icc -> MediaBlackPoint; |
517 return TRUE; |
543 return TRUE; |
518 } |
544 } |
519 |
545 |
520 BOOL LCMSEXPORT cmsTakeIluminant(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
546 LCMSBOOL LCMSEXPORT cmsTakeIluminant(LPcmsCIEXYZ Dest, cmsHPROFILE hProfile) |
521 { |
547 { |
522 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
548 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
523 *Dest = Icc -> Illuminant; |
549 *Dest = Icc -> Illuminant; |
524 return TRUE; |
550 return TRUE; |
525 } |
551 } |
594 { |
620 { |
595 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
621 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
596 Icc -> PCS = pcs; |
622 Icc -> PCS = pcs; |
597 } |
623 } |
598 |
624 |
599 |
|
600 |
|
601 icColorSpaceSignature LCMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile) |
625 icColorSpaceSignature LCMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile) |
602 { |
626 { |
603 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
627 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
604 return Icc -> ColorSpace; |
628 return Icc -> ColorSpace; |
605 } |
629 } |
606 |
630 |
607 |
|
608 |
|
609 void LCMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, icColorSpaceSignature sig) |
631 void LCMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, icColorSpaceSignature sig) |
610 { |
632 { |
611 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
633 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
612 Icc -> ColorSpace = sig; |
634 Icc -> ColorSpace = sig; |
613 } |
635 } |
614 |
636 |
615 |
|
616 icProfileClassSignature LCMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile) |
637 icProfileClassSignature LCMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile) |
617 { |
638 { |
618 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
639 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) hProfile; |
619 return Icc -> DeviceClass; |
640 return Icc -> DeviceClass; |
620 } |
641 } |
690 Stored ->CLut16params.p8 = NULL; |
710 Stored ->CLut16params.p8 = NULL; |
691 return TRUE; |
711 return TRUE; |
692 } |
712 } |
693 |
713 |
694 |
714 |
695 BOOL LCMSEXPORT _cmsAddXYZTag(cmsHPROFILE hProfile, icTagSignature sig, const cmsCIEXYZ* XYZ) |
715 LCMSBOOL LCMSEXPORT _cmsAddXYZTag(cmsHPROFILE hProfile, icTagSignature sig, const cmsCIEXYZ* XYZ) |
696 { |
716 { |
697 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
717 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
698 |
718 |
699 _cmsInitTag(Icc, sig, sizeof(cmsCIEXYZ), XYZ); |
719 _cmsInitTag(Icc, sig, sizeof(cmsCIEXYZ), XYZ); |
700 return TRUE; |
720 return TRUE; |
701 } |
721 } |
702 |
722 |
703 |
723 |
704 BOOL LCMSEXPORT _cmsAddTextTag(cmsHPROFILE hProfile, icTagSignature sig, const char* Text) |
724 LCMSBOOL LCMSEXPORT _cmsAddTextTag(cmsHPROFILE hProfile, icTagSignature sig, const char* Text) |
705 { |
725 { |
706 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
726 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
707 |
727 |
708 _cmsInitTag(Icc, sig, strlen(Text)+1, (LPVOID) Text); |
728 _cmsInitTag(Icc, sig, strlen(Text)+1, (LPVOID) Text); |
709 return TRUE; |
729 return TRUE; |
710 } |
730 } |
711 |
731 |
712 BOOL LCMSEXPORT _cmsAddGammaTag(cmsHPROFILE hProfile, icTagSignature sig, LPGAMMATABLE TransferFunction) |
732 LCMSBOOL LCMSEXPORT _cmsAddGammaTag(cmsHPROFILE hProfile, icTagSignature sig, LPGAMMATABLE TransferFunction) |
713 { |
733 { |
714 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
734 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
715 |
735 |
716 _cmsInitTag(Icc, sig, SizeOfGammaTab(TransferFunction), TransferFunction); |
736 _cmsInitTag(Icc, sig, SizeOfGammaTab(TransferFunction), TransferFunction); |
717 return TRUE; |
737 return TRUE; |
718 } |
738 } |
719 |
739 |
720 |
740 |
721 BOOL LCMSEXPORT _cmsAddChromaticityTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsCIExyYTRIPLE Chrm) |
741 LCMSBOOL LCMSEXPORT _cmsAddChromaticityTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsCIExyYTRIPLE Chrm) |
722 { |
742 { |
723 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
743 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
724 |
744 |
725 _cmsInitTag(Icc, sig, sizeof(cmsCIExyYTRIPLE), Chrm); |
745 _cmsInitTag(Icc, sig, sizeof(cmsCIExyYTRIPLE), Chrm); |
726 return TRUE; |
746 return TRUE; |
727 } |
747 } |
728 |
748 |
729 |
749 |
730 BOOL LCMSEXPORT _cmsAddSequenceDescriptionTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsSEQ pseq) |
750 LCMSBOOL LCMSEXPORT _cmsAddSequenceDescriptionTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsSEQ pseq) |
731 { |
751 { |
732 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
752 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
733 |
753 |
734 _cmsInitTag(Icc, sig, sizeof(int) + pseq -> n * sizeof(cmsPSEQDESC), pseq); |
754 _cmsInitTag(Icc, sig, sizeof(int) + pseq -> n * sizeof(cmsPSEQDESC), pseq); |
735 return TRUE; |
755 return TRUE; |
736 |
756 |
737 } |
757 } |
738 |
758 |
739 |
759 |
740 BOOL LCMSEXPORT _cmsAddNamedColorTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsNAMEDCOLORLIST nc) |
760 LCMSBOOL LCMSEXPORT _cmsAddNamedColorTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsNAMEDCOLORLIST nc) |
741 { |
761 { |
742 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
762 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
743 |
763 |
744 _cmsInitTag(Icc, sig, sizeof(cmsNAMEDCOLORLIST) + (nc ->nColors - 1) * sizeof(cmsNAMEDCOLOR), nc); |
764 _cmsInitTag(Icc, sig, sizeof(cmsNAMEDCOLORLIST) + (nc ->nColors - 1) * sizeof(cmsNAMEDCOLOR), nc); |
745 return FALSE; |
765 return TRUE; |
746 } |
766 } |
747 |
767 |
748 |
768 |
749 BOOL LCMSEXPORT _cmsAddDateTimeTag(cmsHPROFILE hProfile, icTagSignature sig, struct tm *DateTime) |
769 LCMSBOOL LCMSEXPORT _cmsAddDateTimeTag(cmsHPROFILE hProfile, icTagSignature sig, struct tm *DateTime) |
750 { |
770 { |
751 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
771 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
752 |
772 |
753 _cmsInitTag(Icc, sig, sizeof(struct tm), DateTime); |
773 _cmsInitTag(Icc, sig, sizeof(struct tm), DateTime); |
754 return FALSE; |
774 return TRUE; |
755 } |
775 } |
756 |
776 |
757 |
777 |
758 BOOL LCMSEXPORT _cmsAddColorantTableTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsNAMEDCOLORLIST nc) |
778 LCMSBOOL LCMSEXPORT _cmsAddColorantTableTag(cmsHPROFILE hProfile, icTagSignature sig, LPcmsNAMEDCOLORLIST nc) |
759 { |
779 { |
760 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
780 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
761 |
781 |
762 _cmsInitTag(Icc, sig, sizeof(cmsNAMEDCOLORLIST) + (nc ->nColors - 1) * sizeof(cmsNAMEDCOLOR), nc); |
782 _cmsInitTag(Icc, sig, sizeof(cmsNAMEDCOLORLIST) + (nc ->nColors - 1) * sizeof(cmsNAMEDCOLOR), nc); |
763 return FALSE; |
783 return TRUE; |
764 } |
784 } |
|
785 |
|
786 |
|
787 LCMSBOOL LCMSEXPORT _cmsAddChromaticAdaptationTag(cmsHPROFILE hProfile, icTagSignature sig, const cmsCIEXYZ* mat) |
|
788 { |
|
789 LPLCMSICCPROFILE Icc = (LPLCMSICCPROFILE) (LPSTR) hProfile; |
|
790 |
|
791 _cmsInitTag(Icc, sig, 3*sizeof(cmsCIEXYZ), mat); |
|
792 return TRUE; |
|
793 |
|
794 } |
|
795 |
|
796 |