#pragma comment(lib, "Gdiplus.lib") #include #include #include using namespace Gdiplus; bool GetEncoderClsid(const WCHAR* format, CLSID* pClsid) { UINT num, size; Gdiplus::GetImageEncodersSize(&num, &size); Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size)); Gdiplus::GetImageEncoders(num, size, pImageCodecInfo); bool found = false; for (UINT ix = 0; !found && ix < num; ++ix) { if (0 == _wcsicmp(pImageCodecInfo[ix].MimeType, format) == 0) { *pClsid = pImageCodecInfo[ix].Clsid; found = true; } } free(pImageCodecInfo); return found; } struct Pixel { unsigned int a:8;//8bit unsigned int r:8; unsigned int g:8; unsigned int b:8; }; Pixel Convert(DWORD pxl) { Pixel pixel; BYTE *p; p=(BYTE*)&pxl; pixel.a=p[3]; pixel.r=p[2]; pixel.g=p[1]; pixel.b=p[0]; return pixel; } Pixel RGBtoHSV(Pixel pxl) { Pixel HSV; //H=r, S=g, L/V=b HSV.a=255; int r=pxl.r; int g=pxl.g; int b=pxl.b; int maxi=max(r,max(g,b)); int mini=min(r,min(g,b)); int sum=maxi+mini; int diff=maxi-mini; //sum*240/255; HSV.b=(diff*30841)>>16; if (diff==0) HSV.r=HSV.g=0; else { if (HSV.b<120) HSV.g=diff*240/sum; else HSV.g=diff*240/(510-sum); if (maxi==r) { HSV.r=(g-b)/(40*diff); if (HSV.r<0) HSV.r+=239; } else if (maxi==g) { HSV.r=(b-r)/(40*diff); if (HSV.r<0) HSV.r+=79; else HSV.r+=80; } else { HSV.r=(r-g)/(40*diff); if (HSV.r<0) HSV.r+=159; else HSV.r+=160; } if (HSV.r>240) HSV.r-=240; } return HSV; } inline DWORD GetPxl(void *Scan0,int Stride,int x,int y) { DWORD *pxl; pxl=(DWORD*)(Scan0) + (y * Stride) + (x);//*4 scheint irgendwie automatisch zu laufen!?? return *pxl; } inline void SetPxl(void *Scan0,int Stride,int x,int y,Pixel np) { DWORD *pxl; pxl=(DWORD*)(Scan0) + (y * Stride) + (x); DWORD pixel=(((np.a<<24)&0xff000000)|(np.r<<16)|(np.g<<8)|np.b); *pxl=pixel; } Bitmap* Kernauswertung(int kern[3][3],Bitmap *bmp,int w,int h) { Bitmap *nbmp=new Bitmap(w,h,bmp->GetPixelFormat()); BYTE colors[3][3]; for (int y=1; yGetPixel(x+i,y+i2,&c); colors[i+1][i2+1]=c.GetR(); } } int algo=colors[0][0]*kern[0][0]+colors[1][0]*kern[1][0]+colors[2][0]*kern[2][0] +colors[0][1]*kern[0][1]+colors[1][1]*kern[1][1]+colors[2][1]*kern[2][1] +colors[0][2]*kern[0][2]+colors[1][2]*kern[1][2]+colors[2][2]*kern[2][2]; //nur ein Versuch diese Zeile: if (algo>255) algo=255; Color c(255,abs(algo),abs(algo),abs(algo));//algo leider nicht immer <=255 nbmp->SetPixel(x,y,c); } } delete bmp; return nbmp; } int main() { GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Bitmap *bmp=new Bitmap(L"D:\\Downloads\\bg.bmp"); int w,h; w=bmp->GetWidth(); h=bmp->GetHeight(); /*Rect rect(0,0,w-1,h-1); int flag=ImageLockMode::ImageLockModeUserInputBuf; BitmapData *data=new BitmapData(); DWORD *pxls=new DWORD[w,h]; data->Width=w; data->Height=h; data->PixelFormat=PixelFormat32bppARGB; data->Scan0=pxls; data->Stride=data->Width; bmp.LockBits(&rect,flag,PixelFormat32bppARGB,data);*/ for (int y=0; yGetPixel(x,y,&pxl); //DWORD pixel=GetPxl(data->Scan0,data->Stride,x,y); DWORD pixel=pxl.GetValue(); Pixel rgb=Convert(pixel); Pixel hsv=RGBtoHSV(rgb); Color c(hsv.a,hsv.r,hsv.r,hsv.r); //Color c(hsv.a,hsv.r,hsv.g,hsv.b);//colored bmp->SetPixel(x,y,c); //SetPxl(data->Scan0,data->Stride,x,y,hsv); } } //ab hier gehts mit der Bildverarbeitung los! int kern[3][3]; kern[0][0]=1; kern[1][0]=2; kern[2][0]=1; kern[0][1]=0; kern[1][1]=0; kern[2][1]=0; kern[0][2]=-1; kern[1][2]=-2; kern[2][2]=-1; bmp=Kernauswertung(kern,bmp,w,h); kern[0][0]=1; kern[1][0]=0; kern[2][0]=-1; kern[0][1]=2; kern[1][1]=0; kern[2][1]=-2; kern[0][2]=1; kern[1][2]=0; kern[2][2]=-1; bmp=Kernauswertung(kern,bmp,w,h); //bmp.UnlockBits(data); //delete pxls; CLSID clsid; GetEncoderClsid(L"bmp",&clsid); bmp->Save(L"D:\\Downloads\\zbgnew.bmp",&clsid); delete bmp; //GdiplusShutdown(gdiplusToken); //Hiermit gibt es Fehler ohne exit(0);, warum?? //exit(0); return 0; }