summaryrefslogtreecommitdiff
path: root/magick/cdl.c
blob: e33f11b2998b6fe3f0630b1d1249d6de24bacfcf (plain)
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
/*
 * Copyright (C) 2009 GraphicsMagick Group
 *
 * American Society of Cinematographers Color Decision List (ASC-CDL)
 * implementation.
 *
 * Original implementation by Clément Follet.  Additional work by Bob
 * Friesenhahn.
 */

/*
  Include declarations.
*/
#include "magick/studio.h"
#include "magick/cdl.h"
#include "magick/pixel_iterator.h"
#include "magick/utility.h"

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                             %
%                                                                             %
%     C d l I m a g e                                                         %
%                                                                             %
%                                                                             %
%                                                                             %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  The CdlImage() method applies ("bakes in") the ASC-CDL which is a format
%  for the exchange of basic primary color grading information between
%  equipment and software from different manufacturers. The format defines
%  the math for three functions: slope, offset and power. Each function uses
%  a number for the red, green, and blue color channels for a total of nine
%  numbers comprising a single color decision. A tenth number for chrominance
%  (saturation) has been proposed but is not yet standardized.
%
%  The cdl argument string is comma delimited and is in the form (but
%  without invervening spaces or line breaks):
%
%    redslope, redoffset, redpower :
%    greenslope, greenoffset, greenpower :
%    blueslope, blueoffset, bluepower :
%    saturation
%
%  with the unity (no change) specification being:
%
%    "1.0,0.0,1.0:1.0,0.0,1.0:1.0,0.0,1.0:0.0"
%
%  See http://en.wikipedia.org/wiki/ASC_CDL for more information.
%
%  The format of the CdlImage method is:
%
%      MagickPassFail CdlImage(Image *image,const char *cdl)
%
%  A description of each parameter follows:
%
%    o image: The image.
%
%    o cdl: Define the coefficients for slope offset and power in the
%      red green and blue channels, plus saturation.
%
*/

typedef struct _CdlImageParameters_t
{
  double
    redslope,
    redoffset,
    redpower,
    greenslope,
    greenoffset,
    greenpower,
    blueslope,
    blueoffset,
    bluepower,
    saturation;

  const PixelPacket
    *lut;

} CdlImageParameters_t;

static Quantum
CdlQuantum(const Quantum quantum, const double slope, const double offset,
           const double power, const double saturation)
{
  double
    v,
    t;

  t=(((double) quantum)/MaxRGBDouble)*slope+offset;
  if (t < 0.0)
    t = 0.0;
  else if (t > 1.0)
    t = 1.0;
  v = (pow(t,power)+saturation)*MaxRGBDouble;
  return RoundDoubleToQuantum(v);
}

static MagickPassFail
CdlImagePixels(void *mutable_data,         /* User provided mutable data */
               const void *immutable_data, /* User provided immutable data */
               Image * restrict image,               /* Modify image */
               PixelPacket * restrict pixels,        /* Pixel row */
               IndexPacket * restrict indexes,       /* Pixel row indexes */
               const long npixels,         /* Number of pixels in row */
               ExceptionInfo *exception)   /* Exception report */
{
  const CdlImageParameters_t
    param = *(const CdlImageParameters_t *) immutable_data;

  register long
    i;

  ARG_NOT_USED(mutable_data);
  ARG_NOT_USED(image);
  ARG_NOT_USED(indexes);
  ARG_NOT_USED(exception);

  if (param.lut != (PixelPacket *) NULL)
    {
      for(i = 0; i < npixels; i++)
        {
          pixels[i].red=param.lut[pixels[i].red].red;
          pixels[i].green=param.lut[pixels[i].green].green;
          pixels[i].blue=param.lut[pixels[i].blue].blue;
        }
    }
  else
    {
      for(i = 0; i < npixels; i++)
        {
          pixels[i].red=CdlQuantum(pixels[i].red,param.redslope,param.redoffset,
                                   param.redpower,param.saturation);
          pixels[i].green=CdlQuantum(pixels[i].green,param.greenslope,param.greenoffset,
                                     param.greenpower,param.saturation);
          pixels[i].blue=CdlQuantum(pixels[i].blue,param.blueslope,param.blueoffset,
                                    param.bluepower,param.saturation);
        }
    }

  return MagickPass;
}


MagickExport MagickPassFail CdlImage(Image *image,const char *cdl)
{
  char
    progress_message[MaxTextExtent];

  CdlImageParameters_t
    param;

  PixelPacket
    *lut = (PixelPacket *) NULL;

  register long
    i;

  MagickPassFail
    status=MagickPass;

  assert(image != (Image *) NULL);
  assert(image->signature == MagickSignature);
  if (cdl == (char *) NULL)
    return(MagickFail);

  param.redslope=1.0;
  param.redoffset=0.0;
  param.redpower=1.0;
  param.greenslope=1.0;
  param.greenoffset=0.0;
  param.greenpower=1.0;
  param.blueslope=1.0;
  param.blueoffset=0.0;
  param.bluepower=1.0;
  param.saturation=0.0;
  param.lut=(PixelPacket *) NULL;

  (void) sscanf(cdl,
                "%lf%*[,/]%lf%*[,/]%lf%*[:/]%lf%*[,/]%lf%*[,/]%lf%*"
                "[:/]%lf%*[,/]%lf%*[,/]%lf%*[:/]%lf",
                &param.redslope,&param.redoffset,&param.redpower,
                &param.greenslope,&param.greenoffset,&param.greenpower
                ,&param.blueslope,&param.blueoffset,&param.bluepower,
                &param.saturation);

  param.redslope=AbsoluteValue(param.redslope);
  param.redpower=AbsoluteValue(param.redpower);
  param.greenslope=AbsoluteValue(param.greenslope);
  param.greenpower=AbsoluteValue(param.greenpower);
  param.blueslope=AbsoluteValue(param.blueslope);
  param.bluepower=AbsoluteValue(param.bluepower);

  FormatString(progress_message,
               "[%%s] cdl %g/%g/%g/%g/%g/%g/%g/%g/%g/%g image...",
               param.redslope,param.redoffset,param.redpower,
               param.greenslope,param.greenoffset,param.greenpower,
               param.blueslope,param.blueoffset,param.bluepower,
               param.saturation);

  if (!IsRGBCompatibleColorspace(image->colorspace))
    TransformColorspace(image,RGBColorspace);

  /*
    Build a LUT if it is beneficial to do so.
  */
  if ((MaxMap == MaxRGB) && (image->columns*image->rows > MaxMap*3))
    {
      lut=MagickAllocateMemory(PixelPacket *,(MaxMap+1)*sizeof(PixelPacket));
      if (lut != (PixelPacket *) NULL)
        {
#if (MaxMap > 256) && defined(HAVE_OPENMP)
#  if defined(USE_STATIC_SCHEDULING_ONLY)
#    pragma omp parallel for schedule(static,4)
#  else
#    pragma omp parallel for schedule(guided)
#  endif
#endif
          for (i=0; i <= (long) MaxMap; i++)
            {
              lut[i].red=CdlQuantum((Quantum) i,param.redslope,param.redoffset,
                                       param.redpower,param.saturation);
              lut[i].green=CdlQuantum((Quantum) i,param.greenslope,param.greenoffset,
                                         param.greenpower,param.saturation);
              lut[i].blue=CdlQuantum((Quantum) i,param.blueslope,param.blueoffset,
                                        param.bluepower,param.saturation);
            }
          param.lut=lut;
        }
    }

  if (image->storage_class == PseudoClass)
    {
      (void) CdlImagePixels(NULL,&param,image,image->colormap,
                            (IndexPacket *) NULL,image->colors,
                            &image->exception);
      status=SyncImage(image);
    }
  else
    {
      status=PixelIterateMonoModify(CdlImagePixels,NULL,progress_message,
                                    NULL,&param,0,0,image->columns,image->rows,
                                    image,&image->exception);
    }

  MagickFreeMemory(lut);

  return(status);
}