049
03.09.2003, 17:11 Uhr
Pablo
Supertux (Operator)
|
C++: |
#include <iostream> #include <stdio.h> using namespace std;
//int _filter[] ={1,0,1,0,-4,0,1,0,1}; //int _filter[] ={1,1,1,1,1,1,1,1,1}; //glättungsfilter //int _filter[] ={-1,-1,-1,0,0,0,1,1,1}; //gradientenfilterx //int _filter[] ={-1,0,1,-1,0,1,-1,0,1}; //gradientenfiltery //int _filter[] ={1,2,1,0,0,0,-1,-2,-1}; //sobelfilterx //int _filter[] ={-1,0,1,-2,0,-2,-1,0,-1}; //sobelfiltery int _filter[] ={1,2,1,2,4,2,1,2,1};//binominalfilter #define c 120 //int _filter[] ={-1,2,-1,-2,12+c,-2,-1,2,-1}; //laplace-binominalfilter
void filter(int* &picture,int xdim, int ydim);
int* bmp2raw(char* bmp_filename, int& xdim, int& ydim){ FILE *fp=fopen(bmp_filename,"rb"); int *raw_data; int i,j; fseek(fp,10,SEEK_CUR); j=fgetc(fp); j+=fgetc(fp)*256; j+=fgetc(fp)*256*256; j+=fgetc(fp)*256*256*256; fseek(fp,4,SEEK_CUR); xdim=fgetc(fp); xdim+=fgetc(fp)*256; xdim+=fgetc(fp)*256*256; xdim+=fgetc(fp)*256*256*256; ydim=fgetc(fp); ydim+=fgetc(fp)*256; ydim+=fgetc(fp)*256*256; ydim+=fgetc(fp)*256*256*256; raw_data= new int[xdim*ydim]; fseek(fp,j-26,SEEK_CUR); for(j=ydim-1;j>=0;j--){ for(i=0;i<xdim;i++){ raw_data[i+j*xdim]=fgetc(fp); } } fclose(fp); return raw_data; }
void raw2bmp(int* raw_data,char* bmp_filename, int xdim, int ydim){ FILE *fp; int i,j; fp=fopen(bmp_filename,"wb"); i=xdim*ydim + 4*256 + 54; //Dateilaenge ermitteln //Datei-Header schreiben fputc(66,fp);fputc(77,fp); //Magic Number j=i%256; fputc(j,fp); //Dateilaenge j=(i%(256*256))/256; fputc(j,fp); j=(i%(256*256*256))/(256*256); fputc(j,fp); j=i/(256*256*256); fputc(j,fp); fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp);//4 reservierte Byts fputc(54,fp);fputc(4,fp);fputc(0,fp);fputc(0,fp);//Zeiger auf Datenanfang
//Format-Header schreiben fputc(40,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp);//Format-Header-Laenge j=xdim%256; fputc(j,fp); //Bildbreite j=(xdim%(256*256))/256; fputc(j,fp); j=(xdim%(256*256*256))/(256*256); fputc(j,fp); j=xdim/(256*256*256); fputc(j,fp); j=ydim%256; fputc(j,fp); //Bildhoehe j=(ydim%(256*256))/256; fputc(j,fp); j=(ydim%(256*256*256))/(256*256); fputc(j,fp); j=ydim/(256*256*256); fputc(j,fp); fputc(1,fp);fputc(0,fp); //Anzahl der Ebenen fputc(8,fp);fputc(0,fp); //Bits pro Pixel fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp); //Kompression i=xdim*ydim; j=i%256; fputc(j,fp); //Groesse der Pixeldaten j=(i%(256*256))/256; fputc(j,fp); j=(i%(256*256*256))/(256*256); fputc(j,fp); j=i/(256*256*256); fputc(j,fp); fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp); //x-Auflösung(in Pixel pro Meter) fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp); //y-Auflösung(in Pixel pro Meter) fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp); //Anzahl genutzter Farben fputc(0,fp);fputc(0,fp);fputc(0,fp);fputc(0,fp); //Anzahl wichtiger Farben for(i=0;i<256;i++){fputc(i,fp);fputc(i,fp);fputc(i,fp);fputc(0,fp);} for(j=ydim-1;j>=0;j--){ for(i=0;i<xdim;i++){ fputc(raw_data[i+j*xdim],fp); } } fclose(fp); }
int Pixel(int* picture,int x, int y,int xdim, int ydim) { if (x<0 || y <0 || x > xdim - 1 || y > ydim -1) return 0; return picture[x+y*xdim]; }
void filter(int* &picture,int xdim, int ydim){ int newpixel; int* newpic; newpic = new int[xdim*ydim]; for(int y=0; y<ydim-1; ++y) { for (int x=0; x<xdim-1; ++x) { newpixel = _filter[0]*Pixel(picture, x-1,y-1,xdim,ydim) + _filter[1]*Pixel(picture, x,y-1,xdim,ydim) +_filter[2]*Pixel(picture, x+1,y-1,xdim,ydim) + _filter[3]*Pixel(picture, x-1,y,xdim,ydim) + _filter[4]*Pixel(picture, x,y,xdim,ydim) +_filter[5]*Pixel(picture, x+1,y,xdim,ydim) + _filter[6]*Pixel(picture, x-1,y+1,xdim,ydim) + _filter[7]*Pixel(picture, x,y-1,xdim,ydim) +_filter[8]*Pixel(picture, x+1,y+1,xdim,ydim); newpic[x+y*xdim] = newpixel; } } picture = newpic; //delete [] newpic; }
int main(int argc, char** argv) { int* pic; if (argc < 3) { cout << "usage: " << argv[0] << " [input file] [output file]\n"; return 0; } int xdim, ydim, *picture; picture = bmp2raw(argv[1], xdim, ydim); filter(picture, xdim,ydim); raw2bmp(picture, argv[2], xdim,ydim); }
|
-- A! Elbereth Gilthoniel! silivren penna míriel o menel aglar elenath, Gilthoniel, A! Elbereth! |