Otóż miałem zrobić program, który wczytuje typową bitmapę, robi negatyw, przekształca w skale szarości i tam inne funkcje, trochę zrobiłem, ale niestety program kompletnie nie działa i nie potrafię znaleźć przyczyny(chyba przekroczenie zamalokowanej pamięci, ale ja nie widzę, gdzie. Jakby ktoś pomógł mi rozwiązać problem, by to działało to byłoby super, bo od tego zależy moje zaliczenie pozdrawiam.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct bmpfile_head_struct {
unsigned long long int type;
unsigned long long int filesz;
unsigned long long int creator1;
unsigned long long int creator2;
unsigned long long int bmp_offset;
} *bmpfile_header;
struct bmph_struct {
unsigned long long int header_sz;
long long int width;
long long int height;
unsigned long long int nplanes;
unsigned long long int bitspp;
unsigned long long int compress_type;
unsigned long long int bmp_bytesz;
long long int hres;
long long int vres;
unsigned long long int ncolors;
unsigned long long int nimpcolors;
} *bmph;
typedef struct {
char blue;
char green;
char red;
} pixel;
pixel *p, *c;
void zapis(struct bmph_struct *bmph, struct bmpfile_head_struct *bmpfile_header, pixel *p, pixel *c, char nazwa [500])
{
int i, b;
FILE* fileHandle = fopen(nazwa, "rba+");
fwrite(&bmpfile_header->type,2,1,fileHandle);
fwrite(&bmpfile_header->filesz,4,1,fileHandle);
fwrite(&bmpfile_header->creator1,2,1,fileHandle);
fwrite(&bmpfile_header->creator2,2,1,fileHandle);
fwrite(&bmpfile_header->bmp_offset,4,1,fileHandle);
fwrite(&bmph->header_sz,4,1,fileHandle);
fwrite(&bmph->width,4,1,fileHandle);
fwrite(&bmph->height,4,1,fileHandle);
fwrite(&bmph->nplanes,2,1,fileHandle);
fwrite(&bmph->bitspp,2,1,fileHandle);
fwrite(&bmph->compress_type,4,1,fileHandle);
fwrite(&bmph->bmp_bytesz,4,1,fileHandle);
fwrite(&bmph->hres,4,1,fileHandle);
fwrite(&bmph->vres,4,1,fileHandle);
fwrite(&bmph->ncolors,4,1,fileHandle);
fwrite(&bmph->nimpcolors,4,1,fileHandle);
for(i=0;i<bmph->height;i++)
{
b = bmph->width*3%4;
fwrite(&p[i*bmph->width],3,bmph->width,fileHandle);
fwrite(&c[i*bmph->width],b,1,fileHandle);
}
fclose(fileHandle);
}
int main()
{
char plik[500];
int w, b, i, j, k, l;
char tempb, tempr, tempg;
int rmin, gmin, bmin;
int rmax, gmax, bmax;
int Size = 3;
int margin = ((Size-1)/2);
bmpfile_header=malloc(sizeof(bmpfile_header));
bmph=malloc(sizeof(bmph));
printf("Witam jestem programem do obrobki cyfrowej bitmap\n");
printf("Prosze o podanie nazwy pliku, ktory checsz obrobic\n");
scanf("%499s", plik);
FILE* fileHandle = fopen(plik, "rb");
fflush (stdin);
fread(&bmpfile_header->type,2,1,fileHandle);
fread(&bmpfile_header->filesz,4,1,fileHandle);
fread(&bmpfile_header->creator1,2,1,fileHandle);
fread(&bmpfile_header->creator2,2,1,fileHandle);
fread(&bmpfile_header->bmp_offset,4,1,fileHandle);
fread(&bmph->header_sz,4,1,fileHandle);
fread(&bmph->width,4,1,fileHandle);
fread(&bmph->height,4,1,fileHandle);
fread(&bmph->nplanes,2,1,fileHandle);
fread(&bmph->bitspp,2,1,fileHandle);
fread(&bmph->compress_type,4,1,fileHandle);
fread(&bmph->bmp_bytesz,4,1,fileHandle);
fread(&bmph->hres,4,1,fileHandle);
fread(&bmph->vres,4,1,fileHandle);
fread(&bmph->ncolors,4,1,fileHandle);
fread(&bmph->nimpcolors,4,1,fileHandle);
c=(pixel*)malloc((bmph->width)*(bmph->height)*(sizeof(pixel)));
p=(pixel*)malloc((bmph->width)*(bmph->height)*(sizeof(pixel)));
for(i=0;i<bmph->height;i++)
{
b = bmph->width*3%4;
fread(&p[i*bmph->width],3,bmph->width,fileHandle);
fread(&c[i*bmph->width],b,1,fileHandle);
}
printf("Czerwony wynosi: %d\n", p[0].red);
printf("Niebieski wynosi: %d\n", p[0].blue);
printf("Zielony wynosi: %d\n", p[0].green);
fclose(fileHandle);
printf("Co chcesz wykonac ze swoim plikiem?\n");
printf("1.konwersja na obraz w skali szarosci\n");
printf("2.Generowanie negatywu\n");
printf("3.Odszumianie filtrem medianowym\n");
printf("4.Odszumianie filtrem usredniajcym\n");
printf("5.Odszumianie filtrem minimalnym\n");
printf("6.Odszumianie filtrem maksymalnym\n");
scanf("%d", &w);
switch(w)
{
case 1:
for(i=0; i<bmph->width*bmph->height; i++)
{
tempr=p[i].red;
tempg=p[i].green;
tempb=p[i].blue;
p[i].red = 0.299 * tempr + 0.587 * tempg + 0.114 * tempb;
p[i].green = 0.299 * tempr + 0.587 * tempg + 0.114 * tempb;
p[i].blue = 0.299 * tempr + 0.587 * tempg + 0.114 * tempb;
}
zapis(bmph, bmpfile_header, p, c, "skalaszarosci.bmp");
break;
case 2:
for(i=0; i<bmph->width*bmph->height; i++)
{
p[i].red = 255 - p[i].red;
p[i].green = 255 - p[i].green;
p[i].blue = 255 - p[i].blue;
}
zapis(bmph, bmpfile_header, p, c, "negatyw.bmp");
break;
case 3:
zapis(bmph, bmpfile_header, p, c, "medianowy.bmp");
break;
case 4:
zapis(bmph, bmpfile_header, p, c, "usredniajacy.bmp");
break;
case 5:
for ( i=margin; i<bmph->width; i++)
for ( j=margin; j<bmph->height; j++)
{
rmin = 255;
gmin = 255;
bmin = 255;
for ( k=0; k<Size; k++)
for ( l=0; l<Size; l++)
{
if (rmin > (*(p+(i+k-margin)*bmph->width+j+l-margin)).red)
rmin = (*(p+(i+k-margin)*bmph->width+j+l-margin)).red;
if (gmin > (*(p+(i+k-margin)*bmph->width+j+l-margin)).green)
gmin = (*(p+(i+k-margin)*bmph->width+j+l-margin)).green;
if (bmin > (*(p+(i+k-margin)*bmph->width+j+l-margin)).blue)
bmin = (*(p+(i+k-margin)*bmph->width+j+l-margin)).blue;
}
p[i].red=rmin;
p[i].green=gmin;
p[i].blue=bmin;
}
zapis(bmph, bmpfile_header, p, c, "minimalny.bmp");
break;
case 6:
for ( i=margin; i<bmph->width; i++)
for ( j=margin; j<bmph->height; j++)
{
rmax = 0;
gmax = 0;
bmax = 0;
for ( k=0; k<Size; k++)
for ( l=0; l<Size; l++)
{
if (rmax > (*(p+(i+k-margin)*bmph->width+j+l-margin)).red)
rmax = (*(p+(i+k-margin)*bmph->width+j+l-margin)).red;
if (gmax > (*(p+(i+k-margin)*bmph->width+j+l-margin)).green)
gmax = (*(p+(i+k-margin)*bmph->width+j+l-margin)).green;
if (bmax > (*(p+(i+k-margin)*bmph->width+j+l-margin)).blue)
bmax = (*(p+(i+k-margin)*bmph->width+j+l-margin)).blue;
}
p[i].red=rmax;
p[i].green=gmax;
p[i].blue=bmax;
}
zapis(bmph, bmpfile_header, p, c, "maksymalny.bmp");
default:
printf("Zle dane/n");
}
free(c);
free(p);
free(bmpfile_header);
free(bmph);
return 0;
}