-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgtochar.c
executable file
·216 lines (177 loc) · 5.02 KB
/
imgtochar.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
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
#include <stdio.h>
#include <stdlib.h>
#include <IL/il.h>
#include <IL/ilu.h>
#define displayInfo 1
int caracs;
const char chars[17] = {'M','N','D','8','0','Z','$','7','I','?','+','=','~',':',',','.',' '};
char err[200] = "Usage: imgtochar [options] imagefile\nOptions:\n\t-h\t\tPrint this help\n\t-c\t\tNumber of differents chars\n\t-r\t\t(x y) Number of the output resolution\n";
int vec[257];
int width;
int height;
void error(char * msg){
printf("%s\n", msg);
exit(1);
}
void scale(int newX, int newY){
iluScale(newX, newY, 0);
}
unsigned int setup(char * filename){
//Initialize and open the image
ILboolean success;
unsigned int imageID;
ilInit();
ilGenImages(1, &imageID);
ilBindImage(imageID);
success = ilLoadImage((ILstring)filename);
if (!success) {
ilDeleteImages(1, &imageID);
error("Failed to open image!");
}
return imageID;
}
ILubyte ** RGBtoGray(){
ILubyte * bytes = ilGetData();
ILubyte ** image = (ILubyte**)malloc(sizeof(ILubyte*)*height);
int i, j;
for (i = 0; i < height; i++){
image[i] = (ILubyte*)malloc(sizeof(ILubyte)*width);
for (j = 0; j < width; j++){
//printf( "%d,%d ->", i,j);
//printf( "(%d,", bytes[(i*width + j)*3 + 0]);
//printf( "%d,", bytes[(i*width + j)*3 + 1]);
//printf( "%d) -> ", bytes[(i*width + j)*3 + 2]);
image[i][j] = ((bytes[(i*width + j)*3])+(bytes[(i*width + j)*3+1])+(bytes[(i*width + j)*3+2]))/3;
//image[i][j] = bytes[(i*width + j)*3];
//printf("%d\n", image[i][j]);
}
// getchar();
// printf("\n");
}
return image;
}
void setVec(int caracs){
//caracs have the number of diferents chars will appear on the .txt, 17 is the max in the moment
//in the future caracs will be a parameter, and will have a GUI too
int i,j;
if(caracs == 2){
for(i=0;i<129;i++){
vec[i] = 'X';
}
for(i=129;i<258;i++){
vec[i] = ' ';
}
}
else{
for (i=1; i<=caracs; i++){
int x = ((i-1)*(256./caracs));
int y = ((i)*(256./caracs));
int z = ((x/256.)*17);
for (j=x;j<y;j++){
vec[j] = chars[z];
}
}
}
}
void writeOut(ILubyte ** image){
//Just create the output file with the chars
FILE * out = fopen("image.txt", "w");
if (out == NULL){
error("Failed to write the output");
}
int i, j;
for (i = 0; i < height; i++){
for (j = 0; j < width; j++){
fprintf(out, "%c", vec[image[i][j]]);
printf("%c", vec[image[i][j]]);
//printf("%c", vec[image[i][j]]);
//printf("%d -> %c\n", image[i][j], vec[image[i][j]]);
}
fprintf(out, "\n");
printf("\n");
}
fclose(out);
}
void info(){
printf("Width: %d, Height %d, Bytes per Pixel %d\n",
ilGetInteger(IL_IMAGE_WIDTH),
ilGetInteger(IL_IMAGE_HEIGHT),
ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL));
char *s;
switch(ilGetInteger(IL_IMAGE_FORMAT)) {
case IL_COLOR_INDEX : s = "IL_COLOR_INDEX"; break;
case IL_ALPHA : s = "IL_ALPHA"; break;
case IL_RGB : s = "IL_RGB"; break;
case IL_RGBA : s = "IL_RGBA"; break;
case IL_BGR : s = "IL_BGR"; break;
case IL_BGRA : s = "IL_BGRA"; break;
case IL_LUMINANCE : s = "IL_LUMINANCE"; break;
case IL_LUMINANCE_ALPHA : s = "IL_LUMINANCE_ALPHA"; break;
}
printf("Format %s\n", s);
if (ilGetInteger(IL_IMAGE_FORMAT) != IL_RGB){
printf("We do not tested this image format, but we will try to run...\n");
}
switch(ilGetInteger(IL_IMAGE_TYPE)) {
case IL_BYTE : s = "IL_BYTE"; break;
case IL_UNSIGNED_BYTE : s = "IL_UNSIGNED_BYTE"; break;
case IL_SHORT : s = "IL_SHORT"; break;
case IL_UNSIGNED_SHORT : s = "IL_UNSIGNED_SHORT"; break;
case IL_INT : s = "IL_INT"; break;
case IL_UNSIGNED_INT : s = "IL_UNSIGNED_INT"; break;
case IL_FLOAT : s = "IL_FLOAT"; break;
case IL_DOUBLE : s = "IL_DOUBLE"; break;
case IL_HALF : s = "IL_HALF"; break;
}
printf("Data type: %s\n", s);
if (ilGetInteger(IL_IMAGE_TYPE) != IL_UNSIGNED_BYTE){
printf("We do not tested this data type, but we will try to run...\n");
}
}
int main(int argc, char **argv){
char * filename = NULL;
int newX = 0, newY = 0, arg = 1, caracs = 17;
while(arg < argc){
if (argv[arg][0] == '-'){
if (arg + 1 == argc){error(err);}
char opt = argv[arg][1];
switch (opt){
case 'r':
if (arg + 2 >= argc){error(err);}
newX = atoi(argv[++arg]);
newY = atoi(argv[++arg]);
arg++;
break;
case 'c':
caracs = atoi(argv[++arg]);
if (caracs < 2 || caracs > 17){
error("Wrong number of chars, must to be more then 1 and less than 18");
}
arg++;
break;
default:
error(err);
}
}
else{
filename = argv[arg++];
}
}
if (filename == NULL){
error(err);
}
setup(filename);
if (newX > 0 && newY > 0){
scale(newX, newY);
}
width = ilGetInteger(IL_IMAGE_WIDTH);
height = ilGetInteger(IL_IMAGE_HEIGHT);
if (displayInfo){
info();
}
ILubyte ** image = RGBtoGray();
setVec(caracs);
writeOut(image);
printf("done.\n");
return 0;
}