diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2011-12-28 22:31:32 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2011-12-28 22:31:32 +0400 |
commit | c2ae77e5bdd60e4cfe9b1f9d3d54e66f8089245c (patch) | |
tree | 505b5270122ec50302f6f824088b43318d13a296 /source/blender/blenlib | |
parent | fd134927dd23047237c6e490c09454487ba19a4a (diff) | |
parent | fe2131367b3f1ca1f974d1d6160cfc8a1a220abe (diff) |
Merging r42896 through r42944 from trunk into soc-2911-tomato
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_array.h | 185 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_edgehash.h | 11 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_color.h | 29 | ||||
-rw-r--r-- | source/blender/blenlib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/edgehash.c | 185 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color.c | 51 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color_inline.c | 109 |
7 files changed, 416 insertions, 155 deletions
diff --git a/source/blender/blenlib/BLI_array.h b/source/blender/blenlib/BLI_array.h new file mode 100644 index 00000000000..bd14793e0f9 --- /dev/null +++ b/source/blender/blenlib/BLI_array.h @@ -0,0 +1,185 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * The Original Code is Copyright (C) 2008 Blender Foundation. + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): Joseph Eagar. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/* + * this library needs to be changed to not use macros quite so heavily, + * and to be more of a complete array API. The way arrays are + * exposed to client code as normal C arrays is very useful though, imho. + * it does require some use of macros, however. + * + * anyway, it's used a bit too heavily to simply rewrite as a + * more "correct" solution without macros entirely. I originally wrote this + * to be very easy to use, without the normal pain of most array libraries. + * This was especially helpful when it came to the massive refactors necessary + * for bmesh, and really helped to speed the process up. - joeedh + * + * little array macro library. example of usage: + * + * int *arr = NULL; + * BLI_array_declare(arr); + * int i; + * + * for (i=0; i<10; i++) { + * BLI_array_growone(arr); + * arr[i] = something; + * } + * BLI_array_free(arr); + * + * arrays are buffered, using double-buffering (so on each reallocation, + * the array size is doubled). supposedly this should give good Big Oh + * behaviour, though it may not be the best in practice. + */ + +#define BLI_array_declare(arr) \ + int _##arr##_count = 0; \ + void *_##arr##_tmp; \ + void *_##arr##_static = NULL + +/* this will use stack space, up to maxstatic array elements, before + * switching to dynamic heap allocation */ +#define BLI_array_staticdeclare(arr, maxstatic) \ + int _##arr##_count = 0; \ + void *_##arr##_tmp; \ + char _##arr##_static[maxstatic*sizeof(arr)] + + +/* this returns the entire size of the array, including any buffering. */ +#define BLI_array_totalsize_dyn(arr) ( \ + ((arr)==NULL) ? \ + 0 : \ + MEM_allocN_len(arr) / sizeof(*arr) \ +) + + +#define BLI_array_totalsize(arr) ( \ + (size_t) \ + (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ? \ + (sizeof(_##arr##_static) / sizeof(*arr)) : \ + BLI_array_totalsize_dyn(arr)) \ +) + + +/* this returns the logical size of the array, not including buffering. */ +#define BLI_array_count(arr) _##arr##_count + +/* Grow the array by a fixed number of items. zeroes the new elements. + * + * Allow for a large 'num' value when the new size is more then double + * to allocate the exact sized array. */ +#define _bli_array_grow_items(arr, num) ( \ + (BLI_array_totalsize(arr) >= _##arr##_count + num) ? \ + (_##arr##_count += num) : \ + ( \ + (void) (_##arr##_tmp = MEM_callocN( \ + sizeof(*arr) * (num < _##arr##_count ? \ + (_##arr##_count * 2 + 2) : \ + (_##arr##_count + num)), \ + #arr " " __FILE__ ":" STRINGIFY(__LINE__) \ + ) \ + ), \ + (void) (arr && memcpy(_##arr##_tmp, \ + arr, \ + sizeof(*arr) * _##arr##_count) \ + ), \ + (void) (arr && ((void *)(arr) != (void*)_##arr##_static ? \ + (MEM_freeN(arr), arr) : \ + arr) \ + ), \ + (void) (arr = _##arr##_tmp \ + ), \ + (_##arr##_count += num) \ + ) \ +) + +/* grow an array by a specified number of items */ +#define BLI_array_growitems(arr, num) ( \ + ((void *)(arr)==NULL && (void *)(_##arr##_static) != NULL) ? \ + ((arr= (void*)_##arr##_static), (_##arr##_count += num)) : \ + _bli_array_grow_items(arr, num) \ +) + +/* returns length of array */ +#define BLI_array_growone(arr) BLI_array_growitems(arr, 1) + + +/* appends an item to the array. */ +#define BLI_array_append(arr, item) ( \ + (void) BLI_array_growone(arr), \ + (void) (arr[_##arr##_count - 1] = item) \ +) + +/* appends an item to the array and returns a pointer to the item in the array. + * item is not a pointer, but actual data value.*/ +#define BLI_array_append_r(arr, item) ( \ + (void) BLI_array_growone(arr), \ + (void) (arr[_##arr##_count - 1] = item), \ + (&arr[_##arr##_count - 1]) \ +) + +#define BLI_array_free(arr) \ + if (arr && (char *)arr != _##arr##_static) { \ + BLI_array_fake_user(arr); \ + MEM_freeN(arr); \ + } + +#define BLI_array_pop(arr) ( \ + (arr&&_##arr##_count) ? \ + arr[--_##arr##_count] : \ + 0 \ +) + +/* resets the logical size of an array to zero, but doesn't + * free the memory. */ +#define BLI_array_empty(arr) \ + _##arr##_count=0 + +/* set the count of the array, doesn't actually increase the allocated array + * size. don't use this unless you know what you're doing. */ +#define BLI_array_set_length(arr, count) \ + _##arr##_count = (count) + +/* only to prevent unused warnings */ +#define BLI_array_fake_user(arr) \ + (void)_##arr##_count, \ + (void)_##arr##_tmp, \ + (void)_##arr##_static + + +/* not part of the 'API' but handy funcs, + * same purpose as BLI_array_staticdeclare() + * but use when the max size is known ahead of time */ +#define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr) \ + char _##arr##_static[maxstatic*sizeof(*arr)]; \ + const int _##arr##_is_static= ((void *)_##arr##_static) != ( \ + arr= (realsize <= maxstatic) ? \ + (void *)_##arr##_static : \ + MEM_mallocN(sizeof(*arr)*realsize, allocstr) \ + ) \ + +#define BLI_array_fixedstack_free(arr) \ + if (_##arr##_is_static) MEM_freeN(arr) \ + diff --git a/source/blender/blenlib/BLI_edgehash.h b/source/blender/blenlib/BLI_edgehash.h index 283b52e06b9..9153155e359 100644 --- a/source/blender/blenlib/BLI_edgehash.h +++ b/source/blender/blenlib/BLI_edgehash.h @@ -47,22 +47,22 @@ void BLI_edgehash_free (EdgeHash *eh, EdgeHashFreeFP valfreefp); /* Insert edge (v0,v1) into hash with given value, does * not check for duplicates. */ -void BLI_edgehash_insert (EdgeHash *eh, int v0, int v1, void *val); +void BLI_edgehash_insert (EdgeHash *eh, unsigned int v0, unsigned int v1, void *val); /* Return value for given edge (v0,v1), or NULL if * if key does not exist in hash. (If need exists * to differentiate between key-value being NULL and * lack of key then see BLI_edgehash_lookup_p(). */ -void* BLI_edgehash_lookup (EdgeHash *eh, int v0, int v1); +void* BLI_edgehash_lookup (EdgeHash *eh, unsigned int v0, unsigned int v1); /* Return pointer to value for given edge (v0,v1), * or NULL if key does not exist in hash. */ -void** BLI_edgehash_lookup_p (EdgeHash *eh, int v0, int v1); +void** BLI_edgehash_lookup_p (EdgeHash *eh, unsigned int v0, unsigned int v1); /* Return boolean true/false if edge (v0,v1) in hash. */ -int BLI_edgehash_haskey (EdgeHash *eh, int v0, int v1); +int BLI_edgehash_haskey (EdgeHash *eh, unsigned int v0, unsigned int v1); /* Return number of keys in hash. */ int BLI_edgehash_size (EdgeHash *eh); @@ -83,7 +83,7 @@ EdgeHashIterator* BLI_edgehashIterator_new (EdgeHash *eh); void BLI_edgehashIterator_free (EdgeHashIterator *ehi); /* Retrieve the key from an iterator. */ -void BLI_edgehashIterator_getKey (EdgeHashIterator *ehi, int *v0_r, int *v1_r); +void BLI_edgehashIterator_getKey (EdgeHashIterator *ehi, unsigned int *v0_r, unsigned int *v1_r); /* Retrieve the value from an iterator. */ void* BLI_edgehashIterator_getValue (EdgeHashIterator *ehi); @@ -98,4 +98,3 @@ void BLI_edgehashIterator_step (EdgeHashIterator *ehi); int BLI_edgehashIterator_isDone (EdgeHashIterator *ehi); #endif - diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 0e33ee2be3f..7fbb4680b6d 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -34,6 +34,8 @@ extern "C" { #endif +#include "BLI_math_inline.h" + /* primaries */ #define BLI_XYZ_SMPTE 0 #define BLI_XYZ_REC709_SRGB 1 @@ -48,7 +50,7 @@ extern "C" { #define BLI_YCC_ITU_BT601 0 #define BLI_YCC_ITU_BT709 1 #define BLI_YCC_JFIF_0_255 2 - + /******************* Conversion to RGB ********************/ void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b); @@ -70,22 +72,23 @@ unsigned int hsv_to_cpack(float h, float s, float v); float rgb_to_grayscale(float rgb[3]); unsigned char rgb_to_grayscale_byte(unsigned char rgb[3]); -/***************** Profile Transformations ********************/ +/**************** Profile Transformations *****************/ void gamma_correct(float *c, float gamma); float rec709_to_linearrgb(float c); float linearrgb_to_rec709(float c); float srgb_to_linearrgb(float c); float linearrgb_to_srgb(float c); -void srgb_to_linearrgb_v3_v3(float *col_to, float *col_from); -void linearrgb_to_srgb_v3_v3(float *col_to, float *col_from); - -/* rgba buffer convenience functions */ -void srgb_to_linearrgb_rgba_buf(float *col, int tot); -void linearrgb_to_srgb_rgba_buf(float *col, int tot); -void srgb_to_linearrgb_rgba_rgba_buf(float *col_to, float *col_from, int tot); -void linearrgb_to_srgb_rgba_rgba_buf(float *col_to, float *col_from, int tot); - + +MINLINE void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3]); +MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3]); + +MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4]); +MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4]); + +MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4]); +MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4]); + /************************** Other *************************/ int constrain_rgb(float *r, float *g, float *b); @@ -101,6 +104,10 @@ void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *o void rgb_byte_to_float(const unsigned char *in, float *out); void rgb_float_to_byte(const float *in, unsigned char *out); +#ifdef BLI_MATH_INLINE_H +#include "intern/math_color_inline.c" +#endif + #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt index fb9b8021b8e..a03aee7cb7c 100644 --- a/source/blender/blenlib/CMakeLists.txt +++ b/source/blender/blenlib/CMakeLists.txt @@ -87,6 +87,7 @@ set(SRC intern/voxel.c intern/winstuff.c + BLI_array.h BLI_args.h BLI_blenlib.h BLI_boxpack2d.h diff --git a/source/blender/blenlib/intern/edgehash.c b/source/blender/blenlib/intern/edgehash.c index 7ae68101154..b710e5d496d 100644 --- a/source/blender/blenlib/intern/edgehash.c +++ b/source/blender/blenlib/intern/edgehash.c @@ -20,7 +20,7 @@ * * The Original Code is: none of this file. * - * Contributor(s): Daniel Dunbar + * Contributor(s): Daniel Dunbar, Joseph Eagar * * ***** END GPL LICENSE BLOCK ***** * A general (pointer -> pointer) hash table ADT @@ -35,30 +35,41 @@ #include <string.h> #include "MEM_guardedalloc.h" -#include "BLI_edgehash.h" -/***/ +#include "BLI_utildefines.h" +#include "BLI_edgehash.h" +#include "BLI_mempool.h" -static unsigned int hashsizes[]= { - 1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, - 16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169, - 4194319, 8388617, 16777259, 33554467, 67108879, 134217757, +/**************inlined code************/ +static unsigned int _ehash_hashsizes[]= { + 1, 3, 5, 11, 17, 37, 67, 131, 257, 521, 1031, 2053, 4099, 8209, + 16411, 32771, 65537, 131101, 262147, 524309, 1048583, 2097169, + 4194319, 8388617, 16777259, 33554467, 67108879, 134217757, 268435459 }; -#define EDGEHASH(v0,v1) ((v0*39)^(v1*31)) +#define EDGE_HASH(v0, v1) ((v0 * 39)^(v1 * 31)) + +/* ensure v0 is smaller */ +#define EDGE_ORD(v0, v1) \ + if (v0 < v1) { \ + v0 ^= v1; \ + v1 ^= v0; \ + v0 ^= v1; \ + } /***/ -typedef struct Entry Entry; -struct Entry { - Entry *next; - int v0, v1; +typedef struct EdgeEntry EdgeEntry; +struct EdgeEntry { + EdgeEntry *next; + unsigned int v0, v1; void *val; }; struct EdgeHash { - Entry **buckets; + EdgeEntry **buckets; + BLI_mempool *epool; int nbuckets, nentries, cursize; }; @@ -66,87 +77,82 @@ struct EdgeHash { EdgeHash *BLI_edgehash_new(void) { - EdgeHash *eh= MEM_mallocN(sizeof(*eh), "EdgeHash"); - eh->cursize= 0; - eh->nentries= 0; - eh->nbuckets= hashsizes[eh->cursize]; - - eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets)); - memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets)); + EdgeHash *eh = MEM_callocN(sizeof(*eh), "EdgeHash"); + eh->cursize = 0; + eh->nentries = 0; + eh->nbuckets = _ehash_hashsizes[eh->cursize]; + eh->buckets = MEM_callocN(eh->nbuckets * sizeof(*eh->buckets), "eh buckets 2"); + eh->epool = BLI_mempool_create(sizeof(EdgeEntry), 512, 512, TRUE, FALSE); + return eh; } -void BLI_edgehash_insert(EdgeHash *eh, int v0, int v1, void *val) + +void BLI_edgehash_insert(EdgeHash *eh, unsigned int v0, unsigned int v1, void *val) { unsigned int hash; - Entry *e= malloc(sizeof(*e)); + EdgeEntry *e = BLI_mempool_alloc(eh->epool); - if (v1<v0) { - v0 ^= v1; - v1 ^= v0; - v0 ^= v1; - } - hash = EDGEHASH(v0,v1)%eh->nbuckets; + EDGE_ORD(v0, v1); /* ensure v0 is smaller */ + + hash = EDGE_HASH(v0, v1) % eh->nbuckets; e->v0 = v0; e->v1 = v1; e->val = val; - e->next= eh->buckets[hash]; + e->next = eh->buckets[hash]; eh->buckets[hash]= e; - - if (++eh->nentries>eh->nbuckets*3) { - Entry **old= eh->buckets; - int i, nold= eh->nbuckets; - - eh->nbuckets= hashsizes[++eh->cursize]; - eh->buckets= malloc(eh->nbuckets*sizeof(*eh->buckets)); - memset(eh->buckets, 0, eh->nbuckets*sizeof(*eh->buckets)); - - for (i=0; i<nold; i++) { - for (e= old[i]; e;) { - Entry *n= e->next; - - hash= EDGEHASH(e->v0,e->v1)%eh->nbuckets; - e->next= eh->buckets[hash]; + + if (++eh->nentries>eh->nbuckets * 3) { + EdgeEntry *e, **old = eh->buckets; + int i, nold = eh->nbuckets; + + eh->nbuckets = _ehash_hashsizes[++eh->cursize]; + eh->buckets = MEM_mallocN(eh->nbuckets * sizeof(*eh->buckets), "eh buckets"); + memset(eh->buckets, 0, eh->nbuckets * sizeof(*eh->buckets)); + + for (i = 0; i < nold; i++) { + for (e = old[i]; e;) { + EdgeEntry *n = e->next; + + hash = EDGE_HASH(e->v0, e->v1) % eh->nbuckets; + e->next = eh->buckets[hash]; eh->buckets[hash]= e; - - e= n; + + e = n; } } - - free(old); + + MEM_freeN(old); } } -void** BLI_edgehash_lookup_p(EdgeHash *eh, int v0, int v1) +void **BLI_edgehash_lookup_p(EdgeHash *eh, unsigned int v0, unsigned int v1) { unsigned int hash; - Entry *e; + EdgeEntry *e; - if (v1<v0) { - v0 ^= v1; - v1 ^= v0; - v0 ^= v1; - } - hash = EDGEHASH(v0,v1)%eh->nbuckets; - for (e= eh->buckets[hash]; e; e= e->next) - if (v0==e->v0 && v1==e->v1) + EDGE_ORD(v0, v1); /* ensure v0 is smaller */ + + hash = EDGE_HASH(v0, v1) % eh->nbuckets; + for (e = eh->buckets[hash]; e; e = e->next) + if (v0 == e->v0 && v1 == e->v1) return &e->val; - + return NULL; } -void* BLI_edgehash_lookup(EdgeHash *eh, int v0, int v1) +void *BLI_edgehash_lookup(EdgeHash *eh, unsigned int v0, unsigned int v1) { - void **value_p = BLI_edgehash_lookup_p(eh,v0,v1); + void **value_p = BLI_edgehash_lookup_p(eh, v0, v1); return value_p?*value_p:NULL; } -int BLI_edgehash_haskey(EdgeHash *eh, int v0, int v1) +int BLI_edgehash_haskey(EdgeHash *eh, unsigned int v0, unsigned int v1) { - return BLI_edgehash_lookup_p(eh, v0, v1)!=NULL; + return BLI_edgehash_lookup_p(eh, v0, v1) != NULL; } int BLI_edgehash_size(EdgeHash *eh) @@ -158,28 +164,30 @@ void BLI_edgehash_clear(EdgeHash *eh, EdgeHashFreeFP valfreefp) { int i; - for (i=0; i<eh->nbuckets; i++) { - Entry *e; + for (i = 0; i<eh->nbuckets; i++) { + EdgeEntry *e; - for (e= eh->buckets[i]; e; ) { - Entry *n= e->next; + for (e = eh->buckets[i]; e; ) { + EdgeEntry *n = e->next; if (valfreefp) valfreefp(e->val); - free(e); + BLI_mempool_free(eh->epool, e); - e= n; + e = n; } - eh->buckets[i]= NULL; + eh->buckets[i] = NULL; } - eh->nentries= 0; + eh->nentries = 0; } void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp) { BLI_edgehash_clear(eh, valfreefp); - - free(eh->buckets); + + BLI_mempool_destroy(eh->epool); + + MEM_freeN(eh->buckets); MEM_freeN(eh); } @@ -189,29 +197,29 @@ void BLI_edgehash_free(EdgeHash *eh, EdgeHashFreeFP valfreefp) struct EdgeHashIterator { EdgeHash *eh; int curBucket; - Entry *curEntry; + EdgeEntry *curEntry; }; EdgeHashIterator *BLI_edgehashIterator_new(EdgeHash *eh) { - EdgeHashIterator *ehi= malloc(sizeof(*ehi)); - ehi->eh= eh; - ehi->curEntry= NULL; - ehi->curBucket= -1; + EdgeHashIterator *ehi = MEM_mallocN(sizeof(*ehi), "eh iter"); + ehi->eh = eh; + ehi->curEntry = NULL; + ehi->curBucket = -1; while (!ehi->curEntry) { ehi->curBucket++; - if (ehi->curBucket==ehi->eh->nbuckets) + if (ehi->curBucket == ehi->eh->nbuckets) break; - ehi->curEntry= ehi->eh->buckets[ehi->curBucket]; + ehi->curEntry = ehi->eh->buckets[ehi->curBucket]; } return ehi; } void BLI_edgehashIterator_free(EdgeHashIterator *ehi) { - free(ehi); + MEM_freeN(ehi); } -void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, int *v0_r, int *v1_r) +void BLI_edgehashIterator_getKey(EdgeHashIterator *ehi, unsigned int *v0_r, unsigned int *v1_r) { if (ehi->curEntry) { *v0_r = ehi->curEntry->v0; @@ -225,19 +233,22 @@ void *BLI_edgehashIterator_getValue(EdgeHashIterator *ehi) void BLI_edgehashIterator_setValue(EdgeHashIterator *ehi, void *val) { - if(ehi->curEntry) - ehi->curEntry->val= val; + if (ehi->curEntry) { + ehi->curEntry->val = val; + } } void BLI_edgehashIterator_step(EdgeHashIterator *ehi) { if (ehi->curEntry) { - ehi->curEntry= ehi->curEntry->next; + ehi->curEntry = ehi->curEntry->next; while (!ehi->curEntry) { ehi->curBucket++; - if (ehi->curBucket==ehi->eh->nbuckets) + if (ehi->curBucket == ehi->eh->nbuckets) { break; - ehi->curEntry= ehi->eh->buckets[ehi->curBucket]; + } + + ehi->curEntry = ehi->eh->buckets[ehi->curBucket]; } } } diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index 2ef29c1ce94..8f5366b6317 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -393,57 +393,6 @@ float linearrgb_to_srgb(float c) return 1.055f * powf(c, 1.0f/2.4f) - 0.055f; } -void srgb_to_linearrgb_v3_v3(float *col_to, float *col_from) -{ - col_to[0] = srgb_to_linearrgb(col_from[0]); - col_to[1] = srgb_to_linearrgb(col_from[1]); - col_to[2] = srgb_to_linearrgb(col_from[2]); -} - -void linearrgb_to_srgb_v3_v3(float *col_to, float *col_from) -{ - col_to[0] = linearrgb_to_srgb(col_from[0]); - col_to[1] = linearrgb_to_srgb(col_from[1]); - col_to[2] = linearrgb_to_srgb(col_from[2]); -} - -/* todo, should these be moved elsewhere?, they dont belong in imbuf */ -void srgb_to_linearrgb_rgba_buf(float *col, int tot) -{ - while(tot--) { - srgb_to_linearrgb_v3_v3(col, col); - col += 4; - } -} - -void linearrgb_to_srgb_rgba_buf(float *col, int tot) -{ - while(tot--) { - linearrgb_to_srgb_v3_v3(col, col); - col += 4; - } -} - -void srgb_to_linearrgb_rgba_rgba_buf(float *col_to, float *col_from, int tot) -{ - while(tot--) { - srgb_to_linearrgb_v3_v3(col_to, col_from); - col_to[3]= col_from[3]; - col_to += 4; - col_from += 4; - } -} - -void linearrgb_to_srgb_rgba_rgba_buf(float *col_to, float *col_from, int tot) -{ - while(tot--) { - linearrgb_to_srgb_v3_v3(col_to, col_from); - col_to[3]= col_from[3]; - col_to += 4; - col_from += 4; - } -} - void minmax_rgb(short c[]) { if(c[0]>255) c[0]=255; diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c new file mode 100644 index 00000000000..aaaa065f14d --- /dev/null +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -0,0 +1,109 @@ +/* + * ***** BEGIN GPL LICENSE BLOCK ***** + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. + * All rights reserved. + * + * The Original Code is: some of this file. + * + * ***** END GPL LICENSE BLOCK ***** + * */ + +/** \file blender/blenlib/intern/math_color_inline.c + * \ingroup bli + */ + + +#include "BLI_math_color.h" +#include "BLI_utildefines.h" + +#ifndef BLI_MATH_COLOR_INLINE_H +#define BLI_MATH_COLOR_INLINE_H + +/******************************** Color Space ********************************/ + +MINLINE void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3]) +{ + linear[0] = srgb_to_linearrgb(srgb[0]); + linear[1] = srgb_to_linearrgb(srgb[1]); + linear[2] = srgb_to_linearrgb(srgb[2]); +} + +MINLINE void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3]) +{ + srgb[0] = linearrgb_to_srgb(linear[0]); + srgb[1] = linearrgb_to_srgb(linear[1]); + srgb[2] = linearrgb_to_srgb(linear[2]); +} + +MINLINE void srgb_to_linearrgb_v4(float linear[4], const float srgb[4]) +{ + srgb_to_linearrgb_v3_v3(linear, srgb); + linear[3] = srgb[3]; +} + +MINLINE void linearrgb_to_srgb_v4(float srgb[4], const float linear[4]) +{ + linearrgb_to_srgb_v3_v3(srgb, linear); + srgb[3] = linear[3]; +} + +/* predivide versions to work on associated/premultipled alpha. if this should + be done or not depends on the background the image will be composited over, + ideally you would never do color space conversion on an image with alpha + because it is ill defined */ + +MINLINE void srgb_to_linearrgb_predivide_v4(float linear[4], const float srgb[4]) +{ + float alpha, inv_alpha; + + if(srgb[3] == 1.0f || srgb[3] == 0.0f) { + alpha = 1.0f; + inv_alpha = 1.0f; + } + else { + alpha = srgb[3]; + inv_alpha = 1.0f/alpha; + } + + linear[0] = srgb_to_linearrgb(srgb[0] * inv_alpha) * alpha; + linear[1] = srgb_to_linearrgb(srgb[1] * inv_alpha) * alpha; + linear[2] = srgb_to_linearrgb(srgb[2] * inv_alpha) * alpha; + linear[3] = srgb[3]; +} + +MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4]) +{ + float alpha, inv_alpha; + + if(linear[3] == 1.0f || linear[3] == 0.0f) { + alpha = 1.0f; + inv_alpha = 1.0f; + } + else { + alpha = linear[3]; + inv_alpha = 1.0f/alpha; + } + + srgb[0] = linearrgb_to_srgb(linear[0] * inv_alpha) * alpha; + srgb[1] = linearrgb_to_srgb(linear[1] * inv_alpha) * alpha; + srgb[2] = linearrgb_to_srgb(linear[2] * inv_alpha) * alpha; + srgb[3] = linear[3]; +} + +#endif /* BLI_MATH_COLOR_INLINE_H */ + |