Coverage Report

Created: 2021-08-22 09:07

/src/skia/third_party/externals/dng_sdk/source/dng_hue_sat_map.cpp
Line
Count
Source (jump to first uncovered line)
1
/*****************************************************************************/
2
// Copyright 2007 Adobe Systems Incorporated
3
// All Rights Reserved.
4
//
5
// NOTICE:  Adobe permits you to use, modify, and distribute this file in
6
// accordance with the terms of the Adobe license agreement accompanying it.
7
/*****************************************************************************/
8
9
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_hue_sat_map.cpp#1 $ */ 
10
/* $DateTime: 2012/05/30 13:28:51 $ */
11
/* $Change: 832332 $ */
12
/* $Author: tknoll $ */
13
14
/*****************************************************************************/
15
16
#include "dng_hue_sat_map.h"
17
18
#include "dng_assertions.h"
19
#include "dng_auto_ptr.h"
20
#include "dng_bottlenecks.h"
21
#include "dng_exceptions.h"
22
#include "dng_host.h"
23
24
/*****************************************************************************/
25
26
dng_hue_sat_map::dng_hue_sat_map ()
27
28
  : fHueDivisions (0)
29
  , fSatDivisions (0)
30
  , fValDivisions (0)
31
  , fHueStep      (0)
32
  , fValStep    (0)
33
  , fDeltas       ()
34
  
35
0
  {
36
  
37
0
  }
38
39
/*****************************************************************************/
40
41
dng_hue_sat_map::dng_hue_sat_map (const dng_hue_sat_map &src)
42
43
  : fHueDivisions (0)
44
  , fSatDivisions (0)
45
  , fValDivisions (0)
46
  , fHueStep      (0)
47
  , fValStep    (0)
48
  , fDeltas       ()
49
50
0
  {
51
  
52
0
  *this = src;
53
  
54
0
  }
55
56
/*****************************************************************************/
57
58
dng_hue_sat_map &dng_hue_sat_map::operator= (const dng_hue_sat_map &rhs)
59
0
  { 
60
61
0
  if (this != &rhs)
62
0
    {
63
64
0
    if (!rhs.IsValid ())
65
0
      {
66
      
67
0
      SetInvalid ();
68
      
69
0
      }
70
      
71
0
    else
72
0
      {
73
74
0
      fHueDivisions = rhs.fHueDivisions;
75
0
      fSatDivisions = rhs.fSatDivisions;
76
0
      fValDivisions = rhs.fValDivisions;
77
78
0
      fHueStep = rhs.fHueStep;
79
0
      fValStep = rhs.fValStep;
80
81
0
      fDeltas = rhs.fDeltas;
82
        
83
0
      }
84
85
0
    }
86
87
0
  return *this;
88
89
0
  }
90
91
/*****************************************************************************/
92
93
dng_hue_sat_map::~dng_hue_sat_map ()
94
0
  {
95
  
96
0
  }
97
98
/*****************************************************************************/
99
100
void dng_hue_sat_map::SetDivisions (uint32 hueDivisions,
101
                  uint32 satDivisions,
102
                  uint32 valDivisions)
103
0
  {
104
105
0
  DNG_ASSERT (hueDivisions >= 1, "Must have at least 1 hue division.");
106
0
  DNG_ASSERT (satDivisions >= 2, "Must have at least 2 sat divisions.");
107
  
108
0
  if (valDivisions == 0)
109
0
    valDivisions = 1;
110
111
0
  if (hueDivisions == fHueDivisions &&
112
0
    satDivisions == fSatDivisions &&
113
0
    valDivisions == fValDivisions)
114
0
    {
115
0
    return;
116
0
    }
117
118
0
  fHueDivisions = hueDivisions;
119
0
  fSatDivisions = satDivisions;
120
0
  fValDivisions = valDivisions;
121
  
122
0
  fHueStep = satDivisions;
123
0
  fValStep = SafeUint32Mult(hueDivisions, fHueStep);
124
125
0
  uint32 size = SafeUint32Mult(DeltasCount (), (uint32) sizeof (HSBModify));
126
  
127
0
  fDeltas.Allocate (size);
128
  
129
0
  DoZeroBytes (fDeltas.Buffer (), size);
130
131
0
  }
132
133
/*****************************************************************************/
134
135
void dng_hue_sat_map::GetDelta (uint32 hueDiv,
136
                uint32 satDiv,
137
                uint32 valDiv,
138
                HSBModify &modify) const
139
0
  {
140
141
0
  if (hueDiv >= fHueDivisions ||
142
0
    satDiv >= fSatDivisions ||
143
0
    valDiv >= fValDivisions ||
144
0
    fDeltas.Buffer () == NULL)
145
0
    {
146
    
147
0
    DNG_REPORT ("Bad parameters to dng_hue_sat_map::GetDelta");
148
    
149
0
    ThrowProgramError ();
150
    
151
0
    }
152
153
0
  int32 offset = valDiv * fValStep +
154
0
           hueDiv * fHueStep +
155
0
           satDiv;
156
157
0
  const HSBModify *deltas = GetConstDeltas ();
158
159
0
  modify.fHueShift = deltas [offset].fHueShift;
160
0
  modify.fSatScale = deltas [offset].fSatScale;
161
0
  modify.fValScale = deltas [offset].fValScale;
162
163
0
  }
164
165
/*****************************************************************************/
166
167
void dng_hue_sat_map::SetDeltaKnownWriteable (uint32 hueDiv,
168
                        uint32 satDiv,
169
                        uint32 valDiv,
170
                        const HSBModify &modify)
171
0
  {
172
173
0
  if (hueDiv >= fHueDivisions ||
174
0
    satDiv >= fSatDivisions ||
175
0
    valDiv >= fValDivisions ||
176
0
    fDeltas.Buffer () == NULL)
177
0
    {
178
    
179
0
    DNG_REPORT ("Bad parameters to dng_hue_sat_map::SetDelta");
180
    
181
0
    ThrowProgramError ();
182
    
183
0
    }
184
    
185
  // Set this entry.
186
    
187
0
  int32 offset = valDiv * fValStep +
188
0
           hueDiv * fHueStep +
189
0
           satDiv;
190
191
0
  SafeGetDeltas () [offset] = modify;
192
  
193
  // The zero saturation entry is required to have a value scale
194
  // of 1.0f.
195
  
196
0
  if (satDiv == 0)
197
0
    {
198
    
199
0
    if (modify.fValScale != 1.0f)
200
0
      {
201
      
202
      #if qDNGValidate
203
    
204
      ReportWarning ("Value scale for zero saturation entries must be 1.0");
205
             
206
      #endif
207
      
208
0
      SafeGetDeltas () [offset] . fValScale = 1.0f;
209
    
210
0
      }
211
    
212
0
    }
213
    
214
  // If we are settings the first saturation entry and we have not
215
  // set the zero saturation entry yet, fill in the zero saturation entry
216
  // by extrapolating first saturation entry.
217
  
218
0
  if (satDiv == 1)
219
0
    {
220
    
221
0
    HSBModify zeroSatModify;
222
    
223
0
    GetDelta (hueDiv, 0, valDiv, zeroSatModify);
224
    
225
0
    if (zeroSatModify.fValScale != 1.0f)
226
0
      {
227
      
228
0
      zeroSatModify.fHueShift = modify.fHueShift;
229
0
      zeroSatModify.fSatScale = modify.fSatScale;
230
0
      zeroSatModify.fValScale = 1.0f;
231
      
232
0
      SetDelta (hueDiv, 0, valDiv, zeroSatModify);
233
      
234
0
      }
235
    
236
0
    }
237
238
0
  }
239
240
/*****************************************************************************/
241
242
bool dng_hue_sat_map::operator== (const dng_hue_sat_map &rhs) const
243
0
  {
244
  
245
0
  if (fHueDivisions != rhs.fHueDivisions ||
246
0
    fSatDivisions != rhs.fSatDivisions ||
247
0
    fValDivisions != rhs.fValDivisions)
248
0
    return false;
249
250
0
  if (!IsValid ())
251
0
    return true;
252
253
0
  return memcmp (GetConstDeltas (),
254
0
           rhs.GetConstDeltas (),
255
0
           DeltasCount () * sizeof (HSBModify)) == 0;
256
257
0
  }
258
259
/*****************************************************************************/
260
261
dng_hue_sat_map * dng_hue_sat_map::Interpolate (const dng_hue_sat_map &map1,
262
                          const dng_hue_sat_map &map2,
263
                          real64 weight1)
264
0
  {
265
  
266
0
  if (weight1 >= 1.0)
267
0
    {
268
    
269
0
    if (!map1.IsValid ())
270
0
      {
271
      
272
0
      DNG_REPORT ("map1 is not valid");
273
      
274
0
      ThrowProgramError ();
275
      
276
0
      }
277
      
278
0
    return new dng_hue_sat_map (map1);
279
    
280
0
    }
281
    
282
0
  if (weight1 <= 0.0)
283
0
    {
284
    
285
0
    if (!map2.IsValid ())
286
0
      {     
287
0
      DNG_REPORT ("map2 is not valid");
288
      
289
0
      ThrowProgramError ();
290
      
291
0
      }
292
      
293
0
    return new dng_hue_sat_map (map2);
294
    
295
0
    }
296
    
297
  // Both maps must be valid if we are using both.
298
  
299
0
  if (!map1.IsValid () || !map2.IsValid ())
300
0
    {
301
      
302
0
    DNG_REPORT ("map1 or map2 is not valid");
303
    
304
0
    ThrowProgramError ();
305
    
306
0
    }
307
    
308
  // Must have the same dimensions.
309
  
310
0
  if (map1.fHueDivisions != map2.fHueDivisions ||
311
0
    map1.fSatDivisions != map2.fSatDivisions ||
312
0
    map1.fValDivisions != map2.fValDivisions)
313
0
    {
314
    
315
0
    DNG_REPORT ("map1 and map2 have different sizes");
316
    
317
0
    ThrowProgramError ();
318
    
319
0
    }
320
    
321
  // Make table to hold interpolated results.
322
  
323
0
  AutoPtr<dng_hue_sat_map> result (new dng_hue_sat_map);
324
  
325
0
  result->SetDivisions (map1.fHueDivisions,
326
0
              map1.fSatDivisions,
327
0
              map1.fValDivisions);
328
              
329
  // Interpolate between the tables.
330
  
331
0
  real32 w1 = (real32) weight1;
332
0
  real32 w2 = 1.0f - w1;
333
  
334
0
  const HSBModify *data1 = map1.GetConstDeltas ();
335
0
  const HSBModify *data2 = map2.GetConstDeltas ();
336
  
337
0
  HSBModify *data3 = result->SafeGetDeltas ();
338
  
339
0
  uint32 count = map1.DeltasCount ();
340
  
341
0
  for (uint32 index = 0; index < count; index++)
342
0
    {
343
    
344
0
    data3->fHueShift = w1 * data1->fHueShift +
345
0
               w2 * data2->fHueShift;
346
               
347
0
    data3->fSatScale = w1 * data1->fSatScale +
348
0
               w2 * data2->fSatScale;
349
               
350
0
    data3->fValScale = w1 * data1->fValScale +
351
0
               w2 * data2->fValScale;
352
               
353
0
    data1++;
354
0
    data2++;
355
0
    data3++;
356
    
357
0
    }
358
    
359
  // Return interpolated tables.
360
  
361
0
  return result.Release ();
362
    
363
0
  }
364
365
/*****************************************************************************/