~ubuntu-branches/ubuntu/saucy/blender/saucy-proposed

« back to all changes in this revision

Viewing changes to source/blender/blenlib/intern/math_color_inline.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
149
149
        srgb[3] = FTOUSHORT(linear[3]);
150
150
}
151
151
 
152
 
MINLINE void linearrgb_to_srgb_ushort4_predivide(unsigned short srgb[4], const float linear[4])
153
 
{
154
 
        float alpha, inv_alpha, t;
155
 
        int i;
156
 
 
157
 
        if (linear[3] == 1.0f || linear[3] == 0.0f) {
158
 
                linearrgb_to_srgb_ushort4(srgb, linear);
159
 
                return;
160
 
        }
161
 
 
162
 
        alpha = linear[3];
163
 
        inv_alpha = 1.0f / alpha;
164
 
 
165
 
        for (i = 0; i < 3; ++i) {
166
 
                t = linear[i] * inv_alpha;
167
 
                srgb[i] = (t < 1.0f) ? (unsigned short) (to_srgb_table_lookup(t) * alpha) : FTOUSHORT(linearrgb_to_srgb(t) * alpha);
168
 
        }
169
 
 
170
 
        srgb[3] = FTOUSHORT(linear[3]);
171
 
}
172
 
 
173
152
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
174
153
{
175
154
        linear[0] = BLI_color_from_srgb_table[srgb[0]];
195
174
}
196
175
 
197
176
/* color macros for themes */
198
 
#define rgba_char_args_set_fl(col, r, g, b, a)  rgba_char_args_set(col, r * 255, g * 255, b * 255, a * 255)
 
177
#define rgba_char_args_set_fl(col, r, g, b, a) \
 
178
        rgba_char_args_set(col, (r) * 255, (g) * 255, (b) * 255, (a) * 255)
199
179
 
200
180
MINLINE void rgba_char_args_set(char col[4], const char r, const char g, const char b, const char a)
201
181
{
215
195
        }
216
196
}
217
197
 
 
198
MINLINE void cpack_cpy_3ub(unsigned char r_col[3], const unsigned int pack)
 
199
{
 
200
        r_col[0] = ((pack) >>  0) & 0xFF;
 
201
        r_col[1] = ((pack) >>  8) & 0xFF;
 
202
        r_col[2] = ((pack) >> 16) & 0xFF;
 
203
}
 
204
 
 
205
/* TODO:
 
206
 *
 
207
 * regarding #rgb_to_bw vs #rgb_to_grayscale,
 
208
 * it seems nobody knows why we have both functions which convert color to grays
 
209
 * but with different influences, this is quite stupid, and should be resolved
 
210
 * by someone who knows this stuff: see this thread
 
211
 * http://lists.blender.org/pipermail/bf-committers/2012-June/037180.html
 
212
 *
 
213
 * Only conclusion is that rgb_to_grayscale is used more for compositing.
 
214
 */
 
215
MINLINE float rgb_to_bw(const float rgb[3])
 
216
{
 
217
        return 0.35f * rgb[0] + 0.45f * rgb[1] + 0.2f * rgb[2];
 
218
}
 
219
 
 
220
/* non-linear luma from ITU-R BT.601-2
 
221
 * see: http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC11
 
222
 * note: the values used for are not exact matches to those documented above,
 
223
 * but they are from the same */
 
224
MINLINE float rgb_to_grayscale(const float rgb[3])
 
225
{
 
226
        return 0.3f * rgb[0] + 0.58f * rgb[1] + 0.12f * rgb[2];
 
227
}
 
228
 
 
229
MINLINE unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3])
 
230
{
 
231
        return (76 * (unsigned short) rgb[0] + 148 * (unsigned short) rgb[1] + 31 * (unsigned short) rgb[2]) / 255;
 
232
}
 
233
 
 
234
/* luma from defined by 'YCC_JFIF', see #rgb_to_ycc */
 
235
MINLINE float rgb_to_luma(const float rgb[3])
 
236
{
 
237
        return 0.299f * rgb[0] + 0.587f * rgb[1] + 0.114f * rgb[2];
 
238
}
 
239
 
 
240
MINLINE unsigned char rgb_to_luma_byte(const unsigned char rgb[3])
 
241
{
 
242
        return (76 * (unsigned short) rgb[0] + 150 * (unsigned short) rgb[1] + 29 * (unsigned short) rgb[2]) / 255;
 
243
}
 
244
 
 
245
/* gamma-corrected RGB --> CIE XYZ
 
246
 * for this function we only get the Y component
 
247
 * see: http://software.intel.com/sites/products/documentation/hpc/ipp/ippi/ippi_ch6/ch6_color_models.html
 
248
 *
 
249
 * also known as:
 
250
 * luminance rec. 709 */
 
251
MINLINE float rgb_to_luma_y(const float rgb[3])
 
252
{
 
253
        return 0.212671f * rgb[0] + 0.71516f * rgb[1] + 0.072169f * rgb[2];
 
254
}
 
255
 
 
256
MINLINE int compare_rgb_uchar(const unsigned char col_a[3], const unsigned char col_b[3], const int limit)
 
257
{
 
258
        int r = (int)col_a[0] - (int)col_b[0];
 
259
        if (ABS(r) < limit) {
 
260
                int g = (int)col_a[1] - (int)col_b[1];
 
261
                if (ABS(g) < limit) {
 
262
                        int b = (int)col_a[2] - (int)col_b[2];
 
263
                        if (ABS(b) < limit) {
 
264
                                return 1;
 
265
                        }
 
266
                }
 
267
        }
 
268
 
 
269
        return 0;
 
270
}
 
271
 
 
272
/**************** Alpha Transformations *****************/
 
273
 
 
274
MINLINE void premul_to_straight_v4_v4(float straight[4], const float premul[4])
 
275
{
 
276
        if (premul[3] == 0.0f || premul[3] == 1.0f) {
 
277
                straight[0] = premul[0];
 
278
                straight[1] = premul[1];
 
279
                straight[2] = premul[2];
 
280
                straight[3] = premul[3];
 
281
        }
 
282
        else {
 
283
                float alpha_inv = 1.0f / premul[3];
 
284
                straight[0] = premul[0] * alpha_inv;
 
285
                straight[1] = premul[1] * alpha_inv;
 
286
                straight[2] = premul[2] * alpha_inv;
 
287
                straight[3] = premul[3];
 
288
        }
 
289
}
 
290
 
 
291
MINLINE void premul_to_straight_v4(float color[4])
 
292
{
 
293
        premul_to_straight_v4_v4(color, color);
 
294
}
 
295
 
 
296
MINLINE void straight_to_premul_v4_v4(float premul[4], const float straight[4])
 
297
{
 
298
        float alpha = straight[3];
 
299
        premul[0] = straight[0] * alpha;
 
300
        premul[1] = straight[1] * alpha;
 
301
        premul[2] = straight[2] * alpha;
 
302
        premul[3] = straight[3];
 
303
}
 
304
 
 
305
MINLINE void straight_to_premul_v4(float color[4])
 
306
{
 
307
        straight_to_premul_v4_v4(color, color);
 
308
}
 
309
 
 
310
MINLINE void straight_uchar_to_premul_float(float result[4], const unsigned char color[4])
 
311
{
 
312
        float alpha = color[3] / 255.0f;
 
313
        float fac = alpha / 255.0f;
 
314
 
 
315
        result[0] = color[0] * fac;
 
316
        result[1] = color[1] * fac;
 
317
        result[2] = color[2] * fac;
 
318
        result[3] = alpha;
 
319
}
 
320
 
 
321
MINLINE void premul_float_to_straight_uchar(unsigned char *result, const float color[4])
 
322
{
 
323
        if (color[3] == 0.0f || color[3] == 1.0f) {
 
324
                result[0] = FTOCHAR(color[0]);
 
325
                result[1] = FTOCHAR(color[1]);
 
326
                result[2] = FTOCHAR(color[2]);
 
327
                result[3] = FTOCHAR(color[3]);
 
328
        }
 
329
        else {
 
330
                float alpha_inv = 1.0f / color[3];
 
331
 
 
332
                /* hopefully this would be optimized */
 
333
                result[0] = FTOCHAR(color[0] * alpha_inv);
 
334
                result[1] = FTOCHAR(color[1] * alpha_inv);
 
335
                result[2] = FTOCHAR(color[2] * alpha_inv);
 
336
                result[3] = FTOCHAR(color[3]);
 
337
        }
 
338
}
 
339
 
218
340
#endif /* __MATH_COLOR_INLINE_C__ */