Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "bmp_header.h"
- #include<string.h>
- typedef struct {
- unsigned char r, g, b;
- }pixel;
- typedef struct {
- int x,y;
- struct nod *next;
- }nod;
- //Functia pentru task 2
- pixel filtru ( pixel **img, pixel **F, int height, int width, int mat[3][3])
- {
- int red=0;
- int x, y;
- //Am parcurs matricele si am calculat pentru fiecare caz particular valoarea pixelului
- for ( x = 0; x < height; x++ )
- {
- for ( y = 0; y < width; y++ )
- {
- if ( 0 < x && x < height-1 && 0 < y && y < width-1 )
- {
- red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
- img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
- img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
- }
- if ( x == 0 )
- {
- if ( y == 0 )
- {
- red = img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
- img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
- }
- if ( y == (width-1) )
- {
- red = img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1]+
- img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1];
- }
- if ( 0 < y && y < (width-1) )
- {
- red = img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
- img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
- }
- }
- if ( x == height -1)
- {
- if ( y == 0 )
- {
- red = img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
- img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2];
- }
- if ( y == (width-1) )
- {
- red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1]+
- img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1];
- }
- if ( 0 < y && y < (width-1) )
- {
- red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
- img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2];
- }
- }
- if ( y == 0 && 0 < x && x < (height-1) )
- {
- red = img[x-1][y].r*mat[0][1] + img[x-1][y+1].r*mat[0][2]+
- img[x][y].r*mat[1][1] + img[x][y+1].r*mat[1][2]+
- img[x+1][y].r*mat[2][1] + img[x+1][y+1].r*mat[2][2];
- }
- if ( y == width-1 && 0 < x && x < (height-1) )
- {
- red = img[x-1][y-1].r*mat[0][0] + img[x-1][y].r*mat[0][1]+
- img[x][y-1].r*mat[1][0] + img[x][y].r*mat[1][1]+
- img[x+1][y-1].r*mat[2][0] +img[x+1][y].r*mat[2][1];
- }
- if (-1 < red && red < 256)
- {
- F[x][y].r = red;
- F[x][y].g = red;
- F[x][y].b = red;
- }
- else
- {
- if ( red > 255)
- {
- F[x][y].r = 255;
- F[x][y].g = 255;
- F[x][y].b = 255;
- }
- else
- {
- F[x][y].r = 0;
- F[x][y].g = 0;
- F[x][y].b = 0;
- }
- }
- }
- }
- }
- void fill ( pixel **img, int height, int width, int x, int y, int **viz, int id, int tresh, pixel val)
- {
- int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
- int i;
- int modul;
- nod *aux = calloc(1, sizeof(nod));
- aux->x = x;
- aux->y = y;
- aux->next = NULL;
- nod *cap = calloc(1, sizeof(nod));
- nod *coada = calloc(1, sizeof(nod));
- cap = aux;
- coada = aux;
- do
- {
- aux = cap;
- img[aux->x][aux->y] = val;
- viz[aux->x][aux->y] = id;
- for ( i=0; i<4; i++)
- {
- if ( 0 <= aux->x+dx[i] && aux->x+dx[i]<height && 0 <= aux->y+dy[i] && aux->y+dy[i]<width && viz[aux->x+dx[i]][aux->y+dy[i]] == 0)
- {
- modul = abs(img[aux->x][aux->y].r - img[aux->x+dx[i]][aux->y+dy[i]].r) +
- abs(img[aux->x][aux->y].g - img[aux->x+dx[i]][aux->y+dy[i]].g) +
- abs(img[aux->x][aux->y].b - img[aux->x+dx[i]][aux->y+dy[i]].b);
- if ( modul <= tresh )
- {
- nod *vecin = calloc(1, sizeof(nod));
- vecin->x = aux->x+dx[i];
- vecin->y = aux->y+dy[i];
- vecin->next = NULL;
- coada->next = vecin;
- coada = vecin;
- }
- }
- }
- cap = aux->next;
- } while(cap->next != NULL);
- }
- int main()
- {
- pixel **img, **aux;
- bmp_fileheader file;
- bmp_infoheader info;
- FILE *bin_file, *input_text, *arhiva_file;
- char image_name[100], arhiva[100], image_bw[100], image_dec[100];
- char image_f1[100], image_f2[100], image_f3[100];
- int prag, k;
- short i, j;
- //Citire din input.txt
- input_text = fopen ("input.txt", "rt");
- if (input_text == NULL)
- {
- fprintf(stderr, "ERROR: Can't open file");
- return -1;
- }
- fscanf(input_text, "%s", image_name);
- fscanf(input_text, "%d", &prag);
- fscanf(input_text, "%s", arhiva);
- fclose(input_text);
- //Citire fisier binar al imaginii
- bin_file = fopen (image_name, "rb");
- if (bin_file == NULL)
- {
- printf("ERROR: Can't open file");
- return -1;
- }
- //Fileheader
- fread(&file.fileMarker1, sizeof(char), 1, bin_file);
- fread(&file.fileMarker2, sizeof(char), 1, bin_file);
- fread(&file.bfSize, sizeof(int), 1, bin_file);
- fread(&file.unused1, sizeof(short), 1, bin_file);
- fread(&file.unused2, sizeof(short), 1, bin_file);
- fread(&file.imageDataOffset, sizeof(int), 1, bin_file);
- //Infoheader
- fread(&info.biSize, sizeof(int), 1, bin_file);
- fread(&info.width, sizeof(int), 1, bin_file);
- fread(&info.height, sizeof(int), 1, bin_file);
- fread(&info.planes, sizeof(short), 1, bin_file);
- fread(&info.bitPix, sizeof(short), 1, bin_file);
- fread(&info.biCompression, sizeof(int), 1, bin_file);
- fread(&info.biSizeImage, sizeof(int), 1, bin_file);
- fread(&info.biXPelsPerMeter, sizeof(int), 1, bin_file);
- fread(&info.biYPelsPerMeter, sizeof(int), 1, bin_file);
- fread(&info.biClrUsed, sizeof(int), 1, bin_file);
- fread(&info.biClrImportant, sizeof(int), 1, bin_file);
- //Citirea matricei de pixeli
- img = calloc(info.height, sizeof(pixel *));
- for ( i=0; i<info.height; i++)
- {
- img[i] = calloc(info.width, sizeof(pixel));
- }
- fseek(bin_file, file.imageDataOffset, SEEK_SET);
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- fread(&img[i][j].b, sizeof(char), 1, bin_file);
- fread(&img[i][j].g, sizeof(char), 1, bin_file);
- fread(&img[i][j].r, sizeof(char), 1, bin_file);
- }
- }
- aux = calloc(info.height, sizeof(pixel *));
- for ( i=0; i<info.height; i++)
- {
- aux[i] = calloc(info.width, sizeof(pixel));
- }
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- aux[i][j].b = img[i][j].b;
- aux[i][j].g = img[i][j].g;
- aux[i][j].r = img[i][j].r;
- }
- }
- fclose(bin_file);
- //Task 1 - Transformarea imaginii in alb-negru
- int an;
- for ( i=info.height-1; i>=0; i-- )
- {
- for ( j=0; j<info.width; j++)
- {
- an = (img[i][j].b + img[i][j].g + img[i][j].r)/3;
- img[i][j].b = an;
- img[i][j].g = an;
- img[i][j].r = an;
- }
- }
- //Crearea numelui imaginii in var image_bw
- strcpy(image_bw, image_name);
- strcpy(image_bw+strlen(image_bw)-4,"_black_white.bmp");
- //Crearea fisierului cu imagine alb-negru
- int pad_num = info.width % 4, pad0 = 0, var;
- FILE *out1 = fopen(image_bw,"wb");
- fwrite( &file, sizeof(file), 1, out1);
- fwrite( &info, sizeof(info), 1, out1);
- for(i=0;i<84;i++)
- {
- fputc( 0, out1);
- }
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- fwrite( &img[i][j], sizeof(pixel), 1, out1);
- }
- if (pad_num != 0)
- {
- var = pad_num;
- while (var != 0)
- {
- fwrite(&pad0, sizeof(int), 1, out1);
- var--;
- }
- }
- }
- fclose(out1);
- //Task 2
- //Cele 3 matrice-filtru
- int a[3][3] = {-1,-1,-1,-1,8,-1,-1,-1,-1};
- int b[3][3] = {0,1,0,1,-4,1,0,1,0};
- int c[3][3] = {-1,0,1,0,0,0,1,0,-1}; // Este explicat in readme motivul pentru care am schimbat filtrul 3
- //Crearea unei noi matrice
- pixel **F1;
- F1 = calloc(info.height, sizeof(pixel *));
- for ( i=0; i<info.height; i++)
- {
- F1[i] = calloc(info.width, sizeof(pixel));
- }
- filtru(img, F1, info.height, info.width, a);
- //Crearea numelui imaginii
- strcpy(image_f1, image_name);
- strcpy(image_f1+strlen(image_f1)-4,"_f1.bmp");
- //Crearea fisierului "imagine_f1.bmp"
- pad_num = info.width % 4;
- pad0 = 0;
- FILE *out2 = fopen(image_f1,"wb");
- fwrite( &file, sizeof(file), 1, out2);
- fwrite( &info, sizeof(info), 1, out2);
- for(i=0;i<84;i++)
- {
- fputc( 0, out2);
- }
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- fwrite( &F1[i][j], sizeof(pixel), 1, out2);
- }
- if (pad_num != 0)
- {
- var = pad_num;
- while (var != 0)
- {
- fwrite(&pad0, sizeof(int), 1, out2);
- var--;
- }
- }
- }
- fclose(out2);
- //Crearea imaginii image_f2.bmp
- filtru(img, F1, info.height, info.width, b);
- //Crearea numelui imaginii
- strcpy(image_f1, image_name);
- strcpy(image_f1+strlen(image_f1)-4,"_f2.bmp");
- //Crearea fisierului
- pad_num = info.width % 4;
- pad0 = 0;
- out2 = fopen(image_f1,"wb");
- fwrite( &file, sizeof(file), 1, out2);
- fwrite( &info, sizeof(info), 1, out2);
- for(i=0;i<84;i++)
- {
- fputc( 0, out2);
- }
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- fwrite( &F1[i][j], sizeof(pixel), 1, out2);
- }
- if (pad_num != 0)
- {
- var = pad_num;
- while (var != 0)
- {
- fwrite(&pad0, sizeof(int), 1, out2);
- var--;
- }
- }
- }
- fclose(out2);
- //Crearea imaginii image_f3.bmp
- filtru(img, F1, info.height, info.width, c);
- //Crearea numelui imaginii
- strcpy(image_f1, image_name);
- strcpy(image_f1+strlen(image_f1)-4,"_f3.bmp");
- //Crearea fisierului
- pad_num = info.width % 4;
- pad0 = 0;
- out2 = fopen(image_f1,"wb");
- fwrite( &file, sizeof(file), 1, out2);
- fwrite( &info, sizeof(info), 1, out2);
- for(i=0;i<84;i++)
- {
- fputc( 0, out2);
- }
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- fwrite( &F1[i][j], sizeof(pixel), 1, out2);
- }
- if (pad_num != 0)
- {
- var = pad_num;
- while (var != 0)
- {
- fwrite(&pad0, sizeof(int), 1, out2);
- var--;
- }
- }
- }
- fclose(out2);
- for ( i=0; i<info.height; i++ )
- {
- for ( j=0; j<info.width; j++)
- {
- img[i][j].b = aux[i][j].b;
- img[i][j].g = aux[i][j].g;
- img[i][j].r = aux[i][j].r;
- }
- }
- for ( i=0; i<info.height; i++ )
- {
- free(aux[i]);
- free(F1[i]);
- }
- free(aux);
- free(F1);
- //Task 3
- int id = 0;
- int **viz;
- int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
- viz = calloc(info.height, sizeof(int *));
- for ( i=0; i<info.height; i++)
- {
- viz[i] = calloc(info.width, sizeof(int));
- }
- for ( i = info.height-1 ; i >= 0; i--)
- {
- for ( j=0; j<info.width; j++)
- {
- if ( viz[i][j] == 0 )
- {
- id++;
- fill(img, info.height, info.width, i, j, viz, id, prag, img[i][j]);
- }
- }
- }
- //Crearea numelui imaginii
- char comp_name[100];
- strcpy(comp_name, image_name);
- strcpy(comp_name+strlen(comp_name)-4,"_compressed.bin");
- FILE *out3 = fopen("airplane_compressed.bin","wb");
- fwrite( &file, sizeof(file), 1, out3);
- fwrite( &info, sizeof(info), 1, out3);
- for(i=0;i<84;i++)
- {
- fputc( 0, out3);
- }
- for ( i=0; i < info.height; i++)
- {
- for ( j=0; j<info.width; j++)
- {
- int cnt = 0;
- for(k = 1; k < 4; ++k)
- if(0 <= i+dx[k] && i+dx[k] < info.height && 0 <= j+dy[k] && j+dy[k] < info.width && viz[i][j] == viz[ i+dx[k] ][j +dy[k] ])
- cnt++;
- if(cnt < 4 )
- {
- short ii = i+1, jj = j+1;
- fwrite ( &ii, sizeof(short), 1, out3 );
- fwrite ( &jj, sizeof(short), 1, out3 );
- fwrite ( &img[i][j].r, sizeof(char), 1, out3);
- fwrite ( &img[i][j].g, sizeof(char), 1, out3);
- fwrite ( &img[i][j].b, sizeof(char), 1, out3);
- }
- }
- }
- fclose(out3);
- for ( i=0; i<info.height; i++ )
- {
- free(viz[i]);
- free(img[i]);
- }
- free(viz);
- free(img);
- //Task 4
- short col, line;
- short col_prec=0;
- int nr=1;
- //Deschiderea fisierului si alocarea memoriei pt matrice
- arhiva_file = fopen (arhiva, "rb");
- if (arhiva_file == NULL)
- {
- printf("ERROR: Can't open file");
- return -1;
- }
- img = calloc(info.height, sizeof(pixel *));
- for ( i=0; i<info.height; i++)
- {
- img[i] = calloc(info.width, sizeof(pixel));
- }
- fseek(arhiva_file, file.imageDataOffset, SEEK_SET);
- //Citirea elementelor si completarea matricei
- while ( nr != 0)
- {
- fread( &line, sizeof(short), 1, arhiva_file );
- fread( &col, sizeof(short), 1, arhiva_file );
- fread( &img[line-1][col-1].b, sizeof(char), 1, arhiva_file );
- fread( &img[line-1][col-1].g, sizeof(char), 1, arhiva_file );
- fread( &img[line-1][col-1].r, sizeof(char), 1, arhiva_file );
- //Completarea matricei
- if ( (col-1-col_prec) > 1)
- {
- for (k=col_prec+1; k<col; k++)
- {
- img[line-1][k].b = img[line-1][col_prec].b;
- img[line-1][k].g = img[line-1][col_prec].g;
- img[line-1][k].r = img[line-1][col_prec].r;
- }
- }
- if ( (line-1 == (info.height-1)) && (col-1 == (info.width-1)) )
- {
- nr = 0;
- }
- col_prec = col-1;
- }
- fclose(arhiva_file);
- //Crearea numelui imaginii in var image_bw
- strcpy(image_dec, "decompressed.bmp");
- //Crearea fisierului
- FILE *out4 = fopen(image_dec,"wb");
- fwrite(&file,sizeof(file),1,out4);
- fwrite(&info,sizeof(info),1,out4);
- for(i=0;i<84;i++)
- {
- fputc(0,out4);
- }
- for ( i=info.height-1; i>=0; i--)
- {
- for ( j=0; j<info.width; j++)
- {
- fwrite(&img[i][j],sizeof(pixel),1,out4);
- }
- if (pad_num != 0)
- {
- var = pad_num;
- while (var != 0)
- {
- fwrite(&pad0, sizeof(int), 1, out4);
- var--;
- }
- }
- }
- fclose(out4);
- //Eliberare memorie
- for (i=0; i<info.height; i++)
- {
- free(img[i]);
- }
- free(img);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement