Skip to content
Snippets Groups Projects
Commit dac545d4 authored by Nayoung Kim's avatar Nayoung Kim
Browse files

Update lec11

parent 24362aec
No related branches found
No related tags found
No related merge requests found
......@@ -1515,3 +1515,108 @@ int main()
fprintf(stdout, "%d + %d : %d \n", ia, ib, ic);
}
```
## **Lecture 11**
### rgba.c 코드
```
#include <stdio.h>
typedef unsigned int t_rgba;
// input value must be 0~255
/*
방법 1. 함수 만들기
unsigned int fromRGBA(int r, int g, int b, int a)
{
if (r > 255 || r < 0)
r = 0;
if (g > 255 || g < 0)
g = 0;
if (b > 255 || b < 0)
b = 0;
if (a > 255 || a < 0)
a = 0;
if문 성능 나빠짐
// return r*256*256*256 + g*256*256 + b*256 + a*1;
// return ((r<<24)+(g<<16)+(b<<8)+a);
// return ((r<<(24+g))<<(16+b))<<(8+1);
// return (r<<24|g<<16|b<<8|a);
return ((r&0xff)<<24)|((g&0xff)<<16)|((b&0xff)<<8)|(a&0xff);
}
*/
// 방법 2. macro
#define fromRGBA(r,g,b,a) ((((r)&0xff)<<24)|(((g)&0xff)<<16)|(((b)&0xff)<<8)|((a)&0xff))
#define F_NUM_1_255 (1.0f/255.0f)
t_rgba mul_float(t_rgba c1, t_rgba c2){
float r1, g1, b1, a1;
float r2, g2, b2, a2;
int ir, ig, ib, ia;
r1 = (float) ((c1 >> 24) & 0xff) * F_NUM_1_255;
g1 = (float) ((c1 >> 16) & 0xff) * F_NUM_1_255;
b1 = (float) ((c1 >> 8) & 0xff) * F_NUM_1_255;
a1 = (float) ((c1 >> 0) & 0xff) * F_NUM_1_255;
r2 = (float) ((c2 >> 24) & 0xff) * F_NUM_1_255;
g2 = (float) ((c2 >> 16) & 0xff) * F_NUM_1_255;
b2 = (float) ((c2 >> 8) & 0xff) * F_NUM_1_255;
a2 = (float) ((c2 >> 0) & 0xff) * F_NUM_1_255;
ir = (int) ((r1 * r2) * 255.0f);
ig = (int) ((g1 * g2) * 255.0f);
ib = (int) ((b1 * b2) * 255.0f);
ia = (int) ((a1 * a2) * 255.0f);
return fromRGBA(ir, ig, ib, ia);
}
t_rgba mul_int(t_rgba c1, t_rgba c2)
{
unsigned int r1, g1, b1, a1;
unsigned int r2, g2, b2, a2;
unsigned int r, g, b, a;
r1 = c1 >> 24 ; r2 = c2 >> 24 ;
g1 = (c1 >> 16) & 0xff ; g2 = (c2 >> 16) & 0xff;
b1 = (c1 >> 16) & 0xff ; b2 = (c2 >> 8) & 0xff;
a1 = c1 & 0xff ; a2 = c2 & 0xff;
r= (r1 * r2) / 255;
g= (g1 * g2) / 255;
b= (b1 * b2) / 255;
a= (a1 * a2) / 255;
return fromRGBA(r,g,b,a);
}
int main()
{
int red, green, blue, alpha;
t_rgba rgba_1, rgba_2, rgba_3;
// input value must be 0~255 : 0.0~1.0
// rgba_1 [rrrrrrrr][gggggggg][bbbbbbbb][aaaaaaaa]
printf("input 4 values with 0~255 : ");
scanf("%d %d %d %d", &red, &green, &blue, &alpha);
rgba_1 = fromRGBA(red, green, blue, alpha);
printf("%d %d %d %d : %u 0x%08x\n", red, green, blue, alpha, rgba_1, rgba_1);
rgba_2 = mul_float(rgba_1, rgba_1);
printf("%d %d %d %d : %u 0x%08x\n", red, green, blue, alpha, rgba_2, rgba_2);
rgba_3 = mul_int(rgba_1, rgba_1);
printf("%d %d %d %d : %u 0x%08x\n", red, green, blue, alpha, rgba_3, rgba_3);
}
```
출력 결과
```
input 4 values with 0~255 : 127 127 127 127
127 127 127 127 : 2139062143 0x7f7f7f7f
127 127 127 127 : 1061109567 0x3f3f3f3f
127 127 127 127 : 1061109567 0x3f3f3f3f
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment