File Coverage

blib/lib/PDL/Graphics/ColorSpace/RGBSpace.pm
Criterion Covered Total %
statement 26 26 100.0
branch 6 8 75.0
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 38 42 90.4


line stmt bran cond sub pod time code
1             package PDL::Graphics::ColorSpace::RGBSpace;
2              
3 1     1   7 use strict;
  1         6  
  1         43  
4 1     1   5 use warnings;
  1         2  
  1         58  
5 1     1   7 use PDL::LiteF;
  1         2  
  1         8  
6 1     1   2755 use Carp;
  1         2  
  1         1946  
7              
8             =head1 NAME
9              
10             PDL::Graphics::ColorSpace::RGBSpace -- defines RGB space conversion parameters and white points.
11              
12             =head1 DESCRIPTION
13              
14             Sourced from Graphics::ColorObject (Izvorski & Reibenschuh, 2005).
15              
16             =head1 Usage
17              
18             use Data::Dumper;
19             print Dumper $PDL::Graphics::ColorSpace::RGBSpace::RGB_SPACE;
20             print Dumper $PDL::Graphics::ColorSpace::RGBSpace::WHITE_POINT;
21              
22             =cut
23              
24             our $WHITE_POINT = {
25             'D50' => [
26             '0.34567',
27             '0.3585'
28             ],
29             'A' => [
30             '0.44757',
31             '0.40745'
32             ],
33             'D75' => [
34             '0.29902',
35             '0.31485'
36             ],
37             'D55' => [
38             '0.33242',
39             '0.34743'
40             ],
41             'D65' => [
42             '0.312713',
43             '0.329016'
44             ],
45             'E' => [
46             '0.333333',
47             '0.333333'
48             ],
49             'B' => [
50             '0.34842',
51             '0.35161'
52             ],
53             'F11' => [
54             '0.38054',
55             '0.37691'
56             ],
57             'F2' => [
58             '0.37207',
59             '0.37512'
60             ],
61             'C' => [
62             '0.310063',
63             '0.316158'
64             ],
65             'D93' => [
66             '0.2848',
67             '0.2932'
68             ],
69             'F7' => [
70             '0.31285',
71             '0.32918'
72             ]
73             };
74              
75             our $RGB_SPACE = {
76             'BruceRGB' => {
77             'gamma' => '2.2',
78             'm' => pdl([
79             [
80             '0.467384242424242',
81             '0.240995',
82             '0.0219086363636363'
83             ],
84             [
85             '0.294454030769231',
86             '0.683554',
87             '0.0736135076923076'
88             ],
89             [
90             '0.18863',
91             '0.075452',
92             '0.993451333333334'
93             ]
94             ]),
95             'white_point' => $WHITE_POINT->{'D65'},
96             },
97             'Adobe RGB (1998)' => {
98             'gamma' => '2.2',
99             'm' => pdl([
100             [
101             '0.576700121212121',
102             '0.297361',
103             '0.0270328181818181'
104             ],
105             [
106             '0.185555704225352',
107             '0.627355',
108             '0.0706878873239437'
109             ],
110             [
111             '0.1882125',
112             '0.075285',
113             '0.9912525'
114             ]
115             ]),
116             'white_point' => $WHITE_POINT->{'D65'},
117             },
118             'WideGamut' => {
119             'gamma' => '2.2',
120             'm' => pdl([
121             [
122             '0.716103566037736',
123             '0.258187',
124             '0'
125             ],
126             [
127             '0.100929624697337',
128             '0.724938',
129             '0.0517812857142858'
130             ],
131             [
132             '0.1471875',
133             '0.016875',
134             '0.7734375'
135             ]
136             ]),
137             'white_point' => $WHITE_POINT->{'D50'},
138             },
139             'NTSC' => {
140             'gamma' => '2.2',
141             'm' => pdl([
142             [
143             '0.606733727272727',
144             '0.298839',
145             '-1e-16'
146             ],
147             [
148             '0.173563816901409',
149             '0.586811',
150             '0.0661195492957747'
151             ],
152             [
153             '0.2001125',
154             '0.11435',
155             '1.1149125'
156             ]
157             ]),
158             'white_point' => $WHITE_POINT->{'C'},
159             },
160             'Ekta Space PS5' => {
161             'gamma' => '2.2',
162             'm' => pdl([
163             [
164             '0.59389231147541',
165             '0.260629',
166             '0'
167             ],
168             [
169             '0.272979942857143',
170             '0.734946',
171             '0.0419969142857143'
172             ],
173             [
174             '0.09735',
175             '0.004425',
176             '0.783225'
177             ]
178             ]),
179             'white_point' => $WHITE_POINT->{'D50'},
180             },
181             'PAL/SECAM' => {
182             'gamma' => '2.2',
183             'm' => pdl([
184             [
185             '0.430586181818182',
186             '0.222021',
187             '0.0201837272727273'
188             ],
189             [
190             '0.341545083333333',
191             '0.706645',
192             '0.129551583333333'
193             ],
194             [
195             '0.178335',
196             '0.071334',
197             '0.939231'
198             ]
199             ]),
200             'white_point' => $WHITE_POINT->{'D65'},
201             },
202             'Apple RGB' => {
203             'gamma' => '1.8',
204             'm' => pdl([
205             [
206             '0.449694852941176',
207             '0.244634',
208             '0.0251829117647059'
209             ],
210             [
211             '0.316251294117647',
212             '0.672034',
213             '0.141183613445378'
214             ],
215             [
216             '0.184520857142857',
217             '0.083332',
218             '0.922604285714286'
219             ]
220             ]),
221             'white_point' => $WHITE_POINT->{'D65'},
222             },
223             'sRGB' => {
224             'gamma' => '-1', # mark it for special case
225             'm' => pdl([
226             [
227             '0.412423757575757',
228             '0.212656',
229             '0.0193323636363636'
230             ],
231             [
232             '0.357579',
233             '0.715158',
234             '0.119193'
235             ],
236             [
237             '0.180465',
238             '0.072186',
239             '0.950449'
240             ]
241             ]),
242             'white_point' => $WHITE_POINT->{'D65'},
243             },
244             'lsRGB' => {
245             'gamma' => 1.0,
246             'm' => pdl([
247             [
248             '0.412423757575757',
249             '0.212656',
250             '0.0193323636363636'
251             ],
252             [
253             '0.357579',
254             '0.715158',
255             '0.119193'
256             ],
257             [
258             '0.180465',
259             '0.072186',
260             '0.950449'
261             ]
262             ]),
263             'white_point' => $WHITE_POINT->{'D65'},
264             },
265             'ColorMatch' => {
266             'gamma' => '1.8',
267             'm' => pdl([
268             [
269             '0.509343882352941',
270             '0.274884',
271             '0.0242544705882353'
272             ],
273             [
274             '0.320907338842975',
275             '0.658132',
276             '0.108782148760331'
277             ],
278             [
279             '0.13397',
280             '0.066985',
281             '0.692178333333333'
282             ]
283             ]),
284             'white_point' => $WHITE_POINT->{'D50'},
285             },
286             'SMPTE-C' => {
287             'gamma' => '2.2',
288             'm' => pdl([
289             [
290             '0.393555441176471',
291             '0.212395',
292             '0.0187407352941176'
293             ],
294             [
295             '0.365252420168067',
296             '0.701049',
297             '0.111932193277311'
298             ],
299             [
300             '0.191659714285714',
301             '0.086556',
302             '0.958298571428571'
303             ]
304             ]),
305             'white_point' => $WHITE_POINT->{'D65'},
306             },
307             'CIE' => {
308             'gamma' => '2.2',
309             'm' => pdl([
310             [
311             '0.488716754716981',
312             '0.176204',
313             '0'
314             ],
315             [
316             '0.310680460251046',
317             '0.812985',
318             '0.0102048326359833'
319             ],
320             [
321             '0.200604111111111',
322             '0.010811',
323             '0.989807111111111'
324             ]
325             ]),
326             'white_point' => $WHITE_POINT->{'E'},
327             },
328             'ProPhoto' => {
329             'gamma' => '1.8',
330             'm' => pdl([
331             [
332             '0.797674285714286',
333             '0.28804',
334             '0'
335             ],
336             [
337             '0.135191683008091',
338             '0.711874',
339             '0'
340             ],
341             [
342             '0.031476',
343             '8.6e-05',
344             '0.828438'
345             ]
346             ]),
347             'white_point' => $WHITE_POINT->{'D50'},
348             },
349             'BestRGB' => {
350             'gamma' => '2.2',
351             'm' => pdl([
352             [
353             '0.632670026008293',
354             '0.228457',
355             '0'
356             ],
357             [
358             '0.204555716129032',
359             '0.737352',
360             '0.0095142193548387'
361             ],
362             [
363             '0.126995142857143',
364             '0.034191',
365             '0.815699571428571'
366             ]
367             ]),
368             'white_point' => $WHITE_POINT->{'D50'},
369             },
370             'DonRGB4' => {
371             'gamma' => '2.2',
372             'm' => pdl([
373             [
374             '0.645772',
375             '0.27835',
376             '0.0037113333333334'
377             ],
378             [
379             '0.193351045751634',
380             '0.68797',
381             '0.0179861437908497'
382             ],
383             [
384             '0.125097142857143',
385             '0.03368',
386             '0.803508571428572'
387             ]
388             ]),
389             'white_point' => $WHITE_POINT->{'D50'},
390             },
391             'Beta RGB' => {
392             'gamma' => '2.2',
393             'm' => pdl([
394             [
395             '0.67125463496144',
396             '0.303273',
397             '1e-16'
398             ],
399             [
400             '0.1745833659118',
401             '0.663786',
402             '0.0407009558998808'
403             ],
404             [
405             '0.11838171875',
406             '0.032941',
407             '0.784501144886363'
408             ]
409             ]),
410             'white_point' => $WHITE_POINT->{'D50'},
411             },
412             'ECI' => {
413             'gamma' => '1.8',
414             'm' => pdl([
415             [
416             '0.650204545454545',
417             '0.32025',
418             '-1e-16'
419             ],
420             [
421             '0.178077338028169',
422             '0.602071',
423             '0.067838985915493'
424             ],
425             [
426             '0.13593825',
427             '0.077679',
428             '0.75737025'
429             ]
430             ]),
431             'white_point' => $WHITE_POINT->{'D50'},
432             },
433             };
434             $RGB_SPACE->{$_}{white_point} = PDL->topdl($RGB_SPACE->{$_}{white_point}),
435             $RGB_SPACE->{$_}{mstar} = $RGB_SPACE->{$_}{m}->inv
436             for keys %$RGB_SPACE;
437              
438             # aliases
439             $RGB_SPACE->{Adobe} = $RGB_SPACE->{'Adobe RGB (1998)'};
440             $RGB_SPACE->{'601'} = $RGB_SPACE->{NTSC};
441             $RGB_SPACE->{Apple} = $RGB_SPACE->{'Apple RGB'};
442             $RGB_SPACE->{'CIE ITU'} = $RGB_SPACE->{'PAL/SECAM'};
443             $RGB_SPACE->{PAL} = $RGB_SPACE->{'PAL/SECAM'};
444             $RGB_SPACE->{'709'} = $RGB_SPACE->{sRGB};
445             $RGB_SPACE->{SMPTE} = $RGB_SPACE->{'SMPTE-C'};
446             $RGB_SPACE->{'CIE Rec 709'} = $RGB_SPACE->{sRGB};
447             $RGB_SPACE->{'CIE Rec 601'} = $RGB_SPACE->{NTSC};
448              
449             my @NEED_KEYS = grep $_ ne 'mstar', keys %{ $RGB_SPACE->{sRGB} };
450             sub add_rgb_space {
451 2     2 0 1116 my ($new_space) = @_;
452 2         21 my @dup = grep $RGB_SPACE->{$_}, sort keys %$new_space;
453 2 100       162 croak "Already existing RGB space definition with names @dup" if @dup;
454 1         4 while (my ($name, $profile) = each %$new_space) {
455             carp "Missing definition for custom RGB space $name: $_"
456 1         5 for grep !defined $profile->{$_}, @NEED_KEYS;
457 1         3 my $copy = {%$profile};
458 1         6 $copy->{m} = PDL->topdl($copy->{m});
459 1 50       123 $copy->{mstar} = $copy->{m}->inv if !exists $copy->{mstar};
460 1         2332 $copy->{mstar} = PDL->topdl($copy->{mstar});
461 1         5 $copy->{white_point} = PDL->topdl($copy->{white_point});
462 1         42 $RGB_SPACE->{$name} = $copy;
463             }
464             }
465              
466             sub get_space {
467 40     40 0 58 my ($space) = @_;
468 40 50       69 croak "Please specify RGB Space ('sRGB' for generic JPEG images)!"
469             if !$space;
470 40 100       95 my $spec = ref($space) ? $space : $RGB_SPACE->{$space};
471             }
472              
473             1;