-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_sample.c
161 lines (138 loc) · 4.31 KB
/
rgb_sample.c
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
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
uint8_t * rgb = NULL;
size_t rgb_size;
int width;
int stride;
int height;
static inline int clip_c( int c )
{
if (c < 0)
return 0;
if (c > 255)
return 255;
return c;
}
static inline int make_rgb( int r, int g, int b) {
return clip_c(r) | clip_c(g) << 8 | clip_c(b) << 16;
}
static void load_rgb(char * img)
{
int num_channels;
unsigned char* bitmap = stbi_load(img, &width, &height, &num_channels, 0);
if (bitmap == NULL || width * height == 0) {
printf("load image error!\n");
exit(1);
}
rgb_size = width*height*3;
stride = width*3;
rgb = realloc(rgb,rgb_size);
switch (num_channels) {
case 1: {
uint8_t *rgb_p = rgb;
uint8_t *gray8_p = bitmap;
size_t n=width*height;
while (n--) {
const uint8_t g8 = *gray8_p++;
*rgb_p++ = g8;
*rgb_p++ = g8;
*rgb_p++ = g8;
}
break;
}
case 3:
memcpy(rgb,bitmap,rgb_size);
break;
case 4: {
uint8_t *rgb_p = rgb;
uint8_t *bitmap_p = bitmap;
size_t n=width*height;
while (n--) {
*rgb_p++ = bitmap_p[0];
*rgb_p++ = bitmap_p[1];
*rgb_p++ = bitmap_p[2];
bitmap_p += 4;
}
break;
}
default:
printf("number of channels == %d - is not support!!!\n", num_channels);
exit(1);
}
free(bitmap);
}
static void save_rgb(char * img)
{
size_t i = strlen(img)-1;
if (i > 3) {
if (
tolower(img[i-2]) == 'p' &&
tolower(img[i-1]) == 'n' &&
tolower(img[i]) == 'g') {
stbi_write_png(img, width,height, 3, rgb, stride);
return;
};
if (
tolower(img[i-2]) == 'j' &&
tolower(img[i-1]) == 'p' &&
tolower(img[i]) == 'g') {
stbi_write_jpg (img, width,height, 3, rgb, 95);
return;
};
if (
tolower(img[i-2]) == 't' &&
tolower(img[i-1]) == 'g' &&
tolower(img[i]) == 'a') {
stbi_write_tga(img, width,height, 3, rgb);
return;
};
}
stbi_write_bmp(img,width,height,3,rgb);
}
static inline void plot(int x, int y, int color)
{
if (x>=0 && y>=0 && x < width && y < height)
memcpy( rgb + y*stride +x*3, &color, 3);
}
static void draw_circleb( int CX, int CY, int R, int color)
{int a,b,c,x,y; //
x=R; //
///////////////////////////////////////////////////////////
if(x==0) plot(CX ,CY, color ); //Bresenham
else{ b=y=0; //
for (a=-x;a<=x;a++) plot(CX+a,CY, color ); //
do{ //
a=b+1-(x<<1); //
c=a+1+(y<<1); //
b+=c-a; //
if (abs(a)<b && abs(a)<abs(c)) {x--;b=a;} //
else {if (b<abs(a) && b<abs(c)) y++; //
else {x--;y++;b=c;} //
for (a=-x;a<=x;a++) {plot(CX+a,CY+y, color); //
plot(CX+a,CY-y, color); //
} //
} //
} //
while(y<=R); //
} //
///////////////////////////////////////////////////////////
}
int main(int argc, char ** argv) {
if (argc < 3) {
printf("usage: <in_image> <out_image>\n");
return 0;
}
load_rgb(argv[1]);
draw_circleb(width/2-50,height/2+50,width/8,make_rgb(255,0,0));
draw_circleb(width/2+50,height/2+50,width/8,make_rgb(0,255,0));
draw_circleb(width/2,height/2-50,width/8,make_rgb(0,0,255));
save_rgb(argv[2]);
//gcc -Ofast rgb_sample.c -o rgb_sample.exe
}