diff options
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_editVert.h | 3 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_fnmatch.h | 2 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_math_color.h | 14 | ||||
-rw-r--r-- | source/blender/blenlib/BLI_utildefines.h | 1 | ||||
-rw-r--r-- | source/blender/blenlib/intern/boxpack2d.c | 24 | ||||
-rw-r--r-- | source/blender/blenlib/intern/fileops.c | 2 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color.c | 98 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_color_inline.c | 75 | ||||
-rw-r--r-- | source/blender/blenlib/intern/path_util.c | 8 | ||||
-rw-r--r-- | source/blender/blenlib/intern/storage.c | 51 |
10 files changed, 226 insertions, 52 deletions
diff --git a/source/blender/blenlib/BLI_editVert.h b/source/blender/blenlib/BLI_editVert.h index ffe1d7e2b6e..8ebfa73cf6a 100644 --- a/source/blender/blenlib/BLI_editVert.h +++ b/source/blender/blenlib/BLI_editVert.h @@ -42,7 +42,6 @@ #include "BLO_sys_types.h" // for intptr_t support struct DerivedMesh; -struct RetopoPaintData; /* note; changing this also might affect the undo copy in editmesh.c */ typedef struct EditVert @@ -184,8 +183,6 @@ typedef struct EditMesh */ int lastDataMask; - struct RetopoPaintData *retopo_paint_data; - CustomData vdata, edata, fdata; } EditMesh; diff --git a/source/blender/blenlib/BLI_fnmatch.h b/source/blender/blenlib/BLI_fnmatch.h index c09a7c7c0d2..ac8960ff9c9 100644 --- a/source/blender/blenlib/BLI_fnmatch.h +++ b/source/blender/blenlib/BLI_fnmatch.h @@ -51,7 +51,7 @@ extern "C" { #define FNM_NOESCAPE (1 << 1) /* Backslashes don't quote special chars. */ #define FNM_PERIOD (1 << 2) /* Leading `.' is matched only explicitly. */ -#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_GNU_SOURCE) +#if !defined (_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 2 || defined (_GNU_SOURCE) || defined( __SUNPRO_C) #define FNM_FILE_NAME FNM_PATHNAME /* Preferred GNU name. */ #define FNM_LEADING_DIR (1 << 3) /* Ignore `/...' after a match. */ #define FNM_CASEFOLD (1 << 4) /* Compare without regard to case. */ diff --git a/source/blender/blenlib/BLI_math_color.h b/source/blender/blenlib/BLI_math_color.h index 7fbb4680b6d..6d37aabd6ab 100644 --- a/source/blender/blenlib/BLI_math_color.h +++ b/source/blender/blenlib/BLI_math_color.h @@ -69,8 +69,10 @@ void rgb_to_hsv_compat(float r, float g, float b, float *lh, float *ls, float *l unsigned int rgb_to_cpack(float r, float g, float b); 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]); +float rgb_to_grayscale(const float rgb[3]); +unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]); +float rgb_to_luma(const float rgb[3]); +unsigned char rgb_to_luma_byte(const unsigned char rgb[3]); /**************** Profile Transformations *****************/ @@ -89,6 +91,8 @@ 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]); +void BLI_init_srgb_conversion(void); + /************************** Other *************************/ int constrain_rgb(float *r, float *g, float *b); @@ -97,13 +101,13 @@ void minmax_rgb(short c[3]); void rgb_float_set_hue_float_offset(float * rgb, float hue_offset); void rgb_byte_set_hue_float_offset(unsigned char * rgb, float hue_offset); +void rgb_byte_to_float(const unsigned char in[3], float out[3]); +void rgb_float_to_byte(const float in[3], unsigned char out[3]); + /***************** lift/gamma/gain / ASC-CDL conversion *****************/ void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power); -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 diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index abe8fb7d8fe..6d9188c1848 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -108,6 +108,7 @@ #define FTOCHAR(val) ((val)<=0.0f)? 0 : (((val)>(1.0f-0.5f/255.0f))? 255 : (char)((255.0f*(val))+0.5f)) #define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f)) +#define USHORTTOUCHAR(val) ((unsigned char)(((val) >= 65535-128)? 255: ((val)+128)>>8)) #define F3TOCHAR3(v2, v1) { \ (v1)[0]= FTOCHAR((v2[0])); \ (v1)[1]= FTOCHAR((v2[1])); \ diff --git a/source/blender/blenlib/intern/boxpack2d.c b/source/blender/blenlib/intern/boxpack2d.c index 30bf69ba3b4..073b4b5d0c6 100644 --- a/source/blender/blenlib/intern/boxpack2d.c +++ b/source/blender/blenlib/intern/boxpack2d.c @@ -174,33 +174,33 @@ void boxPack2D(boxPack *boxarray, const int len, float *tot_width, float *tot_he for (box=boxarray, box_index=0, i=0; box_index < len; box_index++, box++) { - vert->blb = vert->brb = vert->tlb =\ - vert->isect_cache[0] = vert->isect_cache[1] =\ - vert->isect_cache[2] = vert->isect_cache[3] = NULL; + vert->blb = vert->brb = vert->tlb = + vert->isect_cache[0] = vert->isect_cache[1] = + vert->isect_cache[2] = vert->isect_cache[3] = NULL; vert->free = CORNERFLAGS &~ TRF; vert->trb = box; vert->index = i; i++; box->v[BL] = vert; vert++; - vert->trb= vert->brb = vert->tlb =\ - vert->isect_cache[0] = vert->isect_cache[1] =\ - vert->isect_cache[2] = vert->isect_cache[3] = NULL; + vert->trb= vert->brb = vert->tlb = + vert->isect_cache[0] = vert->isect_cache[1] = + vert->isect_cache[2] = vert->isect_cache[3] = NULL; vert->free = CORNERFLAGS &~ BLF; vert->blb = box; vert->index = i; i++; box->v[TR] = vert; vert++; - vert->trb = vert->blb = vert->tlb =\ - vert->isect_cache[0] = vert->isect_cache[1] =\ - vert->isect_cache[2] = vert->isect_cache[3] = NULL; + vert->trb = vert->blb = vert->tlb = + vert->isect_cache[0] = vert->isect_cache[1] = + vert->isect_cache[2] = vert->isect_cache[3] = NULL; vert->free = CORNERFLAGS &~ BRF; vert->brb = box; vert->index = i; i++; box->v[TL] = vert; vert++; - vert->trb = vert->blb = vert->brb =\ - vert->isect_cache[0] = vert->isect_cache[1] =\ - vert->isect_cache[2] = vert->isect_cache[3] = NULL; + vert->trb = vert->blb = vert->brb = + vert->isect_cache[0] = vert->isect_cache[1] = + vert->isect_cache[2] = vert->isect_cache[3] = NULL; vert->free = CORNERFLAGS &~ TLF; vert->tlb = box; vert->index = i; i++; diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index 95b6a970b49..ad0478060f2 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -82,7 +82,7 @@ int BLI_file_gzip(const char *from, const char *to) return -2; while(1) { - readsize = read(file, buffer, 10240); + readsize = read(file, buffer, sizeof(buffer)); if(readsize < 0) { rval= -2; /* error happened in reading */ diff --git a/source/blender/blenlib/intern/math_color.c b/source/blender/blenlib/intern/math_color.c index 8f5366b6317..20df893015d 100644 --- a/source/blender/blenlib/intern/math_color.c +++ b/source/blender/blenlib/intern/math_color.c @@ -30,7 +30,11 @@ #include <assert.h> +#include "MEM_guardedalloc.h" + #include "BLI_math.h" +#include "BLI_rand.h" +#include "BLI_utildefines.h" void hsv_to_rgb(float h, float s, float v, float *r, float *g, float *b) { @@ -333,14 +337,14 @@ void cpack_to_rgb(unsigned int col, float *r, float *g, float *b) *b /= 255.0f; } -void rgb_byte_to_float(const unsigned char *in, float *out) +void rgb_byte_to_float(const unsigned char in[3], float out[3]) { out[0]= ((float)in[0]) / 255.0f; out[1]= ((float)in[1]) / 255.0f; out[2]= ((float)in[2]) / 255.0f; } -void rgb_float_to_byte(const float *in, unsigned char *out) +void rgb_float_to_byte(const float in[3], unsigned char out[3]) { int r, g, b; @@ -430,16 +434,26 @@ int constrain_rgb(float *r, float *g, float *b) return 0; /* Color within RGB gamut */ } -float rgb_to_grayscale(float rgb[3]) +float rgb_to_grayscale(const float rgb[3]) { return 0.3f*rgb[0] + 0.58f*rgb[1] + 0.12f*rgb[2]; } -unsigned char rgb_to_grayscale_byte(unsigned char rgb[3]) +unsigned char rgb_to_grayscale_byte(const unsigned char rgb[3]) { return (76*(unsigned short)rgb[0] + 148*(unsigned short)rgb[1] + 31*(unsigned short)rgb[2]) / 255; } +float rgb_to_luma(const float rgb[3]) +{ + return 0.299f*rgb[0] + 0.587f*rgb[1] + 0.114f*rgb[2]; +} + +unsigned char rgb_to_luma_byte(const unsigned char rgb[3]) +{ + return (76*(unsigned short)rgb[0] + 150*(unsigned short)rgb[1] + 29*(unsigned short)rgb[2]) / 255; +} + /* ********************************* lift/gamma/gain / ASC-CDL conversion ********************************* */ void lift_gamma_gain_to_asc_cdl(float *lift, float *gamma, float *gain, float *offset, float *slope, float *power) @@ -480,3 +494,79 @@ void rgb_byte_set_hue_float_offset(unsigned char rgb[3], float hue_offset) rgb_float_set_hue_float_offset(rgb_float, hue_offset); rgb_float_to_byte(rgb_float, rgb); } + + +/* fast sRGB conversion + * LUT from linear float to 16-bit short + * based on http://mysite.verizon.net/spitzak/conversion/ + */ + +float BLI_color_from_srgb_table[256]; +unsigned short BLI_color_to_srgb_table[0x10000]; + +static unsigned short hipart(const float f) +{ + union { + float f; + unsigned short us[2]; + } tmp; + + tmp.f = f; + +#ifdef __BIG_ENDIAN__ + return tmp.us[0]; +#else + return tmp.us[1]; +#endif +} + +static float index_to_float(const unsigned short i) +{ + union { + float f; + unsigned short us[2]; + } tmp; + + /* positive and negative zeros, and all gradual underflow, turn into zero: */ + if (i<0x80 || (i >= 0x8000 && i < 0x8080)) return 0; + /* All NaN's and infinity turn into the largest possible legal float: */ + if (i>=0x7f80 && i<0x8000) return FLT_MAX; + if (i>=0xff80) return -FLT_MAX; + +#ifdef __BIG_ENDIAN__ + tmp.us[0] = i; + tmp.us[1] = 0x8000; +#else + tmp.us[0] = 0x8000; + tmp.us[1] = i; +#endif + + return tmp.f; +} + +void BLI_init_srgb_conversion(void) +{ + static int initialized= 0; + int i, b; + + if (initialized) return; + initialized = 1; + + /* Fill in the lookup table to convert floats to bytes: */ + for (i = 0; i < 0x10000; i++) { + float f = linearrgb_to_srgb(index_to_float(i))*255.0f; + if (f <= 0) BLI_color_to_srgb_table[i] = 0; + else if (f < 255) BLI_color_to_srgb_table[i] = (unsigned short)(f*0x100+.5); + else BLI_color_to_srgb_table[i] = 0xff00; + } + + /* Fill in the lookup table to convert bytes to float: */ + for (b = 0; b <= 255; b++) { + float f = srgb_to_linearrgb(((float)b)*(1.0f/255.0f)); + BLI_color_from_srgb_table[b] = f; + i = hipart(f); + /* replace entries so byte->float->byte does not change the data: */ + BLI_color_to_srgb_table[i] = b*0x100; + } +} + diff --git a/source/blender/blenlib/intern/math_color_inline.c b/source/blender/blenlib/intern/math_color_inline.c index aaaa065f14d..1247632cf79 100644 --- a/source/blender/blenlib/intern/math_color_inline.c +++ b/source/blender/blenlib/intern/math_color_inline.c @@ -105,5 +105,80 @@ MINLINE void linearrgb_to_srgb_predivide_v4(float srgb[4], const float linear[4] srgb[3] = linear[3]; } +/* LUT accelerated conversions */ + +extern float BLI_color_from_srgb_table[256]; +extern unsigned short BLI_color_to_srgb_table[0x10000]; + +MINLINE unsigned short to_srgb_table_lookup(const float f) +{ + union { + float f; + unsigned short us[2]; + } tmp; + tmp.f = f; +#ifdef __BIG_ENDIAN__ + return BLI_color_to_srgb_table[tmp.us[0]]; +#else + return BLI_color_to_srgb_table[tmp.us[1]]; +#endif +} + +MINLINE void linearrgb_to_srgb_ushort4(unsigned short srgb[4], const float linear[4]) +{ + srgb[0] = to_srgb_table_lookup(linear[0]); + srgb[1] = to_srgb_table_lookup(linear[1]); + srgb[2] = to_srgb_table_lookup(linear[2]); + srgb[3] = FTOUSHORT(linear[3]); +} + +MINLINE void linearrgb_to_srgb_ushort4_predivide(unsigned short srgb[4], const float linear[4]) +{ + float alpha, inv_alpha, t; + int i; + + if(linear[3] == 1.0f || linear[3] == 0.0f) { + linearrgb_to_srgb_ushort4(srgb, linear); + return; + } + + alpha = linear[3]; + inv_alpha = 1.0f/alpha; + + for(i=0; i<3; ++i) { + t = linear[i] * inv_alpha; + srgb[i] = (t < 1.0f)? to_srgb_table_lookup(t) * alpha : FTOUSHORT(linearrgb_to_srgb(t) * alpha); + } + + srgb[3] = FTOUSHORT(linear[3]); +} + +MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4]) +{ + linear[0] = BLI_color_from_srgb_table[srgb[0]]; + linear[1] = BLI_color_from_srgb_table[srgb[1]]; + linear[2] = BLI_color_from_srgb_table[srgb[2]]; + linear[3] = srgb[3] * (1.0f/255.0f); +} + +MINLINE void srgb_to_linearrgb_uchar4_predivide(float linear[4], const unsigned char srgb[4]) +{ + float alpha, inv_alpha; + int i; + + if(srgb[3] == 255 || srgb[3] == 0) { + srgb_to_linearrgb_uchar4(linear, srgb); + return; + } + + alpha = srgb[3] * (1.0f/255.0f); + inv_alpha = 1.0f/alpha; + + for(i=0; i<3; ++i) + linear[i] = linearrgb_to_srgb(srgb[i] * inv_alpha) * alpha; + + linear[3] = alpha; +} + #endif /* BLI_MATH_COLOR_INLINE_H */ diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c index 9adb39f09ef..bbdb9bc5823 100644 --- a/source/blender/blenlib/intern/path_util.c +++ b/source/blender/blenlib/intern/path_util.c @@ -52,7 +52,7 @@ #include "GHOST_Path-api.h" -#if defined WIN32 && !defined _LIBC +#if defined WIN32 && !defined _LIBC || defined __sun # include "BLI_fnmatch.h" /* use fnmatch included in blenlib */ #else # ifndef _GNU_SOURCE @@ -627,8 +627,10 @@ int BLI_path_frame_range(char *path, int sta, int end, int digits) if (stringframe_chars(path, &ch_sta, &ch_end)) { /* warning, ch_end is the last # +1 */ char tmp[FILE_MAX]; - sprintf(tmp, "%.*s%.*d-%.*d%s", ch_sta, path, ch_end-ch_sta, sta, ch_end-ch_sta, end, path+ch_end); - strcpy(path, tmp); + BLI_snprintf(tmp, sizeof(tmp), + "%.*s%.*d-%.*d%s", + ch_sta, path, ch_end-ch_sta, sta, ch_end-ch_sta, end, path+ch_end); + BLI_strncpy(path, tmp, FILE_MAX); return 1; } return 0; diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c index c5b6f46b3bc..df0634e4fcf 100644 --- a/source/blender/blenlib/intern/storage.c +++ b/source/blender/blenlib/intern/storage.c @@ -203,7 +203,7 @@ static void bli_builddir(const char *dirname, const char *relname) char buf[256]; DIR *dir; - strcpy(buf,relname); + BLI_strncpy(buf, relname, sizeof(buf)); rellen=strlen(relname); if (rellen){ @@ -220,7 +220,7 @@ static void bli_builddir(const char *dirname, const char *relname) while ((fname = (struct dirent*) readdir(dir)) != NULL) { dlink = (struct dirlink *)malloc(sizeof(struct dirlink)); if (dlink){ - strcpy(buf+rellen,fname->d_name); + BLI_strncpy(buf + rellen ,fname->d_name, sizeof(buf) - rellen); dlink->name = BLI_strdup(buf); BLI_addhead(dirbase,dlink); newnum++; @@ -343,8 +343,8 @@ static void bli_adddirstrings(void) tm= localtime(&file->s.st_mtime); // prevent impossible dates in windows if(tm==NULL) tm= localtime(&zero); - strftime(file->time, 8, "%H:%M", tm); - strftime(file->date, 16, "%d-%b-%y", tm); + strftime(file->time, sizeof(file->time), "%H:%M", tm); + strftime(file->date, sizeof(file->date), "%d-%b-%y", tm); /* * Seems st_size is signed 32-bit value in *nix and Windows. This @@ -354,38 +354,43 @@ static void bli_adddirstrings(void) st_size= file->s.st_size; if (st_size > 1024*1024*1024) { - sprintf(file->size, "%.2f GB", ((double)st_size)/(1024*1024*1024)); + BLI_snprintf(file->size, sizeof(file->size), "%.2f GB", ((double)st_size)/(1024*1024*1024)); } else if (st_size > 1024*1024) { - sprintf(file->size, "%.1f MB", ((double)st_size)/(1024*1024)); + BLI_snprintf(file->size, sizeof(file->size), "%.1f MB", ((double)st_size)/(1024*1024)); } else if (st_size > 1024) { - sprintf(file->size, "%d KB", (int)(st_size/1024)); + BLI_snprintf(file->size, sizeof(file->size), "%d KB", (int)(st_size/1024)); } else { - sprintf(file->size, "%d B", (int)st_size); + BLI_snprintf(file->size, sizeof(file->size), "%d B", (int)st_size); } - strftime(datum, 32, "%d-%b-%y %H:%M", tm); + strftime(datum, 32, "%d-%b-%y %H:%M", tm); /* XXX, is this used? - campbell */ if (st_size < 1000) { - sprintf(size, "%10d", (int) st_size); - } else if (st_size < 1000 * 1000) { - sprintf(size, "%6d %03d", (int) (st_size / 1000), (int) (st_size % 1000)); - } else if (st_size < 100 * 1000 * 1000) { - sprintf(size, "%2d %03d %03d", (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000)); - } else { - sprintf(size, "> %4.1f M", (double) (st_size / (1024.0 * 1024.0))); - sprintf(size, "%10d", (int) st_size); + BLI_snprintf(size, sizeof(size), "%10d", + (int) st_size); + } + else if (st_size < 1000 * 1000) { + BLI_snprintf(size, sizeof(size), "%6d %03d", + (int) (st_size / 1000), (int) (st_size % 1000)); + } + else if (st_size < 100 * 1000 * 1000) { + BLI_snprintf(size, sizeof(size), "%2d %03d %03d", + (int) (st_size / (1000 * 1000)), (int) ((st_size / 1000) % 1000), (int) ( st_size % 1000)); + } + else { + /* XXX, whats going on here?. 2x calls - campbell */ + BLI_snprintf(size, sizeof(size), "> %4.1f M", (double) (st_size / (1024.0 * 1024.0))); + BLI_snprintf(size, sizeof(size), "%10d", (int) st_size); } - sprintf(buf,"%s %s %s %7s %s %s %10s %s", file->mode1, file->mode2, file->mode3, file->owner, file->date, file->time, size, - file->relname); + BLI_snprintf(buf, sizeof(buf), "%s %s %s %7s %s %s %10s %s", + file->mode1, file->mode2, file->mode3, file->owner, + file->date, file->time, size, file->relname); - file->string=MEM_mallocN(strlen(buf)+1, "filestring"); - if (file->string){ - strcpy(file->string,buf); - } + file->string = BLI_strdup(buf); } } |