-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunLengthEncoding.c
424 lines (385 loc) · 8.83 KB
/
RunLengthEncoding.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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
////////////////////////////Umut Güzel/////////////////////////////////////////
////////////////////////////18011004//////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define BUFFER 200
typedef struct pgm{
char type[5];
int row;
int col;
int max;
int **mat;
}pgm;
pgm* readPGMfile(char* filename, pgm *pgmdata){
int i,j,rw,cl;
FILE *fp;
char type[5];
char check[BUFFER];
fp=fopen(filename,"r");
if(fp==NULL){
printf("open attempt failed!");
exit(1);
}
fscanf(fp,"%s\n",type);
strcpy(pgmdata->type,type);
if(strcmp(type,"P2")!=0){
printf("invalid file type");
exit(1);
}
fgets(check,BUFFER,fp);
if(check[0]=='#'){
fgets(check,BUFFER,fp);
}
sscanf(check,"%d %d",&cl,&rw);
pgmdata->col=cl;
pgmdata->row=rw;
fscanf(fp,"%d",&pgmdata->max);
pgmdata->mat=(int**)malloc(rw*sizeof(int*));
for(i=0;i<rw;i++){
pgmdata->mat[i]=(int*)malloc(cl*sizeof(int));
}
for(i=0;i<rw;i++){
for(j=0;j<cl;j++){
fscanf(fp,"%d ",&pgmdata->mat[i][j]);
}
}
fclose(fp);
return pgmdata;
}
void encodePGM(pgm *pgmdata){
int i,j,cnt,value;
FILE *fp;
bool last_op;
fp=fopen("test_encoded.txt","w");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
value=pgmdata->mat[0][0];
cnt=0;
fprintf(fp,"%c%c\n",pgmdata->type[0],pgmdata->type[1]);
fprintf(fp,"%d %d\n",pgmdata->col,pgmdata->row);
fprintf(fp,"%d\n",pgmdata->max);
for(i=0;i<pgmdata->row;i++){
for(j=0;j<pgmdata->col;j++){
if(value==pgmdata->mat[i][j]){
cnt++;
last_op=true;
}else{
fprintf(fp,"%d %d \n",cnt,value);
value=pgmdata->mat[i][j];
cnt=1;
last_op=false;
}
}
}
if(last_op==true){
fprintf(fp,"%d %d ",cnt,value);
value=pgmdata->mat[i-1][j-1];
}
fclose(fp);
}
bool control(){
FILE *fp;
char type[5];
int rw,cl,tmp_cnt,tmp_val=0,past_val,max;
fp=fopen("test_encoded.txt","r");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
fscanf(fp,"%s",type);
fscanf(fp,"%d %d",&rw,&cl);
fscanf(fp,"%d",&max);
int pxcnt=0;
while(!feof(fp)){
past_val=tmp_val;
fscanf(fp,"%d %d\n",&tmp_cnt,&tmp_val);
pxcnt+=tmp_cnt;
if(tmp_val<0&&tmp_val>255){
fclose(fp);
printf("a color value is out of bound!!\n");
return false;
}
if(tmp_val==past_val){
fclose(fp);
printf("%d %d\n",past_val,tmp_val);
printf("sequential values cannot be equal!!\n");
return false;
}
}
if(pxcnt!=(rw*cl)){
fclose(fp);
printf("pixel count is not equal to row*column!!\n");
return false;
}
fclose(fp);
printf("this file is compatible to decode\n");
return true;
}
void decodePGM(){
int i,j,rw,cl,max,val,repeat,total,cnt=0;
char type[5];
FILE *fp,*fp2;
fp=fopen("test_decoded.pgm","w");
fp2=fopen("test_encoded.txt","r");
fscanf(fp2,"%s",type);
fprintf(fp,"%c%c\n",type[0],type[1]);
fscanf(fp2,"%d %d",&cl,&rw);
fprintf(fp,"%d %d\n",cl,rw);
fscanf(fp2,"%d",&max);
fprintf(fp,"%d\n",max);
total=rw*cl;
while(total>0){
fscanf(fp2,"%d %d",&repeat,&val);
for(i=0;i<repeat;i++){
fprintf(fp,"%d ",val);
cnt++;
if(cnt==cl){
cnt=0;
fprintf(fp,"\n");
}
}
total=total-repeat;
}
}
void changeColor(int old_color,int new_color){
int i,j,total,rw,cl,idx1=0,cnt=0,val,repeat;
bool last_op;
FILE *fp;
fp=fopen("test_encoded.txt","r");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
pgm *p;
p=(pgm*)malloc(sizeof(pgm));
fscanf(fp,"%s",p->type);
fscanf(fp,"%d %d",&p->col,&p->row);
fscanf(fp,"%d",&p->max);
printf("\ntype: %c%c\nColumns: %d \nRows:%d \nMax Color Value%d\n\n",p->type[0],p->type[1],p->col,p->row,p->max);
rw=p->row;
cl=p->col;
p->mat=(int**)malloc(rw*sizeof(int*));
for(i=0;i<rw;i++){
p->mat[i]=(int*)malloc(cl*sizeof(int));
}
total=rw*cl;
while(total>0){
fscanf(fp,"%d %d",&repeat,&val);
for(i=0;i<repeat;i++){
if(val==old_color){
val=new_color;
}
p->mat[idx1][cnt]=val;
//printf("%d ",p->mat[idx1][cnt]);
cnt++;
if(cnt==cl){
cnt=0;
idx1++;
}
}
total=total-repeat;
}
fclose(fp);
fp=fopen("test_encoded.txt","w");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
fprintf(fp,"%c%c\n",p->type[0],p->type[1]);
fprintf(fp,"%d %d\n",p->col,p->row);
fprintf(fp,"%d\n",p->max);
val=p->mat[0][0];
cnt=0;
for(i=0;i<p->row;i++){
for(j=0;j<p->col;j++){
printf("%d ",p->mat[i][j]);
if(val==p->mat[i][j]){
cnt++;
last_op=true;
}else{
fprintf(fp,"%d %d\n",cnt,val);
val=p->mat[i][j];
cnt=1;
last_op=false;
}
}
printf("\n");
}
if(last_op==true){
fprintf(fp,"%d %d ",cnt,val);
val=p->mat[i-1][j-1];
}
fclose(fp);
free(p);
}
void changePixelVal(int rwco,int clco, int new_val){
int i,j,total,rw,cl,idx1=0,cnt=0,val,repeat;
bool last_op;
FILE *fp;
fp=fopen("test_encoded.txt","r");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
pgm *p;
p=(pgm*)malloc(sizeof(pgm));
fscanf(fp,"%s",p->type);
fscanf(fp,"%d %d",&p->col,&p->row);
fscanf(fp,"%d",&p->max);
printf("\ntype: %c%c\nColumns: %d \nRows:%d \nMax Color Value%d\n\n",p->type[0],p->type[1],p->col,p->row,p->max);
rw=p->row;
cl=p->col;
p->mat=(int**)malloc(rw*sizeof(int*));
for(i=0;i<rw;i++){
p->mat[i]=(int*)malloc(cl*sizeof(int));
}
total=rw*cl;
while(total>0){
fscanf(fp,"%d %d",&repeat,&val);
for(i=0;i<repeat;i++){
if(idx1==rwco&&cnt==clco){
p->mat[idx1][cnt]=new_val;
}else{
p->mat[idx1][cnt]=val;
}
cnt++;
if(cnt==cl){
cnt=0;
idx1++;
}
}
total=total-repeat;
}
fclose(fp);
fp=fopen("test_encoded.txt","w");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
fprintf(fp,"%c%c\n",p->type[0],p->type[1]);
fprintf(fp,"%d %d\n",p->col,p->row);
fprintf(fp,"%d\n",p->max);
val=p->mat[0][0];
cnt=0;
for(i=0;i<p->row;i++){
for(j=0;j<p->col;j++){
if(val==p->mat[i][j]){
cnt++;
last_op=true;
}else{
fprintf(fp,"%d %d\n",cnt,val);
val=p->mat[i][j];
cnt=1;
last_op=false;
}
printf("%d ",p->mat[i][j]);
}
printf("\n");
}
if(last_op==true){
fprintf(fp,"%d %d ",cnt,val);
val=p->mat[i-1][j-1];
}
fclose(fp);
free(p);
}
void createHistogram(){
int rw,cl,max,cnt,val,i;
FILE *fp;
char type[5];
int colors[256];
fp=fopen("test_encoded.txt","r");
if(fp==NULL){
printf("open attempt failed!!");
exit(1);
}
fscanf(fp,"%s",type);
fscanf(fp,"%d %d",&rw,&cl);
fscanf(fp,"%d",&max);
for(i=0;i<256;i++){
colors[i]=0;
}
while(!feof(fp)){
fscanf(fp,"%d %d\n",&cnt,&val);
colors[val]+=cnt;
}
printf("\nColor Value count\n___________ _______\n");
for(i=0;i<256;i++){
if(colors[i]!=0){
printf("%d---------------> %d\n",i,colors[i]);
}
}
fclose(fp);
}
int main(){
int input,input2,x,y,z;
pgm *pgmdata;
char filename[25];
pgmdata=(pgm*)malloc(sizeof(pgm));
input=6;
while(input!=0){
printf("Author:Umut Guzel-18011004\n####################MAIN MENU####################\n1.read PGM file\n2.Compress PGM file\n3.decode PGM file\n4.Operations(change color,change pixel value, create histogram)\n0.exit\n");
printf("input: ");
scanf("%d",&input);
switch(input){
case 1:
printf("please enter name of the file: ");
scanf("%s",filename);
readPGMfile(filename,pgmdata);
printf("operation successful\n");
break;
case 2:
encodePGM(pgmdata);
printf("operation successful\n");
break;
case 3:
if(control()==true){
decodePGM();
printf("operation successful\n");
}
break;
case 4:
printf("\n\nPlease choose an operation: \n1.Change Color\n2.Change a pixel's value\n3.Create Histogram\n0.main menu\n");
printf("input: ");
scanf("%d",&input2);
switch(input2){
case 1:
printf("please enter the color value you want to change: ");
scanf("%d",&x);
printf("please enter the new color value: ");
scanf("%d",&y);
if(x>=0&&x<=255&&y>=0&&y<=255){
changeColor(x,y);
}else{
printf("the color value out of bound");
}
break;
case 2:
printf("please enter the row: ");
scanf("%d",&x);
printf("please enter the column: ");
scanf("%d",&y);
printf("please enter the new color value: ");
scanf("%d",&z);
changePixelVal(x,y,z);
break;
case 3:
createHistogram();
break;
case 0:
break;
default:
printf("you entered invalid value...returning to main menu.\n");
}
case 0:
break;
default:
printf("you entered invalid value...please try again.\n");
}
}
return 0;
}