~amavect/makeu

ref: b6f6785f804b8f37745fdeff16f2e707c3a25664 makeu/hsvmap.c -rw-r--r-- 7.0 KiB
b6f6785famavect rename com.h to more standard fns.h 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Amavect! */
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include "elementile.h"
#include "fns.h"
#include "components.h"

static
char *buttons2[] = { "HS", "SV", "HV", nil};

static
Menu menu2 = { buttons2 };

/* return ratio of point to boundary. bound of 0,0 1,1 is illegal */
static
Ratioxy
ratioxy(Point p, Rectangle bound)
{
	Ratioxy rxy;
	rxy.x = (p.x - bound.min.x) / (double)(bound.max.x - bound.min.x - 1);
	rxy.y = (p.y - bound.min.y) / (double)(bound.max.y - bound.min.y - 1);
	return rxy;
}

/* get position in relative coordinates, not absolute */
static
Point
ratioxytopoint(Ratioxy rxy, Rectangle bound)
{
	Point p;
	p.x = (int)(rxy.x * (bound.max.x - bound.min.x - 1));
	p.y = (int)(rxy.y * (bound.max.y - bound.min.y - 1));
	return p;
}

/* map x,y to HSV to RGB */
static
HSVd
hsmap(Ratioxy rxy, HSVd hsvd)
{
	hsvd.hue = rxy.x * 6;
	hsvd.sat = rxy.y;
	return hsvd;
}

static
HSVd
svmap(Ratioxy rxy, HSVd hsvd)
{
	hsvd.sat = rxy.x;
	hsvd.val = rxy.y;
	return hsvd;
}

static
HSVd
hvmap(Ratioxy rxy, HSVd hsvd)
{
	hsvd.hue = rxy.x * 6;
	hsvd.val = rxy.y;
	return hsvd;
}

/* map HSV to x,y */
static
Ratioxy
hsmapinv(HSVd hsvd)
{
	Ratioxy rxy;
	rxy.x = hsvd.hue / 6.0;
	rxy.y = hsvd.sat;
	return rxy;
}

static
Ratioxy
svmapinv(HSVd hsvd)
{
	Ratioxy rxy;
	rxy.x = hsvd.sat;
	rxy.y = hsvd.val;
	return rxy;
}

static
Ratioxy
hvmapinv(HSVd hsvd)
{
	Ratioxy rxy;
	rxy.x = hsvd.hue / 6.0;
	rxy.y = hsvd.val;
	return rxy;
}

/* 
 * bugs:
 * rxy to pix conversion depends on precision of the src image
 * very unoptimized
 */
void
redraw(HSVmap *h)
{
	Ratioxy rxy;
	HSVd hsvd;
	uchar c[4] = {0,0,0,0};
	rxy = (Ratioxy){0,0};
	hsvd = h->hsv;
	loadimage(h->pix, h->pix->r, c, sizeof(c));
	switch(h->maptype){
	case 0:
		draw(screenbuf, h->r, h->huex, nil, ZP);
		draw(screenbuf, h->r, h->saty, nil, ZP);
		rxy.y = hsvd.val;
		draw(h->pix, h->pix->r, h->valy, nil, ratioxytopoint(rxy, h->r));
		draw(screenbuf, h->r, h->pix, nil, ZP);
		break;
	case 1:
		rxy.x = hsvd.hue / 6.0;
		draw(h->pix, h->pix->r, h->huex, nil, ratioxytopoint(rxy, h->r));
		draw(screenbuf, h->r, h->pix, nil, ZP);
		draw(screenbuf, h->r, h->satx, nil, ZP);
		draw(screenbuf, h->r, h->valy, nil, ZP);
		break;
	case 2:
		draw(screenbuf, h->r, h->huex, nil, ZP);
		rxy.x = hsvd.sat;
		draw(h->pix, h->pix->r, h->satx, nil, ratioxytopoint(rxy, h->r));
		draw(screenbuf, h->r, h->pix, nil, ZP);
		draw(screenbuf, h->r, h->valy, nil, ZP);
		break;
	}
	draw(screenbuf, h->r, h->circ, nil, mulpt(ratioxytopoint(h->pos, h->r),-1));
}

/*
 * Init unchanging data, return min size.
 */
Point
hsvmapinit(Elementile *e)
{
	HSVmap *h;
	h = e->aux;
	switch(h->maptype){
	case 0:
		h->pos = hsmapinv(h->hsv);
		break;
	case 1:
		h->pos = svmapinv(h->hsv);
		break;
	case 2:
		h->pos = hvmapinv(h->hsv);
		break;
	default:
		h->pos = (Ratioxy){0,0};
		h->maptype = 0;
	}
	h->circ = allocimage(display, Rect(-4,-4,5,5), RGBA32, 0, DTransparent);
	h->pix = allocimage(display, Rect(0,0,1,1), RGBA32, 1, DTransparent);
	if(h->circ == nil || h->pix == nil)
		sysfatal("RIP hsvmap, your images didn't alloc: %r");
	ellipse(h->circ, Pt(0,0), 4, 4, 0, display->black, ZP);
	h->huex = nil;
	h->satx = nil;
	h->saty = nil;
	h->valy = nil;
	return (Point){2, 2};
}

static
void
genmapimage(HSVmap *h)
{
	int xmax, ymax, i;
	uint rbs, cbs; /* row buffer size, column buffer size */
	uchar *rbuf, *cbuf; /* row buffer, column buffer */
	RGB rgb = {255,255,255};
	HSVd hues = {0.0, 1.0, 1.0};
	Ratioxy pd;
	double a;
	Rectangle r;
	
	r = rectsubpt(h->r, h->r.min);
	xmax = r.max.x - 1;
	ymax = r.max.y - 1;
	rbs = 4 * r.max.x;
	cbs = 4 * r.max.y;
	
	freeimage(h->huex);
	freeimage(h->satx);
	freeimage(h->saty);
	freeimage(h->valy);
	
	rbuf = malloc(rbs);
	cbuf = malloc(cbs);
	h->huex = allocimage(display, Rect(0,0,r.max.x,1), RGB24, 1, DNofill);
	h->satx = allocimage(display, Rect(0,0,r.max.x,1), RGBA32, 1, DNofill);
	h->saty = allocimage(display, Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	h->valy = allocimage(display, Rect(0,0,1,r.max.y), RGBA32, 1, DNofill);
	if(rbuf == nil || cbuf == nil || 
			h->huex == nil || h->satx == nil || 
			h->saty == nil || h->valy == nil)
		sysfatal("ctorimages failed: %r");
	
	for(i = 0; i < r.max.x; i++){
		pd = ratioxy(Pt(i,ymax), r);
		rgb = hsvdtorgb(hsmap(pd, hues));
		rbuf[3*i] = (uchar)rgb.blue;
		rbuf[3*i+1] = (uchar)rgb.green;
		rbuf[3*i+2] = (uchar)rgb.red;
	}
	loadimage(h->huex, h->huex->r, rbuf, 3 * r.max.x);
	
	for(i = 0; i < r.max.x; i++){
		a = 255.0 * (xmax - i) / xmax; /* invert so S increases with x */
		rbuf[4*i] = (uchar)a;
		rbuf[4*i+1] = (uchar)a;
		rbuf[4*i+2] = (uchar)a;
		rbuf[4*i+3] = (uchar)a;
	}
	loadimage(h->satx, h->satx->r, rbuf, rbs);
	
	for(i = 0; i < r.max.y; i++){
		a = 255.0 * (ymax - i) / ymax; /* invert so S increases with y */
		cbuf[4*i] = (uchar)a;
		cbuf[4*i+1] = (uchar)a;
		cbuf[4*i+2] = (uchar)a;
		cbuf[4*i+3] = (uchar)a;
	}
	loadimage(h->saty, h->valy->r, cbuf, cbs);
	
	for(i = 0; i < r.max.y; i++){
		a = 255.0 * (ymax - i) / ymax; /* invert so V increases with y */
		cbuf[4*i] = (uchar)a;
		cbuf[4*i+1] = 0;
		cbuf[4*i+2] = 0;
		cbuf[4*i+3] = 0;
	}
	loadimage(h->valy, h->valy->r, cbuf, cbs);
	
	free(rbuf);
	free(cbuf);
}

/* 
 * Generate the HS map and static parts.
 * Uses compositing tricks for speed instead of calculating each pixel.
 * Note that this introduces a 1-bit-per-channel maximum error
 *  between the visual and the chosen color.
 * Nobody will notice, right? :D
 */
void
hsvmapresize(Elementile *e, Rectangle r)
{
	HSVmap *h;
	h = e->aux;
	h->r = r;
	genmapimage(h);
	redraw(h);
}

/*
 * Update the current position to match the color.
 */

int
hsvmapupdate(Elementile *e)
{
	HSVmap *h;
	h = e->aux;
	switch(h->maptype){
	case 0:
		h->pos = hsmapinv(h->hsv);
		break;
	case 1:
		h->pos = svmapinv(h->hsv);
		break;
	case 2:
		h->pos = hvmapinv(h->hsv);
		break;
	default:
		break;
	}
	redraw(h);
	return 1;
}

/*
 * Update the current position to the mouse,
 *  and set the color to an opaque one at the current position.
 * Note that some colors are the same in low-saturation areas, 
 *  so don't calculate the position based on color.
 * Redraw.
 */
int
hsvmapmouse(Elementile *e, Mouse m)
{
	HSVmap *h;
	h = e->aux;
	if(m.buttons & 1){
		h->pos = ratioxy(ptclip(m.xy, h->r), h->r);
		switch(h->maptype){
		case 0:
			h->hsv = hsmap(h->pos, h->hsv);
			break;
		case 1:
			h->hsv = svmap(h->pos, h->hsv);
			break;
		case 2:
			h->hsv = hvmap(h->pos, h->hsv);
			break;
		}
		redraw(h);
		h->onchange(h->hsv);
		return 1;
	}
	if(m.buttons == 2){
		switch(menuhit(2, h->mctl, &menu2, nil)){
		case 0:
			h->maptype = 0;
			h->pos = hsmapinv(h->hsv);
			break;
		case 1:
			h->maptype = 1;
			h->pos = svmapinv(h->hsv);
			break;
		case 2:
			h->maptype = 2;
			h->pos = hvmapinv(h->hsv);
			break;
		default:
			break;
		}
		redraw(h);
		return 1;
	}
	return 0;
}

/*
 * insert functionality here
 */
int
hsvmapkeyboard(Elementile*, Rune)
{
	return 0;
}

void
hsvmapfree(Elementile *e)
{
	HSVmap *h;
	h = e->aux;
	freeimage(h->huex);
	freeimage(h->satx);
	freeimage(h->saty);
	freeimage(h->valy);
	freeimage(h->pix);
}