-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoyranos_cmm_oiio.cpp
1206 lines (1049 loc) · 39.8 KB
/
oyranos_cmm_oiio.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file oyranos_cmm_oiio.c
*
* OpenImageIO based file i/o module for Oyranos
*
* @par Copyright:
* 2014 (C) Kai-Uwe Behrmann
*
* @brief OIIO filter for Oyranos
* @internal
* @author Kai-Uwe Behrmann <[email protected]>
* @par License:
* new BSD <http://www.opensource.org/licenses/bsd-license.php>
* @since 2014/03/21
*/
#include "oyCMM_s.h"
#include "oyCMMapi4_s.h"
#include "oyCMMapi7_s.h"
#include "oyCMMapiFilter_s.h"
#include "oyCMMui_s.h"
#include "oyConnectorImaging_s.h"
#include "oyProfiles_s.h"
#include "oyranos_config.h"
#include "oyranos_definitions.h"
#include <math.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h> /* isspace() */
#include <OpenImageIO/imageio.h>
#include "jpegmarkers.h"
#include <png.h>
#include <tiffconf.h>
#include <tiffio.h>
/* --- internal definitions --- */
/** The CMM_NICK consists of four bytes, which appear as well in the library name. This is important for Oyranos to identify the required filter struct name. */
#define CMM_NICK "oiio"
#define OY_OIIO_FILTER_REGISTRATION OY_TOP_INTERNAL OY_SLASH OY_DOMAIN_INTERNAL OY_SLASH OY_TYPE_STD OY_SLASH "file_loader"
/** The message function pointer to use for messaging. */
oyMessage_f oiio_msg = oyMessageFunc;
/* Helpers */
#if defined(__GNUC__)
# define OY_DBG_FORMAT_ "%s:%d %s() "
# define OY_DBG_ARGS_ strrchr(__FILE__,'/') ? strrchr(__FILE__,'/')+1 : __FILE__,__LINE__,__func__
#else
# define OY_DBG_FORMAT_ "%s:%d "
# define OY_DBG_ARGS_ strrchr(__FILE__,'/') ? strrchr(__FILE__,'/')+1 : __FILE__,__LINE__
#endif
#define _DBG_FORMAT_ OY_DBG_FORMAT_
#define _DBG_ARGS_ OY_DBG_ARGS_
/* i18n */
#ifdef USE_GETTEXT
# include <libintl.h>
# define _(text) dgettext( "oyranos_"CMM_NICK, text )
#else
# define _(text) text
#endif
extern "C" {
void* oyAllocateFunc_ (size_t size);
void oyDeAllocateFunc_ (void * data);
#define AD oyAllocateFunc_, oyDeAllocateFunc_
int oiioInit ( oyStruct_s * module_info );
int oiioFilter_CmmRun ( oyFilterPlug_s * requestor_plug,
oyPixelAccess_s * ticket );
const char * oiioApi4UiGetText2 ( const char * select,
oyNAME_e type,
const char * format );
int oiioGetOFORMS ( oyCMMapiFilter_s * module,
oyOptions_s * oy_opts,
char ** ui_text,
oyAlloc_f allocateFunc );
const char * oiioApi4UiGetText ( const char * select,
oyNAME_e type,
oyStruct_s * context );
extern const char * oiio_api4_ui_texts[];
char * oiioFilterNode_GetText ( oyFilterNode_s * node,
oyNAME_e type,
oyAlloc_f allocateFunc );
}
/* --- implementations --- */
/** Function oiioCMMInit
* @brief API requirement
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
int oiioCMMInit ( oyStruct_s * )
{
int error = 0;
return error;
}
/** Function oiioCMMMessageFuncSet
* @brief API requirement
*
* A Oyranos user might want its own message function and omit the default
* one.
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
int oiioCMMMessageFuncSet ( oyMessage_f message_func )
{
oiio_msg = message_func;
return 0;
}
/**
* This function implements oyCMMinfoGetText_f.
*
* Implement at least "name", "manufacturer" and "copyright". If you like with
* internationalisation.
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
const char * oiioGetText ( const char * select,
oyNAME_e type,
oyStruct_s * context )
{
if(strcmp(select, "name")==0)
{
if(type == oyNAME_NICK)
return _(CMM_NICK);
else if(type == oyNAME_NAME)
return _("CMM loader");
else
return _("CMM loader filter");
} else if(strcmp(select, "manufacturer")==0)
{
if(type == oyNAME_NICK)
return _("oy");
else if(type == oyNAME_NAME)
return _("Kai-Uwe Behrmann");
else
return _("Oyranos project; www: http://www.oyranos.com; support/email: [email protected]; sources: http://www.oyranos.com/downloads");
} else if(strcmp(select, "copyright")==0)
{
if(type == oyNAME_NICK)
return _("newBSD");
else if(type == oyNAME_NAME)
return _("Copyright (c) 2014 Kai-Uwe Behrmann; newBSD");
else
return _("new BSD license: http://www.opensource.org/licenses/bsd-license.php");
} else if(strcmp(select, "help")==0)
{
if(type == oyNAME_NICK)
return _("help");
else if(type == oyNAME_NAME)
return _("The module generates a series of file format reader filters.");
else
return _("The module generates a series of file format reader filters. The OpenImageIO library is used to perform the actual reading. This module performs for several formats ICC profile detection, generation and fallback generation.");
}
return 0;
}
const char *oiio_texts[5] = {"name","copyright","manufacturer","help",0};
oyIcon_s oiio_icon = {oyOBJECT_ICON_S, 0,0,0, 0,0,0, "oyranos_logo.png"};
/** @instance oiio_cmm_module
* @brief oiio module infos
*
* This structure is dlopened by Oyranos. Its name has to consist of the
* following elements:
* - the four byte CMM_NICK plus
* - "_cmm_module"
* This string must be included in the the filters filename.
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
oyCMM_s oiio_cmm_module = {
oyOBJECT_CMM_INFO_S, /**< ::type; the object type */
0,0,0, /**< static objects omit these fields */
CMM_NICK, /**< ::cmm; the four char filter id */
(char*)"0.9.6", /**< ::backend_version */
oiioGetText, /**< ::getText; UI texts */
(char**)oiio_texts, /**< ::texts; list of arguments to getText */
OYRANOS_VERSION, /**< ::oy_compatibility; last supported Oyranos CMM API*/
/** ::api; The first filter api structure. */
NULL,
/** ::icon; module icon */
&oiio_icon,
oiioInit
};
/* OY_OIIO_FILTER_REGISTRATION ----------------------------------------------*/
#define OY_OIIO_FILTER_REGISTRATION_BASE OY_TOP_SHARED OY_SLASH OY_DOMAIN_INTERNAL OY_SLASH OY_TYPE_STD OY_SLASH
const char *icc_file_formats[7] = {"jpeg","tiff","png","hdr","exr",0,0};
/** @instance oiio_api7
* @brief oiio oyCMMapi7_s implementation
*
* a filter providing a CMM filter
*
* @version Oyranos: 0.9.6
* @date 2014/03/21
* @since 2014/03/21 (Oyranos: 0.9.6)
*/
oyCMMapi_s * oiioApi7CmmCreate ( const char * format,
const char * ext )
{
int32_t cmm_version[3] = {OYRANOS_VERSION_A,OYRANOS_VERSION_B,OYRANOS_VERSION_C},
module_api[3] = {0,9,6};
static oyDATATYPE_e data_types[7] = {oyUINT8, oyUINT16, oyUINT32,
oyHALF, oyFLOAT, oyDOUBLE, (oyDATATYPE_e)0};
oyConnectorImaging_s * plug = oyConnectorImaging_New(0),
* socket = oyConnectorImaging_New(0);
static oyConnectorImaging_s * plugs[2] = {0,0},
* sockets[2] = {0,0};
char * ext_ = NULL;
const char * properties[] =
{
"file=read", /* file read|write */
"image=pixel", /* image type, pixel/vector/font */
"layers=1", /* layer count, one for plain images */
"icc=0", /* image type ICC profile support */
"ext=", /* supported extensions */
0
};
oyStringAddPrintf( &ext_, oyAllocateFunc_, oyDeAllocateFunc_, "ext=%s", ext+1 );
if(strcmp(format,"tiff") == 0)
oyStringAddPrintf( &ext_, oyAllocateFunc_, oyDeAllocateFunc_, ",sti" );
properties[4] = ext_;
int pos = 0;
while(icc_file_formats[pos])
if(strcmp(icc_file_formats[pos++],format))
properties[3] = "icc=1"; /* image type ICC profile support */
plugs[0] = plug;
sockets[0] = socket;
char * registration = NULL;
oyStringAddPrintf( ®istration, AD,
OY_OIIO_FILTER_REGISTRATION_BASE"file_read.input_%s._%s._CPU._ACCEL", format, CMM_NICK );
if(oy_debug >= 2) oiio_msg(oyMSG_DBG, NULL, _DBG_FORMAT_ "registration:%s oiio v%d %s", _DBG_ARGS_,
registration,
OpenImageIO::openimageio_version(), ext_ );
#if 0
oyConnectorImaging_SetDataTypes( plug, data_types, 6 );
oyConnectorImaging_SetReg( plug, "//" OY_TYPE_STD "/image.data" );
oyConnectorImaging_SetMatch( plug, oyFilterSocket_MatchImagingPlug );
oyConnectorImaging_SetTexts( plug, oyCMMgetImageConnectorPlugText,
oy_image_connector_texts );
oyConnectorImaging_SetIsPlug( plug, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_MAX_COLOR_OFFSET, -1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_MIN_CHANNELS_COUNT, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_MAX_CHANNELS_COUNT, 16 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_MIN_COLOR_COUNT, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_MAX_COLOR_COUNT, 16 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_CAN_INTERWOVEN, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_CAN_PREMULTIPLIED_ALPHA, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_CAN_NONPREMULTIPLIED_ALPHA, 1 );
oyConnectorImaging_SetCapability( plug, oyCONNECTOR_IMAGING_CAP_ID, 1 );
#endif
oyConnectorImaging_SetDataTypes( socket, data_types, 6 );
oyConnectorImaging_SetReg( socket, "//" OY_TYPE_STD "/image.data" );
oyConnectorImaging_SetMatch( socket, oyFilterSocket_MatchImagingPlug );
oyConnectorImaging_SetTexts( socket, oyCMMgetImageConnectorSocketText,
oy_image_connector_texts );
oyConnectorImaging_SetIsPlug( socket, 0 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_MAX_COLOR_OFFSET, -1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_MIN_CHANNELS_COUNT, 1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_MAX_CHANNELS_COUNT, 16 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_MIN_COLOR_COUNT, 1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_MAX_COLOR_COUNT, 16 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_CAN_INTERWOVEN, 1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_CAN_PREMULTIPLIED_ALPHA, 1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_CAN_NONPREMULTIPLIED_ALPHA, 1 );
oyConnectorImaging_SetCapability( socket, oyCONNECTOR_IMAGING_CAP_ID, 1 );
oyCMMapi7_s * cmm7 = oyCMMapi7_Create( oiioCMMInit, oiioCMMMessageFuncSet,
registration,
cmm_version, module_api,
NULL,
oiioFilter_CmmRun,
(oyConnector_s**)plugs, 0, 0,
(oyConnector_s**)sockets, 1, 0,
properties, 0 );
//oyFree_m_( registration );
return (oyCMMapi_s*) cmm7;
}
int deAllocData ( void ** data ) { oyDeAllocateFunc_(*data); *data = NULL; return 0; }
const char oiio_read_extra_options[] = {
"\n\
<" OY_TOP_SHARED ">\n\
<" OY_DOMAIN_INTERNAL ">\n\
<" OY_TYPE_STD ">\n\
<" "file_read" ">\n\
<filename></filename>\n\
</" "file_read" ">\n\
</" OY_TYPE_STD ">\n\
</" OY_DOMAIN_INTERNAL ">\n\
</" OY_TOP_SHARED ">\n"
};
/** @instance oiio_api4
* @brief oiio oyCMMapi4_s implementation
*
* a filter providing a CMM device link creator
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
oyCMMapi_s * oiioApi4CmmCreate ( const char * format )
{
int32_t cmm_version[3] = {OYRANOS_VERSION_A,OYRANOS_VERSION_B,OYRANOS_VERSION_C},
module_api[3] = {0,9,6};
oyPointer_s * backend_context = oyPointer_New(0);
char * registration = NULL;
const char * category = oiioApi4UiGetText2("category", oyNAME_NAME, format);
oyCMMuiGet_f getOFORMS = oiioGetOFORMS;
oyCMMui_s * ui = oyCMMui_Create( category, oiioApi4UiGetText,
oiio_api4_ui_texts, 0 );
oyOptions_s * oy_opts = NULL;
const char * oforms_options = oiio_read_extra_options;
oyCMMui_SetUiOptions( ui, oyStringCopy( oforms_options, oyAllocateFunc_ ), getOFORMS );
oyPointer_Set( backend_context, NULL, "oiio_file_format", oyStringCopy(format, oyAllocateFunc_),
"char*", deAllocData );
oyStringAddPrintf( ®istration, AD,
OY_OIIO_FILTER_REGISTRATION_BASE"file_read.input_%s._oiio._CPU._ACCEL", format );
oyCMMapi4_s * cmm4 = oyCMMapi4_Create( oiioCMMInit, oiioCMMMessageFuncSet,
registration,
cmm_version, module_api,
"",
NULL,
oiioFilterNode_GetText,
ui,
NULL );
oyCMMapi4_SetBackendContext( cmm4, backend_context );
oyOptions_Release( &oy_opts );
return (oyCMMapi_s*)cmm4;
}
char * oiioFilterNode_GetText ( oyFilterNode_s * node,
oyNAME_e type,
oyAlloc_f allocateFunc )
{
char * t = NULL;
const char * tmp = NULL;
oyOptions_s * node_options = oyFilterNode_GetOptions( node, 0 );
tmp = oyOptions_GetText(node_options, oyNAME_NICK);
if(tmp)
t = oyStringCopy( tmp, allocateFunc );
oyOptions_Release( &node_options );
return t;
}
#define A(long_text) oyStringAdd_( &tmp, long_text, AD )
/* TODO */
int oiioGetOFORMS ( oyCMMapiFilter_s * module,
oyOptions_s * oy_opts,
char ** ui_text,
oyAlloc_f allocateFunc )
{
int error = 0;
char * tmp = NULL;
*ui_text = tmp;
return error;
}
oyOptions_s* oiioFilter_CmmLoaderValidateOptions
( oyFilterCore_s * filter,
oyOptions_s * validate,
int statical,
uint32_t * result )
{
uint32_t error = !filter;
#if 0
if(!error)
error = !oyOptions_FindString( validate, "my_options", 0 );
#endif
*result = error;
return 0;
}
void oPNGerror( png_structp png, const char * text )
{
oiio_msg( oyMSG_ERROR, (oyStruct_s*)NULL/*node*/,
OY_DBG_FORMAT_ "%s",
OY_DBG_ARGS_, text );
}
void oPNGwarn( png_structp png, const char * text )
{
oiio_msg( oyMSG_WARN, (oyStruct_s*)NULL/*node*/,
OY_DBG_FORMAT_ "%s",
OY_DBG_ARGS_, text );
}
oyProfile_s * profileFromMatrix( double pandg[9], const char * name, int32_t icc_profile_flags )
{
oyProfile_s * p = oyProfile_FromName(name, icc_profile_flags, NULL);
if(!p)
{
oyOption_s * primaries = oyOption_FromRegistration( "//"
OY_TYPE_STD
"/color_matrix."
"redx_redy_greenx_greeny_bluex_bluey_whitex_whitey_gamma",
0);
oyOptions_s * opts = oyOptions_New(0),
* result = 0;
int pos = 0;
oyOptions_SetFromInt( &opts, "///icc_profile_flags", icc_profile_flags,
0, OY_CREATE_NEW );
/* red */
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
/* green */
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
/* blue */
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
/* white */
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
/* gamma */
oyOption_SetFromDouble( primaries, pandg[pos], pos, 0 ); pos++;
oyOptions_MoveIn( opts, &primaries, -1 );
oyOptions_Handle( "//"OY_TYPE_STD"/create_profile.icc",
opts,"create_profile.icc_profile.color_matrix",
&result );
p = (oyProfile_s*)oyOptions_GetType( result, -1, "icc_profile",
oyOBJECT_PROFILE_S );
oyProfile_AddTagText( p, icSigProfileDescriptionTag, name);
oyProfile_AddTagText( p, icSigCopyrightTag, "ICC License 2011");
oyOptions_Release( &result );
oyOptions_Release( &opts );
oyProfile_Install( p, oySCOPE_USER, NULL );
}
return p;
}
int select_icc_profile(j_decompress_ptr cinfo,
const char * filename,
JOCTET **icc_data_ptr,
unsigned int *icc_data_len)
{
unsigned int len;
int lIsITUFax = jpeg_get_marker_size( cinfo, JPEG_APP0+1, (JOCTET*)"G3FAX", 5, &len ) == 0;
{
char * profile_name = 0;
char * prof_mem = 0;
size_t size = 0;
switch(cinfo->out_color_space)
{
case JCS_GRAYSCALE:
profile_name = oyGetDefaultProfileName (oyASSUMED_GRAY, malloc);
break;
case JCS_RGB:
if(lIsITUFax)
{
profile_name = strdup("ITULab.icc");
if( !oyCheckProfile (profile_name, 0) )
prof_mem = (char*)oyGetProfileBlock( profile_name, &size, malloc );
else if(!oyCheckProfile ("ITUFAX.ICM", 0) )
prof_mem = (char*)oyGetProfileBlock( "ITUFAX.ICM", &size, malloc );
cinfo->out_color_space = JCS_YCbCr; // do'nt convert colors
} else {
/* guesswork */
const char * fn = strrchr( filename, OY_SLASH_C );
if(fn)
fn += 1;
else
fn = filename;
if(fn[0] == '_') /* Canon RAW AdobeRGB */
profile_name = strdup("compatibleWithAdobeRGB1998.icc");
else
{
profile_name = strdup("YCC profile - supports extended sRGB range PRELIMINARY 1-4-2002.icc");
if( !oyCheckProfile (profile_name, 0) )
{
prof_mem = (char*)oyGetProfileBlock( profile_name, &size, malloc );
cinfo->out_color_space = JCS_YCbCr; // do'nt convert colors
} else
profile_name = oyGetDefaultProfileName (oyASSUMED_RGB, malloc);
}
}
break;
case JCS_CMYK:
profile_name = oyGetDefaultProfileName (oyASSUMED_CMYK, malloc);
break;
case JCS_YCbCr:
if(lIsITUFax)
profile_name = strdup("ITULab.icc");
if( !oyCheckProfile (profile_name, 0) )
prof_mem = (char*)oyGetProfileBlock( profile_name, &size, malloc );
else if(!oyCheckProfile ("ITUFAX.ICM", 0) )
prof_mem = (char*)oyGetProfileBlock( "ITUFAX.ICM", &size, malloc );
else
profile_name = strdup("YCC profile - supports extended sRGB range PRELIMINARY 1-4-2002.icc");
break;
case JCS_UNKNOWN:
case JCS_YCCK:
break;
}
if( !oyCheckProfile (profile_name, 0) )
prof_mem = (char*)oyGetProfileBlock( profile_name, &size, malloc );
*icc_data_ptr = (JOCTET*)prof_mem;
*icc_data_len = size;
if(profile_name) free( profile_name );
if(size && prof_mem)
return 1;
}
return 0;
}
/** Function oiioFilter_CmmRun
* @brief implement oyCMMFilter_GetNext_f()
*
* The primary filter entry for data processing.
*
* @param requestor_plug the plug of the requesting node after
* my filter in the graph
* @param ticket the job ticket
*
* @version Oyranos: 0.9.6
* @since 2014/03/21 (Oyranos: 0.9.6)
* @date 2014/03/21
*/
int oiioFilter_CmmRun ( oyFilterPlug_s * requestor_plug,
oyPixelAccess_s * ticket )
{
oyFilterSocket_s * socket = 0;
oyStruct_s * socket_data = 0;
oyFilterNode_s * node = 0;
oyOptions_s * tags = 0;
int error = 0;
const char * filename = 0;
FILE * fp = 0;
oyDATATYPE_e data_type = oyUINT8;
oyPROFILE_e profile_type = oyASSUMED_RGB;
oyProfile_s * prof = 0;
oyImage_s * image_in = 0,
* output_image = 0;
oyPixel_t pixel_type = 0;
size_t fsize = 0;
uint8_t * buf = 0;
size_t mem_n = 0; /* needed memory in bytes */
int info_good = 1;
int32_t icc_profile_flags = 0;
if(requestor_plug->type_ == oyOBJECT_FILTER_PLUG_S)
{
socket = oyFilterPlug_GetSocket( requestor_plug );
socket_data = oyFilterSocket_GetData( socket );
}
/* passing through the data reading */
if(requestor_plug->type_ == oyOBJECT_FILTER_PLUG_S &&
socket_data)
{
error = oyFilterPlug_ImageRootRun( requestor_plug, ticket );
return error;
} else if(requestor_plug->type_ == oyOBJECT_FILTER_SOCKET_S)
{
/* To open the a image here seems not so straight forward.
* Still the plug-in should be prepared to initialise the image data before
* normal processing occurs.
*/
socket = oyFilterSocket_Copy( (oyFilterSocket_s*)requestor_plug, 0 );
requestor_plug = 0;
}
node = oyFilterSocket_GetNode( socket );
/* parse options */
if(error <= 0)
{
oyOptions_s * opts = oyFilterNode_GetOptions( node ,0 );
filename = oyOptions_FindString( opts, "filename", 0 );
oyOptions_FindInt( opts, "icc_profile_flags", 0, &icc_profile_flags );
oyOptions_Release( &opts );
}
/* file tests */
if(filename)
fp = fopen( filename, "rm" );
if(!fp)
{
oiio_msg( oyMSG_WARN, (oyStruct_s*)node,
OY_DBG_FORMAT_ " could not open: %s",
OY_DBG_ARGS_, oyNoEmptyString_m( filename ) );
return 1;
}
/* file size fun */
fseek(fp,0L,SEEK_END);
fsize = ftell(fp);
rewind(fp);
if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s*)node,
OY_DBG_FORMAT_ "file size %u",
OY_DBG_ARGS_, fsize );
/* open the image */
OpenImageIO::ImageInput * image = OpenImageIO::ImageInput::create( filename );
info_good = image ? 1:0;
if( !info_good )
{
oiio_msg( oyMSG_WARN, (oyStruct_s*)node,
OY_DBG_FORMAT_ "failed to get info of %s\n%s",
OY_DBG_ARGS_, oyNoEmptyString_m( filename ),
OpenImageIO::geterror().c_str() );
return FALSE;
}
/* get image informations */
OpenImageIO::ImageSpec spec;
image->open( std::string(filename), spec );
// set a ICC profile
// spec.attribute ("icc-profile", TypeDesc(TypeDesc::UINT8, blobsize), &blob);
OpenImageIO::TypeDesc::BASETYPE type = (OpenImageIO::TypeDesc::BASETYPE) spec.format.basetype;
switch(spec.format.basetype)
{
case OpenImageIO::TypeDesc::UINT8: data_type = oyUINT8; break;
case OpenImageIO::TypeDesc::UINT16: data_type = oyUINT16; break;
case OpenImageIO::TypeDesc::UINT32: data_type = oyUINT32; break;
case OpenImageIO::TypeDesc::HALF: data_type = oyHALF; break;
case OpenImageIO::TypeDesc::FLOAT: data_type = oyFLOAT; break;
case OpenImageIO::TypeDesc::DOUBLE: data_type = oyDOUBLE; break;
default: data_type = oyFLOAT; type = OpenImageIO::TypeDesc::FLOAT; break;
}
if( !info_good )
{
oiio_msg( oyMSG_WARN, (oyStruct_s*)node,
OY_DBG_FORMAT_ "failed to handle %s %s",
OY_DBG_ARGS_, oyNoEmptyString_m( filename ),
spec.format.c_str() );
image->close();
return FALSE;
}
pixel_type = oyChannels_m(spec.nchannels) | oyDataType_m(data_type);
/* allocate a buffer to hold the whole image */
mem_n = spec.width*spec.height*oyDataTypeGetSize(data_type)*spec.nchannels;
if(mem_n)
{
buf = (uint8_t*) oyAllocateFunc_(mem_n * sizeof(uint8_t));
if(!buf)
{
oiio_msg(oyMSG_WARN, (oyStruct_s *) node, _DBG_FORMAT_ "Could not allocate enough memory.", _DBG_ARGS_);
return 1;
}
}
if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s *) node, _DBG_FORMAT_ "allocate image data: 0x%x size: %d ", _DBG_ARGS_, (int)(intptr_t)
buf, mem_n );
/* decode the image into our buffer */
if(strcmp(image->format_name(),"jpeg") != 0)
image->read_image( type, buf );
/* get ICC Profile */
if(strcmp(image->format_name(),"jpeg") == 0)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
// Setup decompression structure
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress (&cinfo);
jpeg_stdio_src (&cinfo, fp);
for (int m = 0; m < 16; m++)
jpeg_save_markers(&cinfo, JPEG_APP0 + m, 0xFFFF);
(void) jpeg_read_header (&cinfo, TRUE);
unsigned int len = 0;
unsigned char * icc = NULL;
if( jpeg_get_marker_size( &cinfo, JPEG_APP0+2, (JOCTET*)"ICC_PROFILE", 12, &len ) == 0 )
{
icc = (unsigned char*) malloc(len);
jpeg_get_marker_data( &cinfo, JPEG_APP0+2, (JOCTET*)"ICC_PROFILE", 12, len, (JOCTET*)icc );
}
if (icc && len)
{ if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s*)node, OY_DBG_FORMAT_ "jpeg embedded profile found: %d", OY_DBG_ARGS_, len);
} else if (select_icc_profile(&cinfo, filename, &icc, &len))
{ if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s*)node, OY_DBG_FORMAT_ "jpeg default profile selected: %d", OY_DBG_ARGS_, len);
} else
if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s*)node, OY_DBG_FORMAT_ "jpeg no profile found", OY_DBG_ARGS_);
if(icc && len)
{
prof = oyProfile_FromMem( len, icc, 0, 0 );
free(icc); icc = NULL;
len = 0;
}
jpeg_start_decompress (&cinfo);
while (cinfo.output_scanline < cinfo.output_height) {
/* jpeg_read_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could ask for
* more than one scanline at a time if that's more convenient.
*/
JSAMPROW b = &buf[(cinfo.output_width * cinfo.output_components)*cinfo.output_scanline];
jpeg_read_scanlines(&cinfo, &b, 1);
}
icColorSpaceSignature csp = (icColorSpaceSignature) oyProfile_GetSignature(prof,oySIGNATURE_COLOR_SPACE);
if(csp == icSigCmykData)
{
int n = spec.width * spec.height * 4;
if(data_type == oyUINT8)
{
uint8_t * d = (uint8_t*)buf;
int i;
#pragma omp parallel for private(i)
for(i = 0; i < n; ++i)
d[i] = 255 - d[i];
}
}
jpeg_finish_decompress (&cinfo);
jpeg_destroy_decompress (&cinfo);
} else if(strcmp(image->format_name(),"png") == 0)
{
png_structp png_ptr = 0;
png_infop info_ptr = 0;
int color_type = 0;
png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING,
(png_voidp)filename,
oPNGerror, oPNGwarn );
info_ptr = png_create_info_struct(png_ptr);
png_init_io( png_ptr, fp );
png_read_info( png_ptr, info_ptr );
png_read_update_info( png_ptr, info_ptr );
color_type = png_get_color_type( png_ptr, info_ptr );
#if defined(PNG_iCCP_SUPPORTED)
png_charp name = 0;
png_charp profile = 0;
png_uint_32 proflen = 0;
int compression = 0;
if( png_get_iCCP( png_ptr, info_ptr, &name, &compression,
&profile, &proflen ) )
{
prof = oyProfile_FromMem( proflen, profile, 0,0 );
if(oy_debug)
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "ICC profile (size: %d): \"%s\"",
OY_DBG_ARGS_, proflen, oyNoEmptyString_m( name ) );
} else
{
switch( color_type )
{
case PNG_COLOR_TYPE_GRAY:
case PNG_COLOR_TYPE_GRAY_ALPHA:
profile_type = oyASSUMED_GRAY;
break;
case PNG_COLOR_TYPE_PALETTE:
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGB_ALPHA:
default:
break;
}
if(oy_debug)
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "no embedded ICC profile",
OY_DBG_ARGS_);
}
#endif
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp)NULL );
} else if(strcmp(image->format_name(),"tiff") == 0)
{
size_t proflen = 0;
char * tiff_ptr = NULL;
uint16_t photomet = PHOTOMETRIC_MINISBLACK;
TIFF * tif = TIFFOpen(filename, "rm");
if(!TIFFGetField( tif, TIFFTAG_PHOTOMETRIC, &photomet ))
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "no TIFFTAG_PHOTOMETRIC",
OY_DBG_ARGS_);
if(TIFFGetField( tif, TIFFTAG_ICCPROFILE, &proflen, &tiff_ptr ))
{
prof = oyProfile_FromMem( proflen, tiff_ptr, 0,0 );
if(oy_debug)
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "ICC profile (size: %d)",
OY_DBG_ARGS_, proflen);
} else
{
oyProfile_s * p = NULL;
if(oy_debug)
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "no embedded ICC profile found",
OY_DBG_ARGS_);
/* build color spaces from tiff tags */
switch(photomet)
{ case PHOTOMETRIC_LOGLUV:
{ /* creating an special profile with equal energy white point */
double primaries_and_gamma[9] = {
1.0,0.0, 0.0,1.0, 0.0,0.0, 0.333,0.333, 1.0};
char * name = NULL;
oyStringAddPrintf( &name, oyAllocateFunc_, oyDeAllocateFunc_,
"XYZ DE%s",
(icc_profile_flags & OY_ICC_VERSION_2) ? " v2" : " v4" );
p = profileFromMatrix( primaries_and_gamma, name, icc_profile_flags );
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "set %s",
OY_DBG_ARGS_, name);
oyDeAllocateFunc_( name );
}
break;
case PHOTOMETRIC_RGB:
case PHOTOMETRIC_PALETTE:
case PHOTOMETRIC_YCBCR:
profile_type = oyASSUMED_RGB;
break;
case PHOTOMETRIC_CIELAB:
{
int n = spec.width * spec.height;
if(data_type == oyUINT8)
{
uint8_t * d = (uint8_t*)buf;
for(int i = 0; i < n; ++i)
{
d[i*spec.nchannels+1] -= 128;
d[i*spec.nchannels+2] -= 128;
}
} else
if(data_type == oyUINT16)
{
uint16_t * d = (uint16_t*)buf;
for(int i = 0; i < n; ++i)
{
d[i*spec.nchannels+1] -= 32768;
d[i*spec.nchannels+2] -= 32768;
}
}
}
case PHOTOMETRIC_ICCLAB: /**@todo TODO signiert / unsigniert */
profile_type = oyASSUMED_LAB;
break;
case PHOTOMETRIC_ITULAB:
p = oyProfile_FromFile( "ITULab.icc", 0, 0 );
break;
case PHOTOMETRIC_SEPARATED:
profile_type = oyASSUMED_CMYK;
break;
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
case PHOTOMETRIC_LOGL:
profile_type = oyASSUMED_GRAY;
break;
}
prof = p;
}
TIFFClose(tif);
} else if(strcmp(image->format_name(),"openexr") == 0 ||
strcmp(image->format_name(),"hdr") == 0)
{
{ /* creating an special profile with equal energy white point */
double primaries_and_gamma[9] = {
0.64,0.33, 0.30,0.60, 0.15,0.05, 0.3127,0.3290, 1.0};
char * name = NULL;
oyStringAddPrintf( &name, oyAllocateFunc_, oyDeAllocateFunc_,
"sRGB Rec709 linear D65%s",
(icc_profile_flags & OY_ICC_VERSION_2) ? " v2" : " v4" );
prof = profileFromMatrix( primaries_and_gamma, name, icc_profile_flags );
oiio_msg( oyMSG_DBG, node,
OY_DBG_FORMAT_ "set %s",
OY_DBG_ARGS_, name);
oyDeAllocateFunc_( name );
}
}
/* fallback profile */
if(!prof)
prof = oyProfile_FromStd( profile_type, icc_profile_flags, 0 );
if(oy_debug)
oiio_msg( oyMSG_DBG, (oyStruct_s*)node,
OY_DBG_FORMAT_ "%dx%d %s|%s[%d] %s\n%s",
OY_DBG_ARGS_, spec.width, spec.height,
spec.format.c_str(), oyDataTypeToText(data_type), spec.nchannels,
image->format_name(),
spec.to_xml().c_str() );
/* create a Oyranos image */
image_in = oyImage_Create( spec.width, spec.height, buf, pixel_type, prof, 0 );
if (!image_in)
{
oiio_msg( oyMSG_WARN, (oyStruct_s*)node,
OY_DBG_FORMAT_ "can't create a new image\n%dx%d %s[%d]",
OY_DBG_ARGS_, spec.width, spec.height, spec.format.c_str(), spec.nchannels );
return FALSE;
}
/* remember the meta data like file name */
tags = oyImage_GetTags( image_in );
error = oyOptions_SetFromText( &tags,
"//" OY_TYPE_STD "/file_read.input_oiio"
"/filename",
filename, OY_CREATE_NEW );
for (size_t i = 0; i < spec.extra_attribs.size(); ++i)
{
const OpenImageIO::ParamValue &p (spec.extra_attribs[i]);