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

color-correction-yuv.c « sequence « plugins « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a78fc7d4b6beb4280a5476a91a1d25bb3d0e55f (plain)
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
/*
 * Color Correction Plugin (YUV Version) 0.01
 *
 * Copyright (c) 2005 Peter Schlaile
 *
 * 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.
 *
 */

#include "math.h"
#include "plugin.h"
#include <stdio.h>

char name[]= "Color Correction";

VarStruct varstr[]= {
	{ NUMSLI|FLO, "St Y:", 0.0,	-1.0,	1.0, "Setup Y"}, 
	{ NUMSLI|FLO, "Gn Y:",  1.0,	0.0,	10.0,"Gain Y"},
	{ NUMSLI|FLO, "Ga Y:", 1.0,	0.0,	10.0, "Gamma Y"},

	{ NUMSLI|FLO, "Lo S:",  1.0,	0.0,	10.0,"Saturation Shadows"},
	{ NUMSLI|FLO, "Md S:",  1.0,	0.0,	10.0,"Saturation Midtones"},
	{ NUMSLI|FLO, "Hi S:",  1.0,	0.0,	10.0,"Saturation Highlights"},

	{ NUMSLI|FLO, "MA S:",  1.0,	0.0,	10.0,"Master Saturation"}, 
	{ NUMSLI|FLO, "Lo T:",  0.25, 0.0, 1.0,
	  "Saturation Shadow Thres"}, 
	{ NUMSLI|FLO, "Hi T:",  0.75, 0.0, 1.0,
	  "Saturation Highlights Thres"}, 
	{ TOG|INT,	"Debug", 0.0,	0.0,	1.0,   
	  "Show curves as overlay"}, 
};

typedef struct Cast {
	float setup_y;
	float gain_y;
	float gamma_y;
	
	float sat_shadows;
	float sat_midtones;
	float sat_highlights;

	float master_sat;
	float lo_thres;
	float hi_thres;
	int debug;
} Cast;

float cfra;

void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);

int plugin_seq_getversion(void) { return B_PLUGIN_VERSION;}
void plugin_but_changed(int but) {}
void plugin_init() {}

void plugin_getinfo(PluginInfo *info) {
	info->name= name;
	info->nvars= sizeof(varstr)/sizeof(VarStruct);
	info->cfra= &cfra;

	info->varstr= varstr;

	info->init= plugin_init;
	info->seq_doit= (SeqDoit) plugin_seq_doit;
	info->callback= plugin_but_changed;
}

static void rgb_to_yuv(float rgb[3], float yuv[3]) {
	yuv[0]= 0.299*rgb[0] + 0.587*rgb[1] + 0.114*rgb[2];
	yuv[1]= 0.492*(rgb[2] - yuv[0]);
	yuv[2]= 0.877*(rgb[0] - yuv[0]);
	
	/* Normalize */
	yuv[1] /= 0.436;
	yuv[2] /= 0.615;
}

static void yuv_to_rgb(float yuv[3], float rgb[3]) {
	yuv[1] *= 0.436;
	yuv[2] *= 0.615;

	rgb[0] = yuv[2]/0.877 + yuv[0];
	rgb[2] = yuv[1]/0.492 + yuv[0];
	rgb[1] = (yuv[0] - 0.299*rgb[0] - 0.114*rgb[2]) / 0.587;
	if (rgb[0] > 1.0) {
		rgb[0] = 1.0;
	}
	if (rgb[0] < 0.0) {
		rgb[0] = 0.0;
	}
	if (rgb[1] > 1.0) {
		rgb[1] = 1.0;
	}
	if (rgb[1] < 0.0) {
		rgb[1] = 0.0;
	}
	if (rgb[2] > 1.0) {
		rgb[2] = 1.0;
	}
	if (rgb[2] < 0.0) {
		rgb[2] = 0.0;
	}
}

void plugin_seq_doit(Cast *cast, float facf0, float facf1, int width, 
	int height, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use) {
	char *dest, *src1, *src2;
	int x, y, c;
	float rgb[3];
	float yuv[3];
	float gamma_table[256];
	float uv_table[256];
	float *destf = out->rect_float;
	float *src1f;
	
	if (!ibuf1) return;

	dest= (char *) out->rect;
	src1= (char *) ibuf1->rect;
	src1f= ibuf1->rect_float;

	for (y = 0; y < 256; y++) {
		float v = 1.0 * y / 255;
		v += cast->setup_y;
		v *= cast->gain_y;
		v = pow(v, cast->gamma_y);
		if ( v > 1.0) {
			v = 1.0;
		} else if (v < 0.0) {
			v = 0.0;
		}
		gamma_table[y] = v * 255;
	}

	for (y = 0; y < 256; y++) {
		float v = 1.0;
		v *= cast->master_sat;
		if (y < cast->lo_thres * 255) {
			v *= cast->sat_shadows;
		} else if (y > cast->hi_thres * 255) {
			v *= cast->sat_highlights;
		} else {
			v *= cast->sat_midtones;
		}
		uv_table[y] = v;
	}


	for (y = 0; y < height; y++) {
		for (x = 0; x < width; x++) {
			float fac;
			if (out->rect_float) {
				rgb[0]= (float)src1f[0]/255.0;
				rgb[1]= (float)src1f[1]/255.0;
				rgb[2]= (float)src1f[2]/255.0;
			} else {
				rgb[0]= (float)src1[0]/255.0;
				rgb[1]= (float)src1[1]/255.0;
				rgb[2]= (float)src1[2]/255.0;
			}
			rgb_to_yuv(rgb, yuv);

			yuv[0] = gamma_table[(int) (yuv[0] * 255.0)] / 255.0;
			fac = uv_table[(int) (255.0 * yuv[0])];

			yuv[1] = yuv[1] * fac;
			yuv[2] = yuv[2] * fac;
			if (yuv[1] > 1.0) {
				yuv[1] = 1.0;
			}
			if (yuv[1] < -1.0) {
				yuv[1] = -1.0;
			}
			if (yuv[2] > 1.0) {
				yuv[2] = 1.0;
			}
			if (yuv[2] < -1.0) {
				yuv[2] = -1.0;
			}
			yuv_to_rgb(yuv, rgb);
			
			if (out->rect_float) {
				*destf++ = rgb[0];
				*destf++ = rgb[1];
				*destf++ = rgb[2];
				destf++;
				src1f += 4;
			} else {
				*dest++ = rgb[0]*255.0;
				*dest++ = rgb[1]*255.0;
				*dest++ = rgb[2]*255.0;
				dest++;
				src1 += 4;
			}
		}
	}

	if (cast->debug) {
		dest= (char *) out->rect;
		for (c = 0; c < 10; c++) {
			x = 0;
			for (y = 0; y < 256; y++) {
				char val = gamma_table[y];
				while (x < y * width / 255) {
					*dest++ = val;
					*dest++ = val;
					*dest++ = val;
					dest++;
					x++;
				}
			}
		}
		for (c = 0; c < 10; c++) {
			x = 0;
			for (y = 0; y < 256; y++) {
				char val = uv_table[y] * 255.0/10.0;
				while (x < y * width / 255) {
					*dest++ = val;
					*dest++ = val;
					*dest++ = val;
					dest++;
					x++;
				}
			}
		}
	}
}