Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Sharybin <sergey.vfx@gmail.com>2011-12-28 22:31:32 +0400
committerSergey Sharybin <sergey.vfx@gmail.com>2011-12-28 22:31:32 +0400
commitc2ae77e5bdd60e4cfe9b1f9d3d54e66f8089245c (patch)
tree505b5270122ec50302f6f824088b43318d13a296 /source/blender/blenlib/intern
parentfd134927dd23047237c6e490c09454487ba19a4a (diff)
parentfe2131367b3f1ca1f974d1d6160cfc8a1a220abe (diff)
Merging r42896 through r42944 from trunk into soc-2911-tomato
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r--source/blender/blenlib/intern/edgehash.c185
-rw-r--r--source/blender/blenlib/intern/math_color.c51
-rw-r--r--source/blender/blenlib/intern/math_color_inline.c109
3 files changed, 207 insertions, 138 deletions
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 */
+