Line | Count | Source (jump to first uncovered line) |
1 | | //--------------------------------------------------------------------------------- |
2 | | // |
3 | | // Little Color Management System |
4 | | // Copyright (c) 1998-2023 Marti Maria Saguer |
5 | | // |
6 | | // Permission is hereby granted, free of charge, to any person obtaining |
7 | | // a copy of this software and associated documentation files (the "Software"), |
8 | | // to deal in the Software without restriction, including without limitation |
9 | | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 | | // and/or sell copies of the Software, and to permit persons to whom the Software |
11 | | // is furnished to do so, subject to the following conditions: |
12 | | // |
13 | | // The above copyright notice and this permission notice shall be included in |
14 | | // all copies or substantial portions of the Software. |
15 | | // |
16 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
18 | | // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19 | | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
20 | | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21 | | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
22 | | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23 | | // |
24 | | //--------------------------------------------------------------------------------- |
25 | | // |
26 | | |
27 | | #include "lcms2_internal.h" |
28 | | |
29 | | // Generic I/O, tag dictionary management, profile struct |
30 | | |
31 | | // IOhandlers are abstractions used by littleCMS to read from whatever file, stream, |
32 | | // memory block or any storage. Each IOhandler provides implementations for read, |
33 | | // write, seek and tell functions. LittleCMS code deals with IO across those objects. |
34 | | // In this way, is easier to add support for new storage media. |
35 | | |
36 | | // NULL stream, for taking care of used space ------------------------------------- |
37 | | |
38 | | // NULL IOhandler basically does nothing but keep track on how many bytes have been |
39 | | // written. This is handy when creating profiles, where the file size is needed in the |
40 | | // header. Then, whole profile is serialized across NULL IOhandler and a second pass |
41 | | // writes the bytes to the pertinent IOhandler. |
42 | | |
43 | | typedef struct { |
44 | | cmsUInt32Number Pointer; // Points to current location |
45 | | } FILENULL; |
46 | | |
47 | | static |
48 | | cmsUInt32Number NULLRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count) |
49 | 0 | { |
50 | 0 | FILENULL* ResData = (FILENULL*) iohandler ->stream; |
51 | |
|
52 | 0 | cmsUInt32Number len = size * count; |
53 | 0 | ResData -> Pointer += len; |
54 | 0 | return count; |
55 | | |
56 | 0 | cmsUNUSED_PARAMETER(Buffer); |
57 | 0 | } |
58 | | |
59 | | static |
60 | | cmsBool NULLSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset) |
61 | 146 | { |
62 | 146 | FILENULL* ResData = (FILENULL*) iohandler ->stream; |
63 | | |
64 | 146 | ResData ->Pointer = offset; |
65 | 146 | return TRUE; |
66 | 146 | } |
67 | | |
68 | | static |
69 | | cmsUInt32Number NULLTell(cmsIOHANDLER* iohandler) |
70 | 57.6k | { |
71 | 57.6k | FILENULL* ResData = (FILENULL*) iohandler ->stream; |
72 | 57.6k | return ResData -> Pointer; |
73 | 57.6k | } |
74 | | |
75 | | static |
76 | | cmsBool NULLWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void *Ptr) |
77 | 137M | { |
78 | 137M | FILENULL* ResData = (FILENULL*) iohandler ->stream; |
79 | | |
80 | 137M | ResData ->Pointer += size; |
81 | 137M | if (ResData ->Pointer > iohandler->UsedSpace) |
82 | 136M | iohandler->UsedSpace = ResData ->Pointer; |
83 | | |
84 | 137M | return TRUE; |
85 | | |
86 | 0 | cmsUNUSED_PARAMETER(Ptr); |
87 | 0 | } |
88 | | |
89 | | static |
90 | | cmsBool NULLClose(cmsIOHANDLER* iohandler) |
91 | 16.2k | { |
92 | 16.2k | FILENULL* ResData = (FILENULL*) iohandler ->stream; |
93 | | |
94 | 16.2k | _cmsFree(iohandler ->ContextID, ResData); |
95 | 16.2k | _cmsFree(iohandler ->ContextID, iohandler); |
96 | 16.2k | return TRUE; |
97 | 16.2k | } |
98 | | |
99 | | // The NULL IOhandler creator |
100 | | cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromNULL(cmsContext ContextID) |
101 | 16.2k | { |
102 | 16.2k | struct _cms_io_handler* iohandler = NULL; |
103 | 16.2k | FILENULL* fm = NULL; |
104 | | |
105 | 16.2k | iohandler = (struct _cms_io_handler*) _cmsMallocZero(ContextID, sizeof(struct _cms_io_handler)); |
106 | 16.2k | if (iohandler == NULL) return NULL; |
107 | | |
108 | 16.2k | fm = (FILENULL*) _cmsMallocZero(ContextID, sizeof(FILENULL)); |
109 | 16.2k | if (fm == NULL) goto Error; |
110 | | |
111 | 16.2k | fm ->Pointer = 0; |
112 | | |
113 | 16.2k | iohandler ->ContextID = ContextID; |
114 | 16.2k | iohandler ->stream = (void*) fm; |
115 | 16.2k | iohandler ->UsedSpace = 0; |
116 | 16.2k | iohandler ->ReportedSize = 0; |
117 | 16.2k | iohandler ->PhysicalFile[0] = 0; |
118 | | |
119 | 16.2k | iohandler ->Read = NULLRead; |
120 | 16.2k | iohandler ->Seek = NULLSeek; |
121 | 16.2k | iohandler ->Close = NULLClose; |
122 | 16.2k | iohandler ->Tell = NULLTell; |
123 | 16.2k | iohandler ->Write = NULLWrite; |
124 | | |
125 | 16.2k | return iohandler; |
126 | | |
127 | 0 | Error: |
128 | 0 | if (iohandler) _cmsFree(ContextID, iohandler); |
129 | 0 | return NULL; |
130 | | |
131 | 16.2k | } |
132 | | |
133 | | |
134 | | // Memory-based stream -------------------------------------------------------------- |
135 | | |
136 | | // Those functions implements an iohandler which takes a block of memory as storage medium. |
137 | | |
138 | | typedef struct { |
139 | | cmsUInt8Number* Block; // Points to allocated memory |
140 | | cmsUInt32Number Size; // Size of allocated memory |
141 | | cmsUInt32Number Pointer; // Points to current location |
142 | | int FreeBlockOnClose; // As title |
143 | | |
144 | | } FILEMEM; |
145 | | |
146 | | static |
147 | | cmsUInt32Number MemoryRead(struct _cms_io_handler* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count) |
148 | 10.7M | { |
149 | 10.7M | FILEMEM* ResData = (FILEMEM*) iohandler ->stream; |
150 | 10.7M | cmsUInt8Number* Ptr; |
151 | 10.7M | cmsUInt32Number len = size * count; |
152 | | |
153 | 10.7M | if (ResData -> Pointer + len > ResData -> Size){ |
154 | | |
155 | 6.36k | len = (ResData -> Size - ResData -> Pointer); |
156 | 6.36k | cmsSignalError(iohandler ->ContextID, cmsERROR_READ, "Read from memory error. Got %d bytes, block should be of %d bytes", len, count * size); |
157 | 6.36k | return 0; |
158 | 6.36k | } |
159 | | |
160 | 10.7M | Ptr = ResData -> Block; |
161 | 10.7M | Ptr += ResData -> Pointer; |
162 | 10.7M | memmove(Buffer, Ptr, len); |
163 | 10.7M | ResData -> Pointer += len; |
164 | | |
165 | 10.7M | return count; |
166 | 10.7M | } |
167 | | |
168 | | // SEEK_CUR is assumed |
169 | | static |
170 | | cmsBool MemorySeek(struct _cms_io_handler* iohandler, cmsUInt32Number offset) |
171 | 67.0k | { |
172 | 67.0k | FILEMEM* ResData = (FILEMEM*) iohandler ->stream; |
173 | | |
174 | 67.0k | if (offset > ResData ->Size) { |
175 | 1.97k | cmsSignalError(iohandler ->ContextID, cmsERROR_SEEK, "Too few data; probably corrupted profile"); |
176 | 1.97k | return FALSE; |
177 | 1.97k | } |
178 | | |
179 | 65.1k | ResData ->Pointer = offset; |
180 | 65.1k | return TRUE; |
181 | 67.0k | } |
182 | | |
183 | | // Tell for memory |
184 | | static |
185 | | cmsUInt32Number MemoryTell(struct _cms_io_handler* iohandler) |
186 | 35.8k | { |
187 | 35.8k | FILEMEM* ResData = (FILEMEM*) iohandler ->stream; |
188 | | |
189 | 35.8k | if (ResData == NULL) return 0; |
190 | 35.8k | return ResData -> Pointer; |
191 | 35.8k | } |
192 | | |
193 | | |
194 | | // Writes data to memory, also keeps used space for further reference. |
195 | | static |
196 | | cmsBool MemoryWrite(struct _cms_io_handler* iohandler, cmsUInt32Number size, const void *Ptr) |
197 | 4.38M | { |
198 | 4.38M | FILEMEM* ResData = (FILEMEM*) iohandler ->stream; |
199 | | |
200 | 4.38M | if (ResData == NULL) return FALSE; // Housekeeping |
201 | | |
202 | | // Check for available space. Clip. |
203 | 4.38M | if (ResData->Pointer + size > ResData->Size) { |
204 | 4.37M | size = ResData ->Size - ResData->Pointer; |
205 | 4.37M | } |
206 | | |
207 | 4.38M | if (size == 0) return TRUE; // Write zero bytes is ok, but does nothing |
208 | | |
209 | 12.2k | memmove(ResData ->Block + ResData ->Pointer, Ptr, size); |
210 | 12.2k | ResData ->Pointer += size; |
211 | | |
212 | 12.2k | if (ResData ->Pointer > iohandler->UsedSpace) |
213 | 12.2k | iohandler->UsedSpace = ResData ->Pointer; |
214 | | |
215 | 12.2k | return TRUE; |
216 | 4.38M | } |
217 | | |
218 | | |
219 | | static |
220 | | cmsBool MemoryClose(struct _cms_io_handler* iohandler) |
221 | 55.6k | { |
222 | 55.6k | FILEMEM* ResData = (FILEMEM*) iohandler ->stream; |
223 | | |
224 | 55.6k | if (ResData ->FreeBlockOnClose) { |
225 | | |
226 | 54.5k | if (ResData ->Block) _cmsFree(iohandler ->ContextID, ResData ->Block); |
227 | 54.5k | } |
228 | | |
229 | 55.6k | _cmsFree(iohandler ->ContextID, ResData); |
230 | 55.6k | _cmsFree(iohandler ->ContextID, iohandler); |
231 | | |
232 | 55.6k | return TRUE; |
233 | 55.6k | } |
234 | | |
235 | | // Create a iohandler for memory block. AccessMode=='r' assumes the iohandler is going to read, and makes |
236 | | // a copy of the memory block for letting user to free the memory after invoking open profile. In write |
237 | | // mode ("w"), Buffer points to the begin of memory block to be written. |
238 | | cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromMem(cmsContext ContextID, void *Buffer, cmsUInt32Number size, const char* AccessMode) |
239 | 55.6k | { |
240 | 55.6k | cmsIOHANDLER* iohandler = NULL; |
241 | 55.6k | FILEMEM* fm = NULL; |
242 | | |
243 | 55.6k | _cmsAssert(AccessMode != NULL); |
244 | | |
245 | 55.6k | iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER)); |
246 | 55.6k | if (iohandler == NULL) return NULL; |
247 | | |
248 | 55.6k | switch (*AccessMode) { |
249 | | |
250 | 54.5k | case 'r': |
251 | 54.5k | fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM)); |
252 | 54.5k | if (fm == NULL) goto Error; |
253 | | |
254 | 54.5k | if (Buffer == NULL) { |
255 | 0 | cmsSignalError(ContextID, cmsERROR_READ, "Couldn't read profile from NULL pointer"); |
256 | 0 | goto Error; |
257 | 0 | } |
258 | | |
259 | 54.5k | fm ->Block = (cmsUInt8Number*) _cmsMalloc(ContextID, size); |
260 | 54.5k | if (fm ->Block == NULL) { |
261 | |
|
262 | 0 | _cmsFree(ContextID, fm); |
263 | 0 | _cmsFree(ContextID, iohandler); |
264 | 0 | cmsSignalError(ContextID, cmsERROR_READ, "Couldn't allocate %ld bytes for profile", (long) size); |
265 | 0 | return NULL; |
266 | 0 | } |
267 | | |
268 | | |
269 | 54.5k | memmove(fm->Block, Buffer, size); |
270 | 54.5k | fm ->FreeBlockOnClose = TRUE; |
271 | 54.5k | fm ->Size = size; |
272 | 54.5k | fm ->Pointer = 0; |
273 | 54.5k | iohandler -> ReportedSize = size; |
274 | 54.5k | break; |
275 | | |
276 | 1.10k | case 'w': |
277 | 1.10k | fm = (FILEMEM*) _cmsMallocZero(ContextID, sizeof(FILEMEM)); |
278 | 1.10k | if (fm == NULL) goto Error; |
279 | | |
280 | 1.10k | fm ->Block = (cmsUInt8Number*) Buffer; |
281 | 1.10k | fm ->FreeBlockOnClose = FALSE; |
282 | 1.10k | fm ->Size = size; |
283 | 1.10k | fm ->Pointer = 0; |
284 | 1.10k | iohandler -> ReportedSize = 0; |
285 | 1.10k | break; |
286 | | |
287 | 0 | default: |
288 | 0 | cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown access mode '%c'", *AccessMode); |
289 | 0 | return NULL; |
290 | 55.6k | } |
291 | | |
292 | 55.6k | iohandler ->ContextID = ContextID; |
293 | 55.6k | iohandler ->stream = (void*) fm; |
294 | 55.6k | iohandler ->UsedSpace = 0; |
295 | 55.6k | iohandler ->PhysicalFile[0] = 0; |
296 | | |
297 | 55.6k | iohandler ->Read = MemoryRead; |
298 | 55.6k | iohandler ->Seek = MemorySeek; |
299 | 55.6k | iohandler ->Close = MemoryClose; |
300 | 55.6k | iohandler ->Tell = MemoryTell; |
301 | 55.6k | iohandler ->Write = MemoryWrite; |
302 | | |
303 | 55.6k | return iohandler; |
304 | | |
305 | 0 | Error: |
306 | 0 | if (fm) _cmsFree(ContextID, fm); |
307 | 0 | if (iohandler) _cmsFree(ContextID, iohandler); |
308 | 0 | return NULL; |
309 | 55.6k | } |
310 | | |
311 | | // File-based stream ------------------------------------------------------- |
312 | | |
313 | | // Read count elements of size bytes each. Return number of elements read |
314 | | static |
315 | | cmsUInt32Number FileRead(cmsIOHANDLER* iohandler, void *Buffer, cmsUInt32Number size, cmsUInt32Number count) |
316 | 502k | { |
317 | 502k | cmsUInt32Number nReaded = (cmsUInt32Number) fread(Buffer, size, count, (FILE*) iohandler->stream); |
318 | | |
319 | 502k | if (nReaded != count) { |
320 | 55.5k | cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Read error. Got %d bytes, block should be of %d bytes", nReaded * size, count * size); |
321 | 55.5k | return 0; |
322 | 55.5k | } |
323 | | |
324 | 446k | return nReaded; |
325 | 502k | } |
326 | | |
327 | | // Position file pointer in the file |
328 | | static |
329 | | cmsBool FileSeek(cmsIOHANDLER* iohandler, cmsUInt32Number offset) |
330 | 72.7k | { |
331 | 72.7k | if (fseek((FILE*) iohandler ->stream, (long) offset, SEEK_SET) != 0) { |
332 | |
|
333 | 0 | cmsSignalError(iohandler ->ContextID, cmsERROR_FILE, "Seek error; probably corrupted file"); |
334 | 0 | return FALSE; |
335 | 0 | } |
336 | | |
337 | 72.7k | return TRUE; |
338 | 72.7k | } |
339 | | |
340 | | // Returns file pointer position or 0 on error, which is also a valid position. |
341 | | static |
342 | | cmsUInt32Number FileTell(cmsIOHANDLER* iohandler) |
343 | 51.1k | { |
344 | 51.1k | long t = ftell((FILE*)iohandler ->stream); |
345 | 51.1k | if (t == -1L) { |
346 | 0 | cmsSignalError(iohandler->ContextID, cmsERROR_FILE, "Tell error; probably corrupted file"); |
347 | 0 | return 0; |
348 | 0 | } |
349 | | |
350 | 51.1k | return (cmsUInt32Number)t; |
351 | 51.1k | } |
352 | | |
353 | | // Writes data to stream, also keeps used space for further reference. Returns TRUE on success, FALSE on error |
354 | | static |
355 | | cmsBool FileWrite(cmsIOHANDLER* iohandler, cmsUInt32Number size, const void* Buffer) |
356 | 3.17M | { |
357 | 3.17M | if (size == 0) return TRUE; // We allow to write 0 bytes, but nothing is written |
358 | | |
359 | 3.17M | iohandler->UsedSpace += size; |
360 | 3.17M | return (fwrite(Buffer, size, 1, (FILE*)iohandler->stream) == 1); |
361 | 3.17M | } |
362 | | |
363 | | // Closes the file |
364 | | static |
365 | | cmsBool FileClose(cmsIOHANDLER* iohandler) |
366 | 12.9k | { |
367 | 12.9k | if (fclose((FILE*) iohandler ->stream) != 0) return FALSE; |
368 | 12.9k | _cmsFree(iohandler ->ContextID, iohandler); |
369 | 12.9k | return TRUE; |
370 | 12.9k | } |
371 | | |
372 | | // Create a iohandler for disk based files. |
373 | | cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromFile(cmsContext ContextID, const char* FileName, const char* AccessMode) |
374 | 12.9k | { |
375 | 12.9k | cmsIOHANDLER* iohandler = NULL; |
376 | 12.9k | FILE* fm = NULL; |
377 | 12.9k | cmsInt32Number fileLen; |
378 | 12.9k | char mode[4] = { 0,0,0,0 }; |
379 | | |
380 | 12.9k | _cmsAssert(FileName != NULL); |
381 | 12.9k | _cmsAssert(AccessMode != NULL); |
382 | | |
383 | 12.9k | iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER)); |
384 | 12.9k | if (iohandler == NULL) return NULL; |
385 | | |
386 | | // Validate access mode |
387 | 25.9k | while (*AccessMode) { |
388 | | |
389 | 12.9k | switch (*AccessMode) |
390 | 12.9k | { |
391 | 2.72k | case 'r': |
392 | 12.9k | case 'w': |
393 | | |
394 | 12.9k | if (mode[0] == 0) { |
395 | 12.9k | mode[0] = *AccessMode; |
396 | 12.9k | mode[1] = 'b'; |
397 | 12.9k | } |
398 | 0 | else { |
399 | 0 | _cmsFree(ContextID, iohandler); |
400 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "Access mode already specified '%c'", *AccessMode); |
401 | 0 | return NULL; |
402 | 0 | } |
403 | 12.9k | break; |
404 | | |
405 | | // Close on exec. Not all runtime supports that. Up to the caller to decide. |
406 | 12.9k | case 'e': |
407 | 0 | mode[2] = 'e'; |
408 | 0 | break; |
409 | | |
410 | 0 | default: |
411 | 0 | _cmsFree(ContextID, iohandler); |
412 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "Wrong access mode '%c'", *AccessMode); |
413 | 0 | return NULL; |
414 | 12.9k | } |
415 | | |
416 | 12.9k | AccessMode++; |
417 | 12.9k | } |
418 | | |
419 | 12.9k | switch (mode[0]) { |
420 | | |
421 | 2.72k | case 'r': |
422 | 2.72k | fm = fopen(FileName, mode); |
423 | 2.72k | if (fm == NULL) { |
424 | 0 | _cmsFree(ContextID, iohandler); |
425 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "File '%s' not found", FileName); |
426 | 0 | return NULL; |
427 | 0 | } |
428 | 2.72k | fileLen = (cmsInt32Number)cmsfilelength(fm); |
429 | 2.72k | if (fileLen < 0) |
430 | 0 | { |
431 | 0 | fclose(fm); |
432 | 0 | _cmsFree(ContextID, iohandler); |
433 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of file '%s'", FileName); |
434 | 0 | return NULL; |
435 | 0 | } |
436 | 2.72k | iohandler -> ReportedSize = (cmsUInt32Number) fileLen; |
437 | 2.72k | break; |
438 | | |
439 | 10.2k | case 'w': |
440 | 10.2k | fm = fopen(FileName, mode); |
441 | 10.2k | if (fm == NULL) { |
442 | 0 | _cmsFree(ContextID, iohandler); |
443 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "Couldn't create '%s'", FileName); |
444 | 0 | return NULL; |
445 | 0 | } |
446 | 10.2k | iohandler -> ReportedSize = 0; |
447 | 10.2k | break; |
448 | | |
449 | 0 | default: |
450 | 0 | _cmsFree(ContextID, iohandler); // Would never reach |
451 | 0 | return NULL; |
452 | 12.9k | } |
453 | | |
454 | 12.9k | iohandler ->ContextID = ContextID; |
455 | 12.9k | iohandler ->stream = (void*) fm; |
456 | 12.9k | iohandler ->UsedSpace = 0; |
457 | | |
458 | | // Keep track of the original file |
459 | 12.9k | strncpy(iohandler -> PhysicalFile, FileName, sizeof(iohandler -> PhysicalFile)-1); |
460 | 12.9k | iohandler -> PhysicalFile[sizeof(iohandler -> PhysicalFile)-1] = 0; |
461 | | |
462 | 12.9k | iohandler ->Read = FileRead; |
463 | 12.9k | iohandler ->Seek = FileSeek; |
464 | 12.9k | iohandler ->Close = FileClose; |
465 | 12.9k | iohandler ->Tell = FileTell; |
466 | 12.9k | iohandler ->Write = FileWrite; |
467 | | |
468 | 12.9k | return iohandler; |
469 | 12.9k | } |
470 | | |
471 | | // Create a iohandler for stream based files |
472 | | cmsIOHANDLER* CMSEXPORT cmsOpenIOhandlerFromStream(cmsContext ContextID, FILE* Stream) |
473 | 0 | { |
474 | 0 | cmsIOHANDLER* iohandler = NULL; |
475 | 0 | cmsInt32Number fileSize; |
476 | |
|
477 | 0 | fileSize = (cmsInt32Number)cmsfilelength(Stream); |
478 | 0 | if (fileSize < 0) |
479 | 0 | { |
480 | 0 | cmsSignalError(ContextID, cmsERROR_FILE, "Cannot get size of stream"); |
481 | 0 | return NULL; |
482 | 0 | } |
483 | | |
484 | 0 | iohandler = (cmsIOHANDLER*) _cmsMallocZero(ContextID, sizeof(cmsIOHANDLER)); |
485 | 0 | if (iohandler == NULL) return NULL; |
486 | | |
487 | 0 | iohandler -> ContextID = ContextID; |
488 | 0 | iohandler -> stream = (void*) Stream; |
489 | 0 | iohandler -> UsedSpace = 0; |
490 | 0 | iohandler -> ReportedSize = (cmsUInt32Number) fileSize; |
491 | 0 | iohandler -> PhysicalFile[0] = 0; |
492 | |
|
493 | 0 | iohandler ->Read = FileRead; |
494 | 0 | iohandler ->Seek = FileSeek; |
495 | 0 | iohandler ->Close = FileClose; |
496 | 0 | iohandler ->Tell = FileTell; |
497 | 0 | iohandler ->Write = FileWrite; |
498 | |
|
499 | 0 | return iohandler; |
500 | 0 | } |
501 | | |
502 | | |
503 | | |
504 | | // Close an open IO handler |
505 | | cmsBool CMSEXPORT cmsCloseIOhandler(cmsIOHANDLER* io) |
506 | 84.7k | { |
507 | 84.7k | return io -> Close(io); |
508 | 84.7k | } |
509 | | |
510 | | // ------------------------------------------------------------------------------------------------------- |
511 | | |
512 | | cmsIOHANDLER* CMSEXPORT cmsGetProfileIOhandler(cmsHPROFILE hProfile) |
513 | 0 | { |
514 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*)hProfile; |
515 | |
|
516 | 0 | if (Icc == NULL) return NULL; |
517 | 0 | return Icc->IOhandler; |
518 | 0 | } |
519 | | |
520 | | // Creates an empty structure holding all required parameters |
521 | | cmsHPROFILE CMSEXPORT cmsCreateProfilePlaceholder(cmsContext ContextID) |
522 | 84.5k | { |
523 | 84.5k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) _cmsMallocZero(ContextID, sizeof(_cmsICCPROFILE)); |
524 | 84.5k | if (Icc == NULL) return NULL; |
525 | | |
526 | 84.5k | Icc ->ContextID = ContextID; |
527 | | |
528 | | // Set it to empty |
529 | 84.5k | Icc -> TagCount = 0; |
530 | | |
531 | | // Set default version |
532 | 84.5k | Icc ->Version = 0x02100000; |
533 | | |
534 | | // Set default CMM (that's me!) |
535 | 84.5k | Icc ->CMM = lcmsSignature; |
536 | | |
537 | | // Set default creator |
538 | | // Created by LittleCMS (that's me!) |
539 | 84.5k | Icc ->creator = lcmsSignature; |
540 | | |
541 | | // Set default platform |
542 | | #ifdef CMS_IS_WINDOWS_ |
543 | | Icc ->platform = cmsSigMicrosoft; |
544 | | #else |
545 | 84.5k | Icc ->platform = cmsSigMacintosh; |
546 | 84.5k | #endif |
547 | | |
548 | | // Set default device class |
549 | 84.5k | Icc->DeviceClass = cmsSigDisplayClass; |
550 | | |
551 | | // Set creation date/time |
552 | 84.5k | if (!_cmsGetTime(&Icc->Created)) |
553 | 0 | goto Error; |
554 | | |
555 | | // Create a mutex if the user provided proper plugin. NULL otherwise |
556 | 84.5k | Icc ->UsrMutex = _cmsCreateMutex(ContextID); |
557 | | |
558 | | // Return the handle |
559 | 84.5k | return (cmsHPROFILE) Icc; |
560 | | |
561 | 0 | Error: |
562 | 0 | _cmsFree(ContextID, Icc); |
563 | 0 | return NULL; |
564 | 84.5k | } |
565 | | |
566 | | cmsContext CMSEXPORT cmsGetProfileContextID(cmsHPROFILE hProfile) |
567 | 168k | { |
568 | 168k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
569 | | |
570 | 168k | if (Icc == NULL) return NULL; |
571 | 166k | return Icc -> ContextID; |
572 | 168k | } |
573 | | |
574 | | |
575 | | // Return the number of tags |
576 | | cmsInt32Number CMSEXPORT cmsGetTagCount(cmsHPROFILE hProfile) |
577 | 2.07k | { |
578 | 2.07k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
579 | 2.07k | if (Icc == NULL) return -1; |
580 | | |
581 | 2.07k | return (cmsInt32Number) Icc->TagCount; |
582 | 2.07k | } |
583 | | |
584 | | // Return the tag signature of a given tag number |
585 | | cmsTagSignature CMSEXPORT cmsGetTagSignature(cmsHPROFILE hProfile, cmsUInt32Number n) |
586 | 2.07k | { |
587 | 2.07k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
588 | | |
589 | 2.07k | if (n > Icc->TagCount) return (cmsTagSignature) 0; // Mark as not available |
590 | 636 | if (n >= MAX_TABLE_TAG) return (cmsTagSignature) 0; // As double check |
591 | | |
592 | 636 | return Icc ->TagNames[n]; |
593 | 636 | } |
594 | | |
595 | | |
596 | | static |
597 | | int SearchOneTag(_cmsICCPROFILE* Profile, cmsTagSignature sig) |
598 | 1.18M | { |
599 | 1.18M | int i; |
600 | | |
601 | 4.27M | for (i=0; i < (int) Profile -> TagCount; i++) { |
602 | | |
603 | 3.58M | if (sig == Profile -> TagNames[i]) |
604 | 491k | return i; |
605 | 3.58M | } |
606 | | |
607 | 695k | return -1; |
608 | 1.18M | } |
609 | | |
610 | | // Search for a specific tag in tag dictionary. Returns position or -1 if tag not found. |
611 | | // If followlinks is turned on, then the position of the linked tag is returned |
612 | | int _cmsSearchTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, cmsBool lFollowLinks) |
613 | 1.17M | { |
614 | 1.17M | int n; |
615 | 1.17M | cmsTagSignature LinkedSig; |
616 | | |
617 | 1.18M | do { |
618 | | |
619 | | // Search for given tag in ICC profile directory |
620 | 1.18M | n = SearchOneTag(Icc, sig); |
621 | 1.18M | if (n < 0) |
622 | 695k | return -1; // Not found |
623 | | |
624 | 491k | if (!lFollowLinks) |
625 | 153k | return n; // Found, don't follow links |
626 | | |
627 | | // Is this a linked tag? |
628 | 338k | LinkedSig = Icc ->TagLinked[n]; |
629 | | |
630 | | // Yes, follow link |
631 | 338k | if (LinkedSig != (cmsTagSignature) 0) { |
632 | 13.3k | sig = LinkedSig; |
633 | 13.3k | } |
634 | | |
635 | 338k | } while (LinkedSig != (cmsTagSignature) 0); |
636 | | |
637 | 324k | return n; |
638 | 1.17M | } |
639 | | |
640 | | // Deletes a tag entry |
641 | | |
642 | | static |
643 | | void _cmsDeleteTagByPos(_cmsICCPROFILE* Icc, int i) |
644 | 71.8k | { |
645 | 71.8k | _cmsAssert(Icc != NULL); |
646 | 71.8k | _cmsAssert(i >= 0); |
647 | | |
648 | | |
649 | 71.8k | if (Icc -> TagPtrs[i] != NULL) { |
650 | | |
651 | | // Free previous version |
652 | 50.0k | if (Icc ->TagSaveAsRaw[i]) { |
653 | 5.44k | _cmsFree(Icc ->ContextID, Icc ->TagPtrs[i]); |
654 | 5.44k | } |
655 | 44.5k | else { |
656 | 44.5k | cmsTagTypeHandler* TypeHandler = Icc ->TagTypeHandlers[i]; |
657 | | |
658 | 44.5k | if (TypeHandler != NULL) { |
659 | | |
660 | 44.5k | cmsTagTypeHandler LocalTypeHandler = *TypeHandler; |
661 | 44.5k | LocalTypeHandler.ContextID = Icc ->ContextID; // As an additional parameter |
662 | 44.5k | LocalTypeHandler.ICCVersion = Icc ->Version; |
663 | 44.5k | LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc -> TagPtrs[i]); |
664 | 44.5k | Icc ->TagPtrs[i] = NULL; |
665 | 44.5k | } |
666 | 44.5k | } |
667 | | |
668 | 50.0k | } |
669 | 71.8k | } |
670 | | |
671 | | |
672 | | // Creates a new tag entry |
673 | | static |
674 | | cmsBool _cmsNewTag(_cmsICCPROFILE* Icc, cmsTagSignature sig, int* NewPos) |
675 | 270k | { |
676 | 270k | int i; |
677 | | |
678 | | // Search for the tag |
679 | 270k | i = _cmsSearchTag(Icc, sig, FALSE); |
680 | 270k | if (i >= 0) { |
681 | | |
682 | | // Already exists? delete it |
683 | 71.8k | _cmsDeleteTagByPos(Icc, i); |
684 | 71.8k | *NewPos = i; |
685 | 71.8k | } |
686 | 198k | else { |
687 | | |
688 | | // No, make a new one |
689 | 198k | if (Icc -> TagCount >= MAX_TABLE_TAG) { |
690 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", MAX_TABLE_TAG); |
691 | 0 | return FALSE; |
692 | 0 | } |
693 | | |
694 | 198k | *NewPos = (int) Icc ->TagCount; |
695 | 198k | Icc -> TagCount++; |
696 | 198k | } |
697 | | |
698 | 270k | return TRUE; |
699 | 270k | } |
700 | | |
701 | | |
702 | | // Check existence |
703 | | cmsBool CMSEXPORT cmsIsTag(cmsHPROFILE hProfile, cmsTagSignature sig) |
704 | 203k | { |
705 | 203k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) (void*) hProfile; |
706 | 203k | return _cmsSearchTag(Icc, sig, FALSE) >= 0; |
707 | 203k | } |
708 | | |
709 | | |
710 | | |
711 | | // Checks for link compatibility |
712 | | static |
713 | | cmsBool CompatibleTypes(const cmsTagDescriptor* desc1, const cmsTagDescriptor* desc2) |
714 | 53.0k | { |
715 | 53.0k | cmsUInt32Number i; |
716 | | |
717 | 53.0k | if (desc1 == NULL || desc2 == NULL) return FALSE; |
718 | | |
719 | 18.0k | if (desc1->nSupportedTypes != desc2->nSupportedTypes) return FALSE; |
720 | 13.0k | if (desc1->ElemCount != desc2->ElemCount) return FALSE; |
721 | | |
722 | 29.0k | for (i = 0; i < desc1->nSupportedTypes; i++) |
723 | 19.9k | { |
724 | 19.9k | if (desc1->SupportedTypes[i] != desc2->SupportedTypes[i]) return FALSE; |
725 | 19.9k | } |
726 | | |
727 | 9.05k | return TRUE; |
728 | 10.4k | } |
729 | | |
730 | | // Enforces that the profile version is per. spec. |
731 | | // Operates on the big endian bytes from the profile. |
732 | | // Called before converting to platform endianness. |
733 | | // Byte 0 is BCD major version, so max 9. |
734 | | // Byte 1 is 2 BCD digits, one per nibble. |
735 | | // Reserved bytes 2 & 3 must be 0. |
736 | | static |
737 | | cmsUInt32Number _validatedVersion(cmsUInt32Number DWord) |
738 | 45.5k | { |
739 | 45.5k | cmsUInt8Number* pByte = (cmsUInt8Number*) &DWord; |
740 | 45.5k | cmsUInt8Number temp1; |
741 | 45.5k | cmsUInt8Number temp2; |
742 | | |
743 | 45.5k | if (*pByte > 0x09) *pByte = (cmsUInt8Number) 0x09; |
744 | 45.5k | temp1 = (cmsUInt8Number) (*(pByte+1) & 0xf0); |
745 | 45.5k | temp2 = (cmsUInt8Number) (*(pByte+1) & 0x0f); |
746 | 45.5k | if (temp1 > 0x90U) temp1 = 0x90U; |
747 | 45.5k | if (temp2 > 0x09U) temp2 = 0x09U; |
748 | 45.5k | *(pByte+1) = (cmsUInt8Number)(temp1 | temp2); |
749 | 45.5k | *(pByte+2) = (cmsUInt8Number)0; |
750 | 45.5k | *(pByte+3) = (cmsUInt8Number)0; |
751 | | |
752 | 45.5k | return DWord; |
753 | 45.5k | } |
754 | | |
755 | | // Check device class |
756 | | static |
757 | | cmsBool validDeviceClass(cmsProfileClassSignature cl) |
758 | 45.4k | { |
759 | 45.4k | if ((int)cl == 0) return TRUE; // We allow zero because older lcms versions defaulted to that. |
760 | | |
761 | 34.9k | switch (cl) |
762 | 34.9k | { |
763 | 1.79k | case cmsSigInputClass: |
764 | 7.73k | case cmsSigDisplayClass: |
765 | 15.4k | case cmsSigOutputClass: |
766 | 21.2k | case cmsSigLinkClass: |
767 | 26.8k | case cmsSigAbstractClass: |
768 | 30.8k | case cmsSigColorSpaceClass: |
769 | 31.5k | case cmsSigNamedColorClass: |
770 | 31.5k | return TRUE; |
771 | | |
772 | 3.44k | default: |
773 | 3.44k | return FALSE; |
774 | 34.9k | } |
775 | | |
776 | 34.9k | } |
777 | | |
778 | | // Read profile header and validate it |
779 | | cmsBool _cmsReadHeader(_cmsICCPROFILE* Icc) |
780 | 57.2k | { |
781 | 57.2k | cmsTagEntry Tag; |
782 | 57.2k | cmsICCHeader Header; |
783 | 57.2k | cmsUInt32Number i, j; |
784 | 57.2k | cmsUInt32Number HeaderSize; |
785 | 57.2k | cmsIOHANDLER* io = Icc ->IOhandler; |
786 | 57.2k | cmsUInt32Number TagCount; |
787 | | |
788 | | |
789 | | // Read the header |
790 | 57.2k | if (io -> Read(io, &Header, sizeof(cmsICCHeader), 1) != 1) { |
791 | 1.56k | return FALSE; |
792 | 1.56k | } |
793 | | |
794 | | // Validate file as an ICC profile |
795 | 55.6k | if (_cmsAdjustEndianess32(Header.magic) != cmsMagicNumber) { |
796 | 10.1k | cmsSignalError(Icc ->ContextID, cmsERROR_BAD_SIGNATURE, "not an ICC profile, invalid signature"); |
797 | 10.1k | return FALSE; |
798 | 10.1k | } |
799 | | |
800 | | // Adjust endianness of the used parameters |
801 | 45.5k | Icc -> CMM = _cmsAdjustEndianess32(Header.cmmId); |
802 | 45.5k | Icc -> DeviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Header.deviceClass); |
803 | 45.5k | Icc -> ColorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.colorSpace); |
804 | 45.5k | Icc -> PCS = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Header.pcs); |
805 | | |
806 | 45.5k | Icc -> RenderingIntent = _cmsAdjustEndianess32(Header.renderingIntent); |
807 | 45.5k | Icc -> platform = (cmsPlatformSignature)_cmsAdjustEndianess32(Header.platform); |
808 | 45.5k | Icc -> flags = _cmsAdjustEndianess32(Header.flags); |
809 | 45.5k | Icc -> manufacturer = _cmsAdjustEndianess32(Header.manufacturer); |
810 | 45.5k | Icc -> model = _cmsAdjustEndianess32(Header.model); |
811 | 45.5k | Icc -> creator = _cmsAdjustEndianess32(Header.creator); |
812 | | |
813 | 45.5k | _cmsAdjustEndianess64(&Icc -> attributes, &Header.attributes); |
814 | 45.5k | Icc -> Version = _cmsAdjustEndianess32(_validatedVersion(Header.version)); |
815 | | |
816 | 45.5k | if (Icc->Version > 0x5000000) { |
817 | 122 | cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported profile version '0x%x'", Icc->Version); |
818 | 122 | return FALSE; |
819 | 122 | } |
820 | | |
821 | 45.4k | if (!validDeviceClass(Icc->DeviceClass)) { |
822 | 3.44k | cmsSignalError(Icc->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported device class '0x%x'", Icc->DeviceClass); |
823 | 3.44k | return FALSE; |
824 | 3.44k | } |
825 | | |
826 | | // Get size as reported in header |
827 | 42.0k | HeaderSize = _cmsAdjustEndianess32(Header.size); |
828 | | |
829 | | // Make sure HeaderSize is lower than profile size |
830 | 42.0k | if (HeaderSize >= Icc ->IOhandler ->ReportedSize) |
831 | 41.2k | HeaderSize = Icc ->IOhandler ->ReportedSize; |
832 | | |
833 | | |
834 | | // Get creation date/time |
835 | 42.0k | _cmsDecodeDateTimeNumber(&Header.date, &Icc ->Created); |
836 | | |
837 | | // The profile ID are 32 raw bytes |
838 | 42.0k | memmove(Icc ->ProfileID.ID32, Header.profileID.ID32, 16); |
839 | | |
840 | | |
841 | | // Read tag directory |
842 | 42.0k | if (!_cmsReadUInt32Number(io, &TagCount)) return FALSE; |
843 | 41.4k | if (TagCount > MAX_TABLE_TAG) { |
844 | | |
845 | 389 | cmsSignalError(Icc ->ContextID, cmsERROR_RANGE, "Too many tags (%d)", TagCount); |
846 | 389 | return FALSE; |
847 | 389 | } |
848 | | |
849 | | |
850 | | // Read tag directory |
851 | 41.1k | Icc -> TagCount = 0; |
852 | 336k | for (i=0; i < TagCount; i++) { |
853 | | |
854 | 297k | if (!_cmsReadUInt32Number(io, (cmsUInt32Number *) &Tag.sig)) return FALSE; |
855 | 295k | if (!_cmsReadUInt32Number(io, &Tag.offset)) return FALSE; |
856 | 295k | if (!_cmsReadUInt32Number(io, &Tag.size)) return FALSE; |
857 | | |
858 | | // Perform some sanity check. Offset + size should fall inside file. |
859 | 295k | if (Tag.size == 0 || Tag.offset == 0) continue; |
860 | 259k | if (Tag.offset + Tag.size > HeaderSize || |
861 | 259k | Tag.offset + Tag.size < Tag.offset) |
862 | 200k | continue; |
863 | | |
864 | 59.4k | Icc -> TagNames[Icc ->TagCount] = Tag.sig; |
865 | 59.4k | Icc -> TagOffsets[Icc ->TagCount] = Tag.offset; |
866 | 59.4k | Icc -> TagSizes[Icc ->TagCount] = Tag.size; |
867 | | |
868 | | // Search for links |
869 | 338k | for (j=0; j < Icc ->TagCount; j++) { |
870 | | |
871 | 278k | if ((Icc ->TagOffsets[j] == Tag.offset) && |
872 | 278k | (Icc ->TagSizes[j] == Tag.size)) { |
873 | | |
874 | | // Check types. |
875 | 53.0k | if (CompatibleTypes(_cmsGetTagDescriptor(Icc->ContextID, Icc->TagNames[j]), |
876 | 53.0k | _cmsGetTagDescriptor(Icc->ContextID, Tag.sig))) { |
877 | | |
878 | 9.05k | Icc->TagLinked[Icc->TagCount] = Icc->TagNames[j]; |
879 | 9.05k | } |
880 | 53.0k | } |
881 | | |
882 | 278k | } |
883 | | |
884 | 59.4k | Icc ->TagCount++; |
885 | 59.4k | } |
886 | | |
887 | | |
888 | 90.7k | for (i = 0; i < Icc->TagCount; i++) { |
889 | 505k | for (j = 0; j < Icc->TagCount; j++) { |
890 | | |
891 | | // Tags cannot be duplicate |
892 | 454k | if ((i != j) && (Icc->TagNames[i] == Icc->TagNames[j])) { |
893 | 167 | cmsSignalError(Icc->ContextID, cmsERROR_RANGE, "Duplicate tag found"); |
894 | 167 | return FALSE; |
895 | 167 | } |
896 | | |
897 | 454k | } |
898 | 51.4k | } |
899 | | |
900 | 39.3k | return TRUE; |
901 | 39.4k | } |
902 | | |
903 | | // Saves profile header |
904 | | cmsBool _cmsWriteHeader(_cmsICCPROFILE* Icc, cmsUInt32Number UsedSpace) |
905 | 15.8k | { |
906 | 15.8k | cmsICCHeader Header; |
907 | 15.8k | cmsUInt32Number i; |
908 | 15.8k | cmsTagEntry Tag; |
909 | 15.8k | cmsUInt32Number Count; |
910 | | |
911 | 15.8k | Header.size = _cmsAdjustEndianess32(UsedSpace); |
912 | 15.8k | Header.cmmId = _cmsAdjustEndianess32(Icc ->CMM); |
913 | 15.8k | Header.version = _cmsAdjustEndianess32(Icc ->Version); |
914 | | |
915 | 15.8k | Header.deviceClass = (cmsProfileClassSignature) _cmsAdjustEndianess32(Icc -> DeviceClass); |
916 | 15.8k | Header.colorSpace = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> ColorSpace); |
917 | 15.8k | Header.pcs = (cmsColorSpaceSignature) _cmsAdjustEndianess32(Icc -> PCS); |
918 | | |
919 | | // NOTE: in v4 Timestamp must be in UTC rather than in local time |
920 | 15.8k | _cmsEncodeDateTimeNumber(&Header.date, &Icc ->Created); |
921 | | |
922 | 15.8k | Header.magic = _cmsAdjustEndianess32(cmsMagicNumber); |
923 | | |
924 | 15.8k | Header.platform = (cmsPlatformSignature) _cmsAdjustEndianess32(Icc -> platform); |
925 | | |
926 | 15.8k | Header.flags = _cmsAdjustEndianess32(Icc -> flags); |
927 | 15.8k | Header.manufacturer = _cmsAdjustEndianess32(Icc -> manufacturer); |
928 | 15.8k | Header.model = _cmsAdjustEndianess32(Icc -> model); |
929 | | |
930 | 15.8k | _cmsAdjustEndianess64(&Header.attributes, &Icc -> attributes); |
931 | | |
932 | | // Rendering intent in the header (for embedded profiles) |
933 | 15.8k | Header.renderingIntent = _cmsAdjustEndianess32(Icc -> RenderingIntent); |
934 | | |
935 | | // Illuminant is always D50 |
936 | 15.8k | Header.illuminant.X = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->X)); |
937 | 15.8k | Header.illuminant.Y = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Y)); |
938 | 15.8k | Header.illuminant.Z = (cmsS15Fixed16Number) _cmsAdjustEndianess32((cmsUInt32Number) _cmsDoubleTo15Fixed16(cmsD50_XYZ()->Z)); |
939 | | |
940 | 15.8k | Header.creator = _cmsAdjustEndianess32(Icc ->creator); |
941 | | |
942 | 15.8k | memset(&Header.reserved, 0, sizeof(Header.reserved)); |
943 | | |
944 | | // Set profile ID. Endianness is always big endian |
945 | 15.8k | memmove(&Header.profileID, &Icc ->ProfileID, 16); |
946 | | |
947 | | // Dump the header |
948 | 15.8k | if (!Icc -> IOhandler->Write(Icc->IOhandler, sizeof(cmsICCHeader), &Header)) return FALSE; |
949 | | |
950 | | // Saves Tag directory |
951 | | |
952 | | // Get true count |
953 | 15.8k | Count = 0; |
954 | 125k | for (i=0; i < Icc -> TagCount; i++) { |
955 | 109k | if (Icc ->TagNames[i] != (cmsTagSignature) 0) |
956 | 109k | Count++; |
957 | 109k | } |
958 | | |
959 | | // Store number of tags |
960 | 15.8k | if (!_cmsWriteUInt32Number(Icc ->IOhandler, Count)) return FALSE; |
961 | | |
962 | 125k | for (i=0; i < Icc -> TagCount; i++) { |
963 | | |
964 | 109k | if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue; // It is just a placeholder |
965 | | |
966 | 109k | Tag.sig = (cmsTagSignature) _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagNames[i]); |
967 | 109k | Tag.offset = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagOffsets[i]); |
968 | 109k | Tag.size = _cmsAdjustEndianess32((cmsUInt32Number) Icc -> TagSizes[i]); |
969 | | |
970 | 109k | if (!Icc ->IOhandler -> Write(Icc-> IOhandler, sizeof(cmsTagEntry), &Tag)) return FALSE; |
971 | 109k | } |
972 | | |
973 | 15.8k | return TRUE; |
974 | 15.8k | } |
975 | | |
976 | | // ----------------------------------------------------------------------- Set/Get several struct members |
977 | | |
978 | | |
979 | | cmsUInt32Number CMSEXPORT cmsGetHeaderRenderingIntent(cmsHPROFILE hProfile) |
980 | 0 | { |
981 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
982 | 0 | return Icc -> RenderingIntent; |
983 | 0 | } |
984 | | |
985 | | void CMSEXPORT cmsSetHeaderRenderingIntent(cmsHPROFILE hProfile, cmsUInt32Number RenderingIntent) |
986 | 24.2k | { |
987 | 24.2k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
988 | 24.2k | Icc -> RenderingIntent = RenderingIntent; |
989 | 24.2k | } |
990 | | |
991 | | cmsUInt32Number CMSEXPORT cmsGetHeaderFlags(cmsHPROFILE hProfile) |
992 | 0 | { |
993 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
994 | 0 | return (cmsUInt32Number) Icc -> flags; |
995 | 0 | } |
996 | | |
997 | | void CMSEXPORT cmsSetHeaderFlags(cmsHPROFILE hProfile, cmsUInt32Number Flags) |
998 | 0 | { |
999 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1000 | 0 | Icc -> flags = (cmsUInt32Number) Flags; |
1001 | 0 | } |
1002 | | |
1003 | | cmsUInt32Number CMSEXPORT cmsGetHeaderManufacturer(cmsHPROFILE hProfile) |
1004 | 1.23k | { |
1005 | 1.23k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1006 | 1.23k | return Icc ->manufacturer; |
1007 | 1.23k | } |
1008 | | |
1009 | | void CMSEXPORT cmsSetHeaderManufacturer(cmsHPROFILE hProfile, cmsUInt32Number manufacturer) |
1010 | 0 | { |
1011 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1012 | 0 | Icc -> manufacturer = manufacturer; |
1013 | 0 | } |
1014 | | |
1015 | | cmsUInt32Number CMSEXPORT cmsGetHeaderCreator(cmsHPROFILE hProfile) |
1016 | 0 | { |
1017 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1018 | 0 | return Icc ->creator; |
1019 | 0 | } |
1020 | | |
1021 | | cmsUInt32Number CMSEXPORT cmsGetHeaderModel(cmsHPROFILE hProfile) |
1022 | 1.23k | { |
1023 | 1.23k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1024 | 1.23k | return Icc ->model; |
1025 | 1.23k | } |
1026 | | |
1027 | | void CMSEXPORT cmsSetHeaderModel(cmsHPROFILE hProfile, cmsUInt32Number model) |
1028 | 0 | { |
1029 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1030 | 0 | Icc -> model = model; |
1031 | 0 | } |
1032 | | |
1033 | | void CMSEXPORT cmsGetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number* Flags) |
1034 | 1.23k | { |
1035 | 1.23k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1036 | 1.23k | memmove(Flags, &Icc -> attributes, sizeof(cmsUInt64Number)); |
1037 | 1.23k | } |
1038 | | |
1039 | | void CMSEXPORT cmsSetHeaderAttributes(cmsHPROFILE hProfile, cmsUInt64Number Flags) |
1040 | 0 | { |
1041 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1042 | 0 | memmove(&Icc -> attributes, &Flags, sizeof(cmsUInt64Number)); |
1043 | 0 | } |
1044 | | |
1045 | | void CMSEXPORT cmsGetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID) |
1046 | 1.23k | { |
1047 | 1.23k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1048 | 1.23k | memmove(ProfileID, Icc ->ProfileID.ID8, 16); |
1049 | 1.23k | } |
1050 | | |
1051 | | void CMSEXPORT cmsSetHeaderProfileID(cmsHPROFILE hProfile, cmsUInt8Number* ProfileID) |
1052 | 0 | { |
1053 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1054 | 0 | memmove(&Icc -> ProfileID, ProfileID, 16); |
1055 | 0 | } |
1056 | | |
1057 | | cmsBool CMSEXPORT cmsGetHeaderCreationDateTime(cmsHPROFILE hProfile, struct tm *Dest) |
1058 | 0 | { |
1059 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1060 | 0 | memmove(Dest, &Icc ->Created, sizeof(struct tm)); |
1061 | 0 | return TRUE; |
1062 | 0 | } |
1063 | | |
1064 | | cmsColorSpaceSignature CMSEXPORT cmsGetPCS(cmsHPROFILE hProfile) |
1065 | 163k | { |
1066 | 163k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1067 | 163k | return Icc -> PCS; |
1068 | 163k | } |
1069 | | |
1070 | | void CMSEXPORT cmsSetPCS(cmsHPROFILE hProfile, cmsColorSpaceSignature pcs) |
1071 | 37.4k | { |
1072 | 37.4k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1073 | 37.4k | Icc -> PCS = pcs; |
1074 | 37.4k | } |
1075 | | |
1076 | | cmsColorSpaceSignature CMSEXPORT cmsGetColorSpace(cmsHPROFILE hProfile) |
1077 | 268k | { |
1078 | 268k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1079 | 268k | return Icc -> ColorSpace; |
1080 | 268k | } |
1081 | | |
1082 | | void CMSEXPORT cmsSetColorSpace(cmsHPROFILE hProfile, cmsColorSpaceSignature sig) |
1083 | 37.4k | { |
1084 | 37.4k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1085 | 37.4k | Icc -> ColorSpace = sig; |
1086 | 37.4k | } |
1087 | | |
1088 | | cmsProfileClassSignature CMSEXPORT cmsGetDeviceClass(cmsHPROFILE hProfile) |
1089 | 207k | { |
1090 | 207k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1091 | 207k | return Icc -> DeviceClass; |
1092 | 207k | } |
1093 | | |
1094 | | void CMSEXPORT cmsSetDeviceClass(cmsHPROFILE hProfile, cmsProfileClassSignature sig) |
1095 | 37.4k | { |
1096 | 37.4k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1097 | 37.4k | Icc -> DeviceClass = sig; |
1098 | 37.4k | } |
1099 | | |
1100 | | cmsUInt32Number CMSEXPORT cmsGetEncodedICCversion(cmsHPROFILE hProfile) |
1101 | 38.9k | { |
1102 | 38.9k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1103 | 38.9k | return Icc -> Version; |
1104 | 38.9k | } |
1105 | | |
1106 | | void CMSEXPORT cmsSetEncodedICCversion(cmsHPROFILE hProfile, cmsUInt32Number Version) |
1107 | 0 | { |
1108 | 0 | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1109 | 0 | Icc -> Version = Version; |
1110 | 0 | } |
1111 | | |
1112 | | // Get an hexadecimal number with same digits as v |
1113 | | static |
1114 | | cmsUInt32Number BaseToBase(cmsUInt32Number in, int BaseIn, int BaseOut) |
1115 | 255k | { |
1116 | 255k | char Buff[100]; |
1117 | 255k | int i, len; |
1118 | 255k | cmsUInt32Number out; |
1119 | | |
1120 | 1.01M | for (len=0; in > 0 && len < 100; len++) { |
1121 | | |
1122 | 761k | Buff[len] = (char) (in % BaseIn); |
1123 | 761k | in /= BaseIn; |
1124 | 761k | } |
1125 | | |
1126 | 1.01M | for (i=len-1, out=0; i >= 0; --i) { |
1127 | 761k | out = out * BaseOut + Buff[i]; |
1128 | 761k | } |
1129 | | |
1130 | 255k | return out; |
1131 | 255k | } |
1132 | | |
1133 | | void CMSEXPORT cmsSetProfileVersion(cmsHPROFILE hProfile, cmsFloat64Number Version) |
1134 | 37.1k | { |
1135 | 37.1k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1136 | | |
1137 | | // 4.2 -> 0x4200000 |
1138 | | |
1139 | 37.1k | Icc -> Version = BaseToBase((cmsUInt32Number) floor(Version * 100.0 + 0.5), 10, 16) << 16; |
1140 | 37.1k | } |
1141 | | |
1142 | | cmsFloat64Number CMSEXPORT cmsGetProfileVersion(cmsHPROFILE hProfile) |
1143 | 218k | { |
1144 | 218k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1145 | 218k | cmsUInt32Number n = Icc -> Version >> 16; |
1146 | | |
1147 | 218k | return BaseToBase(n, 16, 10) / 100.0; |
1148 | 218k | } |
1149 | | // -------------------------------------------------------------------------------------------------------------- |
1150 | | |
1151 | | |
1152 | | // Create profile from IOhandler |
1153 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandlerTHR(cmsContext ContextID, cmsIOHANDLER* io) |
1154 | 0 | { |
1155 | 0 | _cmsICCPROFILE* NewIcc; |
1156 | 0 | cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID); |
1157 | |
|
1158 | 0 | if (hEmpty == NULL) return NULL; |
1159 | | |
1160 | 0 | NewIcc = (_cmsICCPROFILE*) hEmpty; |
1161 | |
|
1162 | 0 | NewIcc ->IOhandler = io; |
1163 | 0 | if (!_cmsReadHeader(NewIcc)) goto Error; |
1164 | 0 | return hEmpty; |
1165 | | |
1166 | 0 | Error: |
1167 | 0 | cmsCloseProfile(hEmpty); |
1168 | 0 | return NULL; |
1169 | 0 | } |
1170 | | |
1171 | | // Create profile from IOhandler |
1172 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromIOhandler2THR(cmsContext ContextID, cmsIOHANDLER* io, cmsBool write) |
1173 | 0 | { |
1174 | 0 | _cmsICCPROFILE* NewIcc; |
1175 | 0 | cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID); |
1176 | |
|
1177 | 0 | if (hEmpty == NULL) return NULL; |
1178 | | |
1179 | 0 | NewIcc = (_cmsICCPROFILE*) hEmpty; |
1180 | |
|
1181 | 0 | NewIcc ->IOhandler = io; |
1182 | 0 | if (write) { |
1183 | |
|
1184 | 0 | NewIcc -> IsWrite = TRUE; |
1185 | 0 | return hEmpty; |
1186 | 0 | } |
1187 | | |
1188 | 0 | if (!_cmsReadHeader(NewIcc)) goto Error; |
1189 | 0 | return hEmpty; |
1190 | | |
1191 | 0 | Error: |
1192 | 0 | cmsCloseProfile(hEmpty); |
1193 | 0 | return NULL; |
1194 | 0 | } |
1195 | | |
1196 | | |
1197 | | // Create profile from disk file |
1198 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromFileTHR(cmsContext ContextID, const char *lpFileName, const char *sAccess) |
1199 | 5.44k | { |
1200 | 5.44k | _cmsICCPROFILE* NewIcc; |
1201 | 5.44k | cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID); |
1202 | | |
1203 | 5.44k | if (hEmpty == NULL) return NULL; |
1204 | | |
1205 | 5.44k | NewIcc = (_cmsICCPROFILE*) hEmpty; |
1206 | | |
1207 | 5.44k | NewIcc ->IOhandler = cmsOpenIOhandlerFromFile(ContextID, lpFileName, sAccess); |
1208 | 5.44k | if (NewIcc ->IOhandler == NULL) goto Error; |
1209 | | |
1210 | 5.44k | if (*sAccess == 'W' || *sAccess == 'w') { |
1211 | | |
1212 | 2.72k | NewIcc -> IsWrite = TRUE; |
1213 | | |
1214 | 2.72k | return hEmpty; |
1215 | 2.72k | } |
1216 | | |
1217 | 2.72k | if (!_cmsReadHeader(NewIcc)) goto Error; |
1218 | 2.07k | return hEmpty; |
1219 | | |
1220 | 654 | Error: |
1221 | 654 | cmsCloseProfile(hEmpty); |
1222 | 654 | return NULL; |
1223 | 2.72k | } |
1224 | | |
1225 | | |
1226 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromFile(const char *ICCProfile, const char *sAccess) |
1227 | 5.44k | { |
1228 | 5.44k | return cmsOpenProfileFromFileTHR(NULL, ICCProfile, sAccess); |
1229 | 5.44k | } |
1230 | | |
1231 | | |
1232 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromStreamTHR(cmsContext ContextID, FILE* ICCProfile, const char *sAccess) |
1233 | 0 | { |
1234 | 0 | _cmsICCPROFILE* NewIcc; |
1235 | 0 | cmsHPROFILE hEmpty = cmsCreateProfilePlaceholder(ContextID); |
1236 | |
|
1237 | 0 | if (hEmpty == NULL) return NULL; |
1238 | | |
1239 | 0 | NewIcc = (_cmsICCPROFILE*) hEmpty; |
1240 | |
|
1241 | 0 | NewIcc ->IOhandler = cmsOpenIOhandlerFromStream(ContextID, ICCProfile); |
1242 | 0 | if (NewIcc ->IOhandler == NULL) goto Error; |
1243 | | |
1244 | 0 | if (*sAccess == 'w') { |
1245 | |
|
1246 | 0 | NewIcc -> IsWrite = TRUE; |
1247 | 0 | return hEmpty; |
1248 | 0 | } |
1249 | | |
1250 | 0 | if (!_cmsReadHeader(NewIcc)) goto Error; |
1251 | 0 | return hEmpty; |
1252 | | |
1253 | 0 | Error: |
1254 | 0 | cmsCloseProfile(hEmpty); |
1255 | 0 | return NULL; |
1256 | |
|
1257 | 0 | } |
1258 | | |
1259 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromStream(FILE* ICCProfile, const char *sAccess) |
1260 | 0 | { |
1261 | 0 | return cmsOpenProfileFromStreamTHR(NULL, ICCProfile, sAccess); |
1262 | 0 | } |
1263 | | |
1264 | | |
1265 | | // Open from memory block |
1266 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromMemTHR(cmsContext ContextID, const void* MemPtr, cmsUInt32Number dwSize) |
1267 | 54.5k | { |
1268 | 54.5k | _cmsICCPROFILE* NewIcc; |
1269 | 54.5k | cmsHPROFILE hEmpty; |
1270 | | |
1271 | 54.5k | hEmpty = cmsCreateProfilePlaceholder(ContextID); |
1272 | 54.5k | if (hEmpty == NULL) return NULL; |
1273 | | |
1274 | 54.5k | NewIcc = (_cmsICCPROFILE*) hEmpty; |
1275 | | |
1276 | | // Ok, in this case const void* is casted to void* just because open IO handler |
1277 | | // shares read and writing modes. Don't abuse this feature! |
1278 | 54.5k | NewIcc ->IOhandler = cmsOpenIOhandlerFromMem(ContextID, (void*) MemPtr, dwSize, "r"); |
1279 | 54.5k | if (NewIcc ->IOhandler == NULL) goto Error; |
1280 | | |
1281 | 54.5k | if (!_cmsReadHeader(NewIcc)) goto Error; |
1282 | | |
1283 | 37.2k | return hEmpty; |
1284 | | |
1285 | 17.2k | Error: |
1286 | 17.2k | cmsCloseProfile(hEmpty); |
1287 | 17.2k | return NULL; |
1288 | 54.5k | } |
1289 | | |
1290 | | cmsHPROFILE CMSEXPORT cmsOpenProfileFromMem(const void* MemPtr, cmsUInt32Number dwSize) |
1291 | 54.5k | { |
1292 | 54.5k | return cmsOpenProfileFromMemTHR(NULL, MemPtr, dwSize); |
1293 | 54.5k | } |
1294 | | |
1295 | | |
1296 | | |
1297 | | // Dump tag contents. If the profile is being modified, untouched tags are copied from FileOrig |
1298 | | static |
1299 | | cmsBool SaveTags(_cmsICCPROFILE* Icc, _cmsICCPROFILE* FileOrig) |
1300 | 15.8k | { |
1301 | 15.8k | cmsUInt8Number* Data; |
1302 | 15.8k | cmsUInt32Number i; |
1303 | 15.8k | cmsUInt32Number Begin; |
1304 | 15.8k | cmsIOHANDLER* io = Icc ->IOhandler; |
1305 | 15.8k | cmsTagDescriptor* TagDescriptor; |
1306 | 15.8k | cmsTagTypeSignature TypeBase; |
1307 | 15.8k | cmsTagTypeSignature Type; |
1308 | 15.8k | cmsTagTypeHandler* TypeHandler; |
1309 | 15.8k | cmsFloat64Number Version = cmsGetProfileVersion((cmsHPROFILE) Icc); |
1310 | 15.8k | cmsTagTypeHandler LocalTypeHandler; |
1311 | | |
1312 | 125k | for (i=0; i < Icc -> TagCount; i++) { |
1313 | | |
1314 | 109k | if (Icc ->TagNames[i] == (cmsTagSignature) 0) continue; |
1315 | | |
1316 | | // Linked tags are not written |
1317 | 109k | if (Icc ->TagLinked[i] != (cmsTagSignature) 0) continue; |
1318 | | |
1319 | 108k | Icc -> TagOffsets[i] = Begin = io ->UsedSpace; |
1320 | | |
1321 | 108k | Data = (cmsUInt8Number*) Icc -> TagPtrs[i]; |
1322 | | |
1323 | 108k | if (!Data) { |
1324 | | |
1325 | | // Reach here if we are copying a tag from a disk-based ICC profile which has not been modified by user. |
1326 | | // In this case a blind copy of the block data is performed |
1327 | 20.5k | if (FileOrig != NULL && Icc -> TagOffsets[i]) { |
1328 | | |
1329 | 20.5k | if (FileOrig->IOhandler != NULL) |
1330 | 20.5k | { |
1331 | 20.5k | cmsUInt32Number TagSize = FileOrig->TagSizes[i]; |
1332 | 20.5k | cmsUInt32Number TagOffset = FileOrig->TagOffsets[i]; |
1333 | 20.5k | void* Mem; |
1334 | | |
1335 | 20.5k | if (!FileOrig->IOhandler->Seek(FileOrig->IOhandler, TagOffset)) return FALSE; |
1336 | | |
1337 | 20.5k | Mem = _cmsMalloc(Icc->ContextID, TagSize); |
1338 | 20.5k | if (Mem == NULL) return FALSE; |
1339 | | |
1340 | 20.5k | if (FileOrig->IOhandler->Read(FileOrig->IOhandler, Mem, TagSize, 1) != 1) return FALSE; |
1341 | 20.5k | if (!io->Write(io, TagSize, Mem)) return FALSE; |
1342 | 20.5k | _cmsFree(Icc->ContextID, Mem); |
1343 | | |
1344 | 20.5k | Icc->TagSizes[i] = (io->UsedSpace - Begin); |
1345 | | |
1346 | | |
1347 | | // Align to 32 bit boundary. |
1348 | 20.5k | if (!_cmsWriteAlignment(io)) |
1349 | 0 | return FALSE; |
1350 | 20.5k | } |
1351 | 20.5k | } |
1352 | | |
1353 | 20.5k | continue; |
1354 | 20.5k | } |
1355 | | |
1356 | | |
1357 | | // Should this tag be saved as RAW? If so, tagsizes should be specified in advance (no further cooking is done) |
1358 | 88.3k | if (Icc ->TagSaveAsRaw[i]) { |
1359 | | |
1360 | 87.1k | if (io -> Write(io, Icc ->TagSizes[i], Data) != 1) return FALSE; |
1361 | 87.1k | } |
1362 | 1.16k | else { |
1363 | | |
1364 | | // Search for support on this tag |
1365 | 1.16k | TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, Icc -> TagNames[i]); |
1366 | 1.16k | if (TagDescriptor == NULL) continue; // Unsupported, ignore it |
1367 | | |
1368 | 1.16k | if (TagDescriptor ->DecideType != NULL) { |
1369 | | |
1370 | 1.09k | Type = TagDescriptor ->DecideType(Version, Data); |
1371 | 1.09k | } |
1372 | 77 | else { |
1373 | | |
1374 | 77 | Type = TagDescriptor ->SupportedTypes[0]; |
1375 | 77 | } |
1376 | | |
1377 | 1.16k | TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type); |
1378 | | |
1379 | 1.16k | if (TypeHandler == NULL) { |
1380 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_INTERNAL, "(Internal) no handler for tag %x", Icc -> TagNames[i]); |
1381 | 0 | continue; |
1382 | 0 | } |
1383 | | |
1384 | 1.16k | TypeBase = TypeHandler ->Signature; |
1385 | 1.16k | if (!_cmsWriteTypeBase(io, TypeBase)) |
1386 | 0 | return FALSE; |
1387 | | |
1388 | 1.16k | LocalTypeHandler = *TypeHandler; |
1389 | 1.16k | LocalTypeHandler.ContextID = Icc ->ContextID; |
1390 | 1.16k | LocalTypeHandler.ICCVersion = Icc ->Version; |
1391 | 1.16k | if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, io, Data, TagDescriptor ->ElemCount)) { |
1392 | | |
1393 | 61 | char String[5]; |
1394 | | |
1395 | 61 | _cmsTagSignature2String(String, (cmsTagSignature) TypeBase); |
1396 | 61 | cmsSignalError(Icc ->ContextID, cmsERROR_WRITE, "Couldn't write type '%s'", String); |
1397 | 61 | return FALSE; |
1398 | 61 | } |
1399 | 1.16k | } |
1400 | | |
1401 | | |
1402 | 88.2k | Icc -> TagSizes[i] = (io ->UsedSpace - Begin); |
1403 | | |
1404 | | // Align to 32 bit boundary. |
1405 | 88.2k | if (! _cmsWriteAlignment(io)) |
1406 | 0 | return FALSE; |
1407 | 88.2k | } |
1408 | | |
1409 | | |
1410 | 15.8k | return TRUE; |
1411 | 15.8k | } |
1412 | | |
1413 | | |
1414 | | // Fill the offset and size fields for all linked tags |
1415 | | static |
1416 | | cmsBool SetLinks( _cmsICCPROFILE* Icc) |
1417 | 7.76k | { |
1418 | 7.76k | cmsUInt32Number i; |
1419 | | |
1420 | 60.3k | for (i=0; i < Icc -> TagCount; i++) { |
1421 | | |
1422 | 52.6k | cmsTagSignature lnk = Icc ->TagLinked[i]; |
1423 | 52.6k | if (lnk != (cmsTagSignature) 0) { |
1424 | | |
1425 | 237 | int j = _cmsSearchTag(Icc, lnk, FALSE); |
1426 | 237 | if (j >= 0) { |
1427 | | |
1428 | 237 | Icc ->TagOffsets[i] = Icc ->TagOffsets[j]; |
1429 | 237 | Icc ->TagSizes[i] = Icc ->TagSizes[j]; |
1430 | 237 | } |
1431 | | |
1432 | 237 | } |
1433 | 52.6k | } |
1434 | | |
1435 | 7.76k | return TRUE; |
1436 | 7.76k | } |
1437 | | |
1438 | | // Low-level save to IOHANDLER. It returns the number of bytes used to |
1439 | | // store the profile, or zero on error. io may be NULL and in this case |
1440 | | // no data is written--only sizes are calculated |
1441 | | cmsUInt32Number CMSEXPORT cmsSaveProfileToIOhandler(cmsHPROFILE hProfile, cmsIOHANDLER* io) |
1442 | 8.13k | { |
1443 | 8.13k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1444 | 8.13k | _cmsICCPROFILE Keep; |
1445 | 8.13k | cmsIOHANDLER* PrevIO = NULL; |
1446 | 8.13k | cmsUInt32Number UsedSpace; |
1447 | 8.13k | cmsContext ContextID; |
1448 | | |
1449 | 8.13k | _cmsAssert(hProfile != NULL); |
1450 | | |
1451 | 8.13k | if (!_cmsLockMutex(Icc->ContextID, Icc->UsrMutex)) return 0; |
1452 | 8.13k | memmove(&Keep, Icc, sizeof(_cmsICCPROFILE)); |
1453 | | |
1454 | 8.13k | ContextID = cmsGetProfileContextID(hProfile); |
1455 | 8.13k | PrevIO = Icc ->IOhandler = cmsOpenIOhandlerFromNULL(ContextID); |
1456 | 8.13k | if (PrevIO == NULL) { |
1457 | 0 | _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex); |
1458 | 0 | return 0; |
1459 | 0 | } |
1460 | | |
1461 | | // Pass #1 does compute offsets |
1462 | | |
1463 | 8.13k | if (!_cmsWriteHeader(Icc, 0)) goto Error; |
1464 | 8.13k | if (!SaveTags(Icc, &Keep)) goto Error; |
1465 | | |
1466 | 8.07k | UsedSpace = PrevIO ->UsedSpace; |
1467 | | |
1468 | | // Pass #2 does save to iohandler |
1469 | | |
1470 | 8.07k | if (io != NULL) { |
1471 | | |
1472 | 7.76k | Icc ->IOhandler = io; |
1473 | 7.76k | if (!SetLinks(Icc)) goto Error; |
1474 | 7.76k | if (!_cmsWriteHeader(Icc, UsedSpace)) goto Error; |
1475 | 7.76k | if (!SaveTags(Icc, &Keep)) goto Error; |
1476 | 7.76k | } |
1477 | | |
1478 | 8.07k | memmove(Icc, &Keep, sizeof(_cmsICCPROFILE)); |
1479 | 8.07k | if (!cmsCloseIOhandler(PrevIO)) |
1480 | 0 | UsedSpace = 0; // As a error marker |
1481 | | |
1482 | 8.07k | _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex); |
1483 | | |
1484 | 8.07k | return UsedSpace; |
1485 | | |
1486 | | |
1487 | 61 | Error: |
1488 | 61 | cmsCloseIOhandler(PrevIO); |
1489 | 61 | memmove(Icc, &Keep, sizeof(_cmsICCPROFILE)); |
1490 | 61 | _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex); |
1491 | | |
1492 | 61 | return 0; |
1493 | 8.07k | } |
1494 | | |
1495 | | |
1496 | | // Low-level save to disk. |
1497 | | cmsBool CMSEXPORT cmsSaveProfileToFile(cmsHPROFILE hProfile, const char* FileName) |
1498 | 7.51k | { |
1499 | 7.51k | cmsContext ContextID = cmsGetProfileContextID(hProfile); |
1500 | 7.51k | cmsIOHANDLER* io = cmsOpenIOhandlerFromFile(ContextID, FileName, "w"); |
1501 | 7.51k | cmsBool rc; |
1502 | | |
1503 | 7.51k | if (io == NULL) return FALSE; |
1504 | | |
1505 | 7.51k | rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0); |
1506 | 7.51k | rc &= cmsCloseIOhandler(io); |
1507 | | |
1508 | 7.51k | if (rc == FALSE) { // remove() is C99 per 7.19.4.1 |
1509 | 61 | remove(FileName); // We have to IGNORE return value in this case |
1510 | 61 | } |
1511 | 7.51k | return rc; |
1512 | 7.51k | } |
1513 | | |
1514 | | // Same as anterior, but for streams |
1515 | | cmsBool CMSEXPORT cmsSaveProfileToStream(cmsHPROFILE hProfile, FILE* Stream) |
1516 | 0 | { |
1517 | 0 | cmsBool rc; |
1518 | 0 | cmsContext ContextID = cmsGetProfileContextID(hProfile); |
1519 | 0 | cmsIOHANDLER* io = cmsOpenIOhandlerFromStream(ContextID, Stream); |
1520 | |
|
1521 | 0 | if (io == NULL) return FALSE; |
1522 | | |
1523 | 0 | rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0); |
1524 | 0 | rc &= cmsCloseIOhandler(io); |
1525 | |
|
1526 | 0 | return rc; |
1527 | 0 | } |
1528 | | |
1529 | | |
1530 | | // Same as anterior, but for memory blocks. In this case, a NULL as MemPtr means calculate needed space only |
1531 | | cmsBool CMSEXPORT cmsSaveProfileToMem(cmsHPROFILE hProfile, void *MemPtr, cmsUInt32Number* BytesNeeded) |
1532 | 614 | { |
1533 | 614 | cmsBool rc; |
1534 | 614 | cmsIOHANDLER* io; |
1535 | 614 | cmsContext ContextID = cmsGetProfileContextID(hProfile); |
1536 | | |
1537 | 614 | _cmsAssert(BytesNeeded != NULL); |
1538 | | |
1539 | | // Should we just calculate the needed space? |
1540 | 614 | if (MemPtr == NULL) { |
1541 | | |
1542 | 307 | *BytesNeeded = cmsSaveProfileToIOhandler(hProfile, NULL); |
1543 | 307 | return (*BytesNeeded == 0) ? FALSE : TRUE; |
1544 | 307 | } |
1545 | | |
1546 | | // That is a real write operation |
1547 | 307 | io = cmsOpenIOhandlerFromMem(ContextID, MemPtr, *BytesNeeded, "w"); |
1548 | 307 | if (io == NULL) return FALSE; |
1549 | | |
1550 | 307 | rc = (cmsSaveProfileToIOhandler(hProfile, io) != 0); |
1551 | 307 | rc &= cmsCloseIOhandler(io); |
1552 | | |
1553 | 307 | return rc; |
1554 | 307 | } |
1555 | | |
1556 | | // Free one tag contents |
1557 | | static |
1558 | | void freeOneTag(_cmsICCPROFILE* Icc, cmsUInt32Number i) |
1559 | 322k | { |
1560 | 322k | if (Icc->TagPtrs[i]) { |
1561 | | |
1562 | 219k | cmsTagTypeHandler* TypeHandler = Icc->TagTypeHandlers[i]; |
1563 | | |
1564 | 219k | if (TypeHandler != NULL) { |
1565 | 175k | cmsTagTypeHandler LocalTypeHandler = *TypeHandler; |
1566 | | |
1567 | 175k | LocalTypeHandler.ContextID = Icc->ContextID; |
1568 | 175k | LocalTypeHandler.ICCVersion = Icc->Version; |
1569 | 175k | LocalTypeHandler.FreePtr(&LocalTypeHandler, Icc->TagPtrs[i]); |
1570 | 175k | } |
1571 | 43.5k | else |
1572 | 43.5k | _cmsFree(Icc->ContextID, Icc->TagPtrs[i]); |
1573 | 219k | } |
1574 | 322k | } |
1575 | | |
1576 | | // Closes a profile freeing any involved resources |
1577 | | cmsBool CMSEXPORT cmsCloseProfile(cmsHPROFILE hProfile) |
1578 | 98.1k | { |
1579 | 98.1k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1580 | 98.1k | cmsBool rc = TRUE; |
1581 | 98.1k | cmsUInt32Number i; |
1582 | | |
1583 | 98.1k | if (!Icc) return FALSE; |
1584 | | |
1585 | | // Was open in write mode? |
1586 | 84.5k | if (Icc ->IsWrite) { |
1587 | | |
1588 | 2.72k | Icc ->IsWrite = FALSE; // Assure no further writing |
1589 | 2.72k | rc &= cmsSaveProfileToFile(hProfile, Icc ->IOhandler->PhysicalFile); |
1590 | 2.72k | } |
1591 | | |
1592 | 342k | for (i=0; i < Icc -> TagCount; i++) { |
1593 | | |
1594 | 257k | freeOneTag(Icc, i); |
1595 | 257k | } |
1596 | | |
1597 | 84.5k | if (Icc ->IOhandler != NULL) { |
1598 | 59.9k | rc &= cmsCloseIOhandler(Icc->IOhandler); |
1599 | 59.9k | } |
1600 | | |
1601 | 84.5k | _cmsDestroyMutex(Icc->ContextID, Icc->UsrMutex); |
1602 | | |
1603 | 84.5k | _cmsFree(Icc ->ContextID, Icc); // Free placeholder memory |
1604 | | |
1605 | 84.5k | return rc; |
1606 | 98.1k | } |
1607 | | |
1608 | | |
1609 | | // ------------------------------------------------------------------------------------------------------------------- |
1610 | | |
1611 | | |
1612 | | // Returns TRUE if a given tag is supported by a plug-in |
1613 | | static |
1614 | | cmsBool IsTypeSupported(cmsTagDescriptor* TagDescriptor, cmsTagTypeSignature Type) |
1615 | 296k | { |
1616 | 296k | cmsUInt32Number i, nMaxTypes; |
1617 | | |
1618 | 296k | nMaxTypes = TagDescriptor->nSupportedTypes; |
1619 | 296k | if (nMaxTypes >= MAX_TYPES_IN_LCMS_PLUGIN) |
1620 | 0 | nMaxTypes = MAX_TYPES_IN_LCMS_PLUGIN; |
1621 | | |
1622 | 440k | for (i=0; i < nMaxTypes; i++) { |
1623 | 438k | if (Type == TagDescriptor ->SupportedTypes[i]) return TRUE; |
1624 | 438k | } |
1625 | | |
1626 | 1.75k | return FALSE; |
1627 | 296k | } |
1628 | | |
1629 | | |
1630 | | // That's the main read function |
1631 | | void* CMSEXPORT cmsReadTag(cmsHPROFILE hProfile, cmsTagSignature sig) |
1632 | 265k | { |
1633 | 265k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1634 | 265k | cmsIOHANDLER* io; |
1635 | 265k | cmsTagTypeHandler* TypeHandler; |
1636 | 265k | cmsTagTypeHandler LocalTypeHandler; |
1637 | 265k | cmsTagDescriptor* TagDescriptor; |
1638 | 265k | cmsTagTypeSignature BaseType; |
1639 | 265k | cmsUInt32Number Offset, TagSize; |
1640 | 265k | cmsUInt32Number ElemCount; |
1641 | 265k | int n; |
1642 | | |
1643 | 265k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return NULL; |
1644 | | |
1645 | 265k | n = _cmsSearchTag(Icc, sig, TRUE); |
1646 | 265k | if (n < 0) |
1647 | 122k | { |
1648 | | // Not found, return NULL |
1649 | 122k | _cmsUnlockMutex(Icc->ContextID, Icc->UsrMutex); |
1650 | 122k | return NULL; |
1651 | 122k | } |
1652 | | |
1653 | | // If the element is already in memory, return the pointer |
1654 | 143k | if (Icc -> TagPtrs[n]) { |
1655 | | |
1656 | 83.1k | if (Icc->TagTypeHandlers[n] == NULL) goto Error; |
1657 | | |
1658 | | // Sanity check |
1659 | 61.3k | BaseType = Icc->TagTypeHandlers[n]->Signature; |
1660 | 61.3k | if (BaseType == 0) goto Error; |
1661 | | |
1662 | 61.3k | TagDescriptor = _cmsGetTagDescriptor(Icc->ContextID, sig); |
1663 | 61.3k | if (TagDescriptor == NULL) goto Error; |
1664 | | |
1665 | 61.3k | if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error; |
1666 | | |
1667 | 61.3k | if (Icc ->TagSaveAsRaw[n]) goto Error; // We don't support read raw tags as cooked |
1668 | | |
1669 | 61.3k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1670 | 61.3k | return Icc -> TagPtrs[n]; |
1671 | 61.3k | } |
1672 | | |
1673 | | // We need to read it. Get the offset and size to the file |
1674 | 60.4k | Offset = Icc -> TagOffsets[n]; |
1675 | 60.4k | TagSize = Icc -> TagSizes[n]; |
1676 | | |
1677 | 60.4k | if (TagSize < 8) goto Error; |
1678 | | |
1679 | 33.1k | io = Icc ->IOhandler; |
1680 | | |
1681 | 33.1k | if (io == NULL) { // This is a built-in profile that has been manipulated, abort early |
1682 | |
|
1683 | 0 | cmsSignalError(Icc->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted built-in profile."); |
1684 | 0 | goto Error; |
1685 | 0 | } |
1686 | | |
1687 | | // Seek to its location |
1688 | 33.1k | if (!io -> Seek(io, Offset)) |
1689 | 0 | goto Error; |
1690 | | |
1691 | | // Search for support on this tag |
1692 | 33.1k | TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig); |
1693 | 33.1k | if (TagDescriptor == NULL) { |
1694 | | |
1695 | 34 | char String[5]; |
1696 | | |
1697 | 34 | _cmsTagSignature2String(String, sig); |
1698 | | |
1699 | | // An unknown element was found. |
1700 | 34 | cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unknown tag type '%s' found.", String); |
1701 | 34 | goto Error; // Unsupported. |
1702 | 34 | } |
1703 | | |
1704 | | // if supported, get type and check if in list |
1705 | 33.0k | BaseType = _cmsReadTypeBase(io); |
1706 | 33.0k | if (BaseType == 0) goto Error; |
1707 | | |
1708 | 32.8k | if (!IsTypeSupported(TagDescriptor, BaseType)) goto Error; |
1709 | | |
1710 | 31.0k | TagSize -= 8; // Already read by the type base logic |
1711 | | |
1712 | | // Get type handler |
1713 | 31.0k | TypeHandler = _cmsGetTagTypeHandler(Icc ->ContextID, BaseType); |
1714 | 31.0k | if (TypeHandler == NULL) goto Error; |
1715 | 31.0k | LocalTypeHandler = *TypeHandler; |
1716 | | |
1717 | | |
1718 | | // Read the tag |
1719 | 31.0k | Icc -> TagTypeHandlers[n] = TypeHandler; |
1720 | | |
1721 | 31.0k | LocalTypeHandler.ContextID = Icc ->ContextID; |
1722 | 31.0k | LocalTypeHandler.ICCVersion = Icc ->Version; |
1723 | 31.0k | Icc -> TagPtrs[n] = LocalTypeHandler.ReadPtr(&LocalTypeHandler, io, &ElemCount, TagSize); |
1724 | | |
1725 | | // The tag type is supported, but something wrong happened and we cannot read the tag. |
1726 | | // let know the user about this (although it is just a warning) |
1727 | 31.0k | if (Icc -> TagPtrs[n] == NULL) { |
1728 | | |
1729 | 13.0k | char String[5]; |
1730 | | |
1731 | 13.0k | _cmsTagSignature2String(String, sig); |
1732 | 13.0k | cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Corrupted tag '%s'", String); |
1733 | 13.0k | goto Error; |
1734 | 13.0k | } |
1735 | | |
1736 | | // This is a weird error that may be a symptom of something more serious, the number of |
1737 | | // stored item is actually less than the number of required elements. |
1738 | 18.0k | if (ElemCount < TagDescriptor ->ElemCount) { |
1739 | | |
1740 | 5 | char String[5]; |
1741 | | |
1742 | 5 | _cmsTagSignature2String(String, sig); |
1743 | 5 | cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "'%s' Inconsistent number of items: expected %d, got %d", |
1744 | 5 | String, TagDescriptor ->ElemCount, ElemCount); |
1745 | 5 | goto Error; |
1746 | 5 | } |
1747 | | |
1748 | | |
1749 | | // Return the data |
1750 | 18.0k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1751 | 18.0k | return Icc -> TagPtrs[n]; |
1752 | | |
1753 | | |
1754 | | // Return error and unlock the data |
1755 | 64.2k | Error: |
1756 | | |
1757 | 64.2k | freeOneTag(Icc, n); |
1758 | 64.2k | Icc->TagPtrs[n] = NULL; |
1759 | | |
1760 | 64.2k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1761 | 64.2k | return NULL; |
1762 | 18.0k | } |
1763 | | |
1764 | | |
1765 | | // Get true type of data |
1766 | | cmsTagTypeSignature _cmsGetTagTrueType(cmsHPROFILE hProfile, cmsTagSignature sig) |
1767 | 25.3k | { |
1768 | 25.3k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1769 | 25.3k | cmsTagTypeHandler* TypeHandler; |
1770 | 25.3k | int n; |
1771 | | |
1772 | | // Search for given tag in ICC profile directory |
1773 | 25.3k | n = _cmsSearchTag(Icc, sig, TRUE); |
1774 | 25.3k | if (n < 0) return (cmsTagTypeSignature) 0; // Not found, return NULL |
1775 | | |
1776 | | // Get the handler. The true type is there |
1777 | 25.3k | TypeHandler = Icc -> TagTypeHandlers[n]; |
1778 | 25.3k | return TypeHandler ->Signature; |
1779 | 25.3k | } |
1780 | | |
1781 | | |
1782 | | // Write a single tag. This just keeps track of the tak into a list of "to be written". If the tag is already |
1783 | | // in that list, the previous version is deleted. |
1784 | | cmsBool CMSEXPORT cmsWriteTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data) |
1785 | 202k | { |
1786 | 202k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1787 | 202k | cmsTagTypeHandler* TypeHandler = NULL; |
1788 | 202k | cmsTagTypeHandler LocalTypeHandler; |
1789 | 202k | cmsTagDescriptor* TagDescriptor = NULL; |
1790 | 202k | cmsTagTypeSignature Type; |
1791 | 202k | int i; |
1792 | 202k | cmsFloat64Number Version; |
1793 | 202k | char TypeString[5], SigString[5]; |
1794 | | |
1795 | 202k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE; |
1796 | | |
1797 | | // To delete tags. |
1798 | 202k | if (data == NULL) { |
1799 | | |
1800 | | // Delete the tag |
1801 | 0 | i = _cmsSearchTag(Icc, sig, FALSE); |
1802 | 0 | if (i >= 0) { |
1803 | | |
1804 | | // Use zero as a mark of deleted |
1805 | 0 | _cmsDeleteTagByPos(Icc, i); |
1806 | 0 | Icc ->TagNames[i] = (cmsTagSignature) 0; |
1807 | 0 | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1808 | 0 | return TRUE; |
1809 | 0 | } |
1810 | | // Didn't find the tag |
1811 | 0 | goto Error; |
1812 | 0 | } |
1813 | | |
1814 | 202k | if (!_cmsNewTag(Icc, sig, &i)) goto Error; |
1815 | | |
1816 | | // This is not raw |
1817 | 202k | Icc ->TagSaveAsRaw[i] = FALSE; |
1818 | | |
1819 | | // This is not a link |
1820 | 202k | Icc ->TagLinked[i] = (cmsTagSignature) 0; |
1821 | | |
1822 | | // Get information about the TAG. |
1823 | 202k | TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig); |
1824 | 202k | if (TagDescriptor == NULL){ |
1825 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported tag '%x'", sig); |
1826 | 0 | goto Error; |
1827 | 0 | } |
1828 | | |
1829 | | |
1830 | | // Now we need to know which type to use. It depends on the version. |
1831 | 202k | Version = cmsGetProfileVersion(hProfile); |
1832 | | |
1833 | 202k | if (TagDescriptor ->DecideType != NULL) { |
1834 | | |
1835 | | // Let the tag descriptor to decide the type base on depending on |
1836 | | // the data. This is useful for example on parametric curves, where |
1837 | | // curves specified by a table cannot be saved as parametric and needs |
1838 | | // to be casted to single v2-curves, even on v4 profiles. |
1839 | | |
1840 | 145k | Type = TagDescriptor ->DecideType(Version, data); |
1841 | 145k | } |
1842 | 56.5k | else { |
1843 | | |
1844 | 56.5k | Type = TagDescriptor ->SupportedTypes[0]; |
1845 | 56.5k | } |
1846 | | |
1847 | | // Does the tag support this type? |
1848 | 202k | if (!IsTypeSupported(TagDescriptor, Type)) { |
1849 | |
|
1850 | 0 | _cmsTagSignature2String(TypeString, (cmsTagSignature) Type); |
1851 | 0 | _cmsTagSignature2String(SigString, sig); |
1852 | |
|
1853 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString); |
1854 | 0 | goto Error; |
1855 | 0 | } |
1856 | | |
1857 | | // Does we have a handler for this type? |
1858 | 202k | TypeHandler = _cmsGetTagTypeHandler(Icc->ContextID, Type); |
1859 | 202k | if (TypeHandler == NULL) { |
1860 | |
|
1861 | 0 | _cmsTagSignature2String(TypeString, (cmsTagSignature) Type); |
1862 | 0 | _cmsTagSignature2String(SigString, sig); |
1863 | |
|
1864 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported type '%s' for tag '%s'", TypeString, SigString); |
1865 | 0 | goto Error; // Should never happen |
1866 | 0 | } |
1867 | | |
1868 | | |
1869 | | // Fill fields on icc structure |
1870 | 202k | Icc ->TagTypeHandlers[i] = TypeHandler; |
1871 | 202k | Icc ->TagNames[i] = sig; |
1872 | 202k | Icc ->TagSizes[i] = 0; |
1873 | 202k | Icc ->TagOffsets[i] = 0; |
1874 | | |
1875 | 202k | LocalTypeHandler = *TypeHandler; |
1876 | 202k | LocalTypeHandler.ContextID = Icc ->ContextID; |
1877 | 202k | LocalTypeHandler.ICCVersion = Icc ->Version; |
1878 | 202k | Icc ->TagPtrs[i] = LocalTypeHandler.DupPtr(&LocalTypeHandler, data, TagDescriptor ->ElemCount); |
1879 | | |
1880 | 202k | if (Icc ->TagPtrs[i] == NULL) { |
1881 | |
|
1882 | 0 | _cmsTagSignature2String(TypeString, (cmsTagSignature) Type); |
1883 | 0 | _cmsTagSignature2String(SigString, sig); |
1884 | 0 | cmsSignalError(Icc ->ContextID, cmsERROR_CORRUPTION_DETECTED, "Malformed struct in type '%s' for tag '%s'", TypeString, SigString); |
1885 | |
|
1886 | 0 | goto Error; |
1887 | 0 | } |
1888 | | |
1889 | 202k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1890 | 202k | return TRUE; |
1891 | | |
1892 | 0 | Error: |
1893 | 0 | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1894 | 0 | return FALSE; |
1895 | | |
1896 | 202k | } |
1897 | | |
1898 | | // Read and write raw data. Read/Write Raw/cooked pairs try to maintain consistency within the pair. Some sequences |
1899 | | // raw/cooked would work, but at a cost. Data "cooked" may be converted to "raw" by using the "write" serialization logic. |
1900 | | // In general it is better to avoid mixing pairs. |
1901 | | |
1902 | | cmsUInt32Number CMSEXPORT cmsReadRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, void* data, cmsUInt32Number BufferSize) |
1903 | 405k | { |
1904 | 405k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
1905 | 405k | void *Object; |
1906 | 405k | int i; |
1907 | 405k | cmsIOHANDLER* MemIO; |
1908 | 405k | cmsTagTypeHandler* TypeHandler = NULL; |
1909 | 405k | cmsTagTypeHandler LocalTypeHandler; |
1910 | 405k | cmsTagDescriptor* TagDescriptor = NULL; |
1911 | 405k | cmsUInt32Number rc; |
1912 | 405k | cmsUInt32Number Offset, TagSize; |
1913 | | |
1914 | | // Sanity check |
1915 | 405k | if (data != NULL && BufferSize == 0) return 0; |
1916 | | |
1917 | 405k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0; |
1918 | | |
1919 | | // Search for given tag in ICC profile directory |
1920 | | |
1921 | 405k | i = _cmsSearchTag(Icc, sig, TRUE); |
1922 | 405k | if (i < 0) goto Error; // Not found, |
1923 | | |
1924 | | // It is already read? |
1925 | 155k | if (Icc -> TagPtrs[i] == NULL) { |
1926 | | |
1927 | | // Not yet, get original position |
1928 | 89.3k | Offset = Icc ->TagOffsets[i]; |
1929 | 89.3k | TagSize = Icc ->TagSizes[i]; |
1930 | | |
1931 | | // read the data directly, don't keep copy |
1932 | | |
1933 | 89.3k | if (data != NULL) { |
1934 | | |
1935 | 59.5k | if (BufferSize < TagSize) |
1936 | 5.02k | TagSize = BufferSize; |
1937 | | |
1938 | 59.5k | if (!Icc ->IOhandler ->Seek(Icc ->IOhandler, Offset)) goto Error; |
1939 | 59.5k | if (!Icc ->IOhandler ->Read(Icc ->IOhandler, data, 1, TagSize)) goto Error; |
1940 | | |
1941 | 5.05k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1942 | 5.05k | return TagSize; |
1943 | 59.5k | } |
1944 | | |
1945 | 29.7k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1946 | 29.7k | return Icc ->TagSizes[i]; |
1947 | 89.3k | } |
1948 | | |
1949 | | // The data has been already read, or written. But wait!, maybe the user choose to save as |
1950 | | // raw data. In this case, return the raw data directly |
1951 | | |
1952 | 66.5k | if (Icc ->TagSaveAsRaw[i]) { |
1953 | | |
1954 | 65.3k | if (data != NULL) { |
1955 | | |
1956 | 43.5k | TagSize = Icc ->TagSizes[i]; |
1957 | 43.5k | if (BufferSize < TagSize) |
1958 | 0 | TagSize = BufferSize; |
1959 | | |
1960 | 43.5k | memmove(data, Icc ->TagPtrs[i], TagSize); |
1961 | | |
1962 | 43.5k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1963 | 43.5k | return TagSize; |
1964 | 43.5k | } |
1965 | | |
1966 | 21.7k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1967 | 21.7k | return Icc ->TagSizes[i]; |
1968 | 65.3k | } |
1969 | | |
1970 | | // Already read, or previously set by cmsWriteTag(). We need to serialize that |
1971 | | // data to raw to get something that makes sense |
1972 | | |
1973 | 1.20k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
1974 | 1.20k | Object = cmsReadTag(hProfile, sig); |
1975 | 1.20k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0; |
1976 | | |
1977 | 1.20k | if (Object == NULL) goto Error; |
1978 | | |
1979 | | // Now we need to serialize to a memory block: just use a memory iohandler |
1980 | | |
1981 | 1.20k | if (data == NULL) { |
1982 | 401 | MemIO = cmsOpenIOhandlerFromNULL(cmsGetProfileContextID(hProfile)); |
1983 | 802 | } else{ |
1984 | 802 | MemIO = cmsOpenIOhandlerFromMem(cmsGetProfileContextID(hProfile), data, BufferSize, "w"); |
1985 | 802 | } |
1986 | 1.20k | if (MemIO == NULL) goto Error; |
1987 | | |
1988 | | // Obtain type handling for the tag |
1989 | 1.20k | TypeHandler = Icc ->TagTypeHandlers[i]; |
1990 | 1.20k | TagDescriptor = _cmsGetTagDescriptor(Icc-> ContextID, sig); |
1991 | 1.20k | if (TagDescriptor == NULL) { |
1992 | 0 | cmsCloseIOhandler(MemIO); |
1993 | 0 | goto Error; |
1994 | 0 | } |
1995 | | |
1996 | 1.20k | if (TypeHandler == NULL) goto Error; |
1997 | | |
1998 | | // Serialize |
1999 | 1.20k | LocalTypeHandler = *TypeHandler; |
2000 | 1.20k | LocalTypeHandler.ContextID = Icc ->ContextID; |
2001 | 1.20k | LocalTypeHandler.ICCVersion = Icc ->Version; |
2002 | | |
2003 | 1.20k | if (!_cmsWriteTypeBase(MemIO, TypeHandler ->Signature)) { |
2004 | 0 | cmsCloseIOhandler(MemIO); |
2005 | 0 | goto Error; |
2006 | 0 | } |
2007 | | |
2008 | 1.20k | if (!LocalTypeHandler.WritePtr(&LocalTypeHandler, MemIO, Object, TagDescriptor ->ElemCount)) { |
2009 | 114 | cmsCloseIOhandler(MemIO); |
2010 | 114 | goto Error; |
2011 | 114 | } |
2012 | | |
2013 | | // Get Size and close |
2014 | 1.08k | rc = MemIO ->Tell(MemIO); |
2015 | 1.08k | cmsCloseIOhandler(MemIO); // Ignore return code this time |
2016 | | |
2017 | 1.08k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2018 | 1.08k | return rc; |
2019 | | |
2020 | 304k | Error: |
2021 | 304k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2022 | 304k | return 0; |
2023 | 1.20k | } |
2024 | | |
2025 | | // Similar to the anterior. This function allows to write directly to the ICC profile any data, without |
2026 | | // checking anything. As a rule, mixing Raw with cooked doesn't work, so writing a tag as raw and then reading |
2027 | | // it as cooked without serializing does result into an error. If that is what you want, you will need to dump |
2028 | | // the profile to memry or disk and then reopen it. |
2029 | | cmsBool CMSEXPORT cmsWriteRawTag(cmsHPROFILE hProfile, cmsTagSignature sig, const void* data, cmsUInt32Number Size) |
2030 | 49.0k | { |
2031 | 49.0k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
2032 | 49.0k | int i; |
2033 | | |
2034 | 49.0k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return 0; |
2035 | | |
2036 | 49.0k | if (!_cmsNewTag(Icc, sig, &i)) { |
2037 | 0 | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2038 | 0 | return FALSE; |
2039 | 0 | } |
2040 | | |
2041 | | // Mark the tag as being written as RAW |
2042 | 49.0k | Icc ->TagSaveAsRaw[i] = TRUE; |
2043 | 49.0k | Icc ->TagNames[i] = sig; |
2044 | 49.0k | Icc ->TagLinked[i] = (cmsTagSignature) 0; |
2045 | | |
2046 | | // Keep a copy of the block |
2047 | 49.0k | Icc ->TagPtrs[i] = _cmsDupMem(Icc ->ContextID, data, Size); |
2048 | 49.0k | Icc ->TagSizes[i] = Size; |
2049 | | |
2050 | 49.0k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2051 | | |
2052 | 49.0k | if (Icc->TagPtrs[i] == NULL) { |
2053 | 0 | Icc->TagNames[i] = (cmsTagSignature) 0; |
2054 | 0 | return FALSE; |
2055 | 0 | } |
2056 | 49.0k | return TRUE; |
2057 | 49.0k | } |
2058 | | |
2059 | | // Using this function you can collapse several tag entries to the same block in the profile |
2060 | | cmsBool CMSEXPORT cmsLinkTag(cmsHPROFILE hProfile, cmsTagSignature sig, cmsTagSignature dest) |
2061 | 18.7k | { |
2062 | 18.7k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
2063 | 18.7k | int i; |
2064 | | |
2065 | 18.7k | if (!_cmsLockMutex(Icc->ContextID, Icc ->UsrMutex)) return FALSE; |
2066 | | |
2067 | 18.7k | if (!_cmsNewTag(Icc, sig, &i)) { |
2068 | 0 | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2069 | 0 | return FALSE; |
2070 | 0 | } |
2071 | | |
2072 | | // Keep necessary information |
2073 | 18.7k | Icc ->TagSaveAsRaw[i] = FALSE; |
2074 | 18.7k | Icc ->TagNames[i] = sig; |
2075 | 18.7k | Icc ->TagLinked[i] = dest; |
2076 | | |
2077 | 18.7k | Icc ->TagPtrs[i] = NULL; |
2078 | 18.7k | Icc ->TagSizes[i] = 0; |
2079 | 18.7k | Icc ->TagOffsets[i] = 0; |
2080 | | |
2081 | 18.7k | _cmsUnlockMutex(Icc->ContextID, Icc ->UsrMutex); |
2082 | 18.7k | return TRUE; |
2083 | 18.7k | } |
2084 | | |
2085 | | |
2086 | | // Returns the tag linked to sig, in the case two tags are sharing same resource |
2087 | | cmsTagSignature CMSEXPORT cmsTagLinkedTo(cmsHPROFILE hProfile, cmsTagSignature sig) |
2088 | 2.07k | { |
2089 | 2.07k | _cmsICCPROFILE* Icc = (_cmsICCPROFILE*) hProfile; |
2090 | 2.07k | int i; |
2091 | | |
2092 | | // Search for given tag in ICC profile directory |
2093 | 2.07k | i = _cmsSearchTag(Icc, sig, FALSE); |
2094 | 2.07k | if (i < 0) return (cmsTagSignature) 0; // Not found, return 0 |
2095 | | |
2096 | 9 | return Icc -> TagLinked[i]; |
2097 | 2.07k | } |