fix compile errors
[wdl/wdl-ol.git] / WDL / lice / lice_colorspace.cpp
blob6e2eb3885188d3ae782ee0ed5ff56c0ec3de001e
1 #include "lice.h"
2 #include <math.h>
4 #define LICE_COMBINE_IMPLEMENT_HSV
5 #include "lice_combine.h"
8 LICE_pixel LICE_AlterColorHSV_int(LICE_pixel color, int dH, int dS, int dV) // H is rolled over [0,384), S and V are clamped [0,255)
10 int h, s, v;
11 LICE_RGB2HSV(LICE_GETR(color), LICE_GETG(color), LICE_GETB(color), &h, &s, &v);
13 h += dH;
14 s += dS;
15 v += dV;
17 if (h < 0) h += 384;
18 else if (h >= 384) h -= 384;
20 if (s & ~255)
22 if (s<0) s = 0;
23 else s = 255;
26 if (v&~255)
28 if (v < 0) v = 0.;
29 else v = 255;
32 return LICE_HSV2Pix(h, s, v, LICE_GETA(color));
35 LICE_pixel LICE_AlterColorHSV(LICE_pixel color, float dH, float dS, float dV) // H is rolled over, S and V are clamped, all [0,1)
37 int dHi = (int)(dH*384.0f);
38 int dSi = (int)(dS*255.0f);
39 int dVi = (int)(dV*255.0f);
40 return LICE_AlterColorHSV_int(color, dHi, dSi, dVi);
43 void LICE_AlterBitmapHSV(LICE_IBitmap* src, float dH, float dS, float dV) // H is rolled over, S and V are clamped
45 if (src) LICE_AlterRectHSV(src,0,0,src->getWidth(),src->getHeight(),dH,dS,dV);
48 void LICE_AlterRectHSV(LICE_IBitmap* src, int xpos, int ypos, int w, int h, float dH, float dS, float dV) // H is rolled over, S and V are clamped
50 if (!src) return;
52 if (xpos < 0) {
53 w += xpos;
54 xpos = 0;
56 if (ypos < 0) {
57 h += ypos;
58 ypos = 0;
61 const int span = src->getRowSpan();
62 const int destbm_w = src->getWidth(), destbm_h = src->getHeight();
63 if (span < 1 || w < 1 || h < 1 || xpos >= destbm_w || ypos >= destbm_h) return;
65 if (w > destbm_w - xpos) w = destbm_w - xpos;
66 if (h > destbm_h - ypos) h = destbm_h - ypos;
68 LICE_pixel* px = src->getBits()+ypos*span+xpos;
70 int dHi = (int)(dH*384.0f);
71 int dSi = (int)(dS*255.0f);
72 int dVi = (int)(dV*255.0f);
73 if (dHi > 383) dHi=383;
74 else if (dHi < -383) dHi=-383;
77 if (!dHi && !dSi && !dVi) return; // no mod
79 if (w*h > 8192)
81 // generate a table of HSV translations with clip/clamp
82 unsigned char stab[256], vtab[256];
83 short htab[384];
84 int x;
85 for(x=0;x<256;x++)
87 int a=x+dSi;
88 if(a<0)a=0; else if (a>255)a=255;
89 stab[x]=a;
91 a=x+dVi;
92 if(a<0)a=0; else if (a>255)a=255;
93 vtab[x]=a;
95 a=x+dHi;
96 if(a<0)a+=384; else if (a>=384)a-=384;
97 htab[x]=a;
99 for(;x<384;x++)
101 int a=x+dHi;
102 if(a<0)a+=384; else if (a>=384)a-=384;
103 htab[x]=a;
106 while (h-->0)
108 LICE_pixel* tpx = px;
109 px+=span;
110 int xi=w;
111 while (xi-->0)
113 LICE_pixel color = *tpx;
114 int hh,s,v;
115 LICE_RGB2HSV(LICE_GETR(color), LICE_GETG(color), LICE_GETB(color), &hh, &s, &v);
116 *tpx++ = LICE_HSV2Pix(htab[hh],stab[s],vtab[v],LICE_GETA(color));
120 else
122 while (h-->0)
124 LICE_pixel* tpx = px;
125 px+=span;
126 int xi=w;
127 while (xi-->0)
129 *tpx = LICE_AlterColorHSV_int(*tpx, dHi, dSi, dVi);
130 tpx++;