// Compile it with: // g++ a.cpp -lopencv_core249 -lopencv_highgui249 -lopencv_imgproc249 #include #include #include #include #include IplImage* crop(IplImage* src, CvRect roi){ IplImage* cropped = cvCreateImage(cvSize(roi.width,roi.height), src->depth, src->nChannels); cvSetImageROI(src, roi); cvCopy(src, cropped); cvResetImageROI(src); return cropped; } inline unsigned char saturate(int p){ return p > 255 ? 255 : p < 0 ? 0 : p; } unsigned char settings[3]; int main(void){ unsigned char *p = NULL; MEMORY_BASIC_INFORMATION info; int pid; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); PROCESSENTRY32 pp; ZeroMemory(&pp, sizeof(pp)); pp.dwSize = sizeof(pp); if (Process32First(snapshot, &pp)){ do { if (strcmp(pp.szExeFile, "bgb.exe") == 0){ pid = pp.th32ProcessID; break; } } while (Process32Next(snapshot, &pp)); } CloseHandle(snapshot); printf("PID=%i\n",pid); HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); unsigned char *vram = NULL; for (p = NULL; VirtualQueryEx(process, p, &info, sizeof(info)) == sizeof(info); p+=info.RegionSize){ if (info.State == MEM_COMMIT && (info.Type == MEM_MAPPED || info.Type == MEM_PRIVATE)){ DWORD bytes_read; unsigned char *buf = (unsigned char*)malloc(info.RegionSize); ReadProcessMemory(process, p, buf, info.RegionSize, &bytes_read); if (bytes_read != 0){ for (int i=0; iwidth)/(frame->height), 14*8), frame->depth, frame->nChannels); CvRect copy = cvRect((prc->width/2) - ((16*8)/2), 0, 16*8, 14*8); cvResize(frame,prc); rdy = crop(prc, copy); im_gray = cvCreateImage(cvGetSize(rdy),IPL_DEPTH_8U,1); cvCvtColor(rdy, im_gray, CV_RGB2GRAY); unsigned char *data = reinterpret_cast(im_gray->imageData); #define IM_XY(x,y) (((im_gray->widthStep)*(y))+((im_gray->nChannels)*(x))) for (int i=0; iheight-1; i++){ for (int j=0; jwidth-1; j++){ #define PIX (data[IM_XY(j,i)]) PIX = saturate(PIX + 0x54 - settings[0]); int factor = 8-settings[1]; if (factor<0){ if (PIX>0x7f){ PIX = saturate(PIX + factor*(-16)); }else{ PIX = saturate(PIX - factor*(-16)); } }else{ if (factor != 0){ int dist = -(PIX-0x80); PIX += (int)(dist*(factor/8.0f)); } } } } int e; for (int i=0; iheight-1; i++){ for (int j=0; jwidth-1; j++){ if (settings[2] == 0){ data[IM_XY(j,i)] = ((data[IM_XY(j,i)] / 0x40)+1)*0x39; continue; } unsigned char oldpix = data[IM_XY(j,i)]; short int under = (oldpix/64)*64; short int over = ((oldpix/64)+1)*64; short int newpix = saturate((abs(over-oldpix) > abs(under-oldpix)) ? under : over); if (newpix>255) printf("%i\n",newpix); if (newpix<0) printf("%i\n",newpix); data[IM_XY(j,i)] = newpix; int error = oldpix-newpix; data[IM_XY(j+1,i)] = saturate(data[IM_XY(j+1,i)] + (int)((7/16.0f)*error)); data[IM_XY(j-1,i+1)] = saturate(data[IM_XY(j-1,i+1)] + (int)((3/16.0f)*error)); data[IM_XY(j,i+1)] = saturate(data[IM_XY(j,i+1)] + (int)((5/16.0f)*error)); data[IM_XY(j+1,i+1)] = saturate(data[IM_XY(j+1,i+1)] + (int)((1/16.0f)*error)); } } unsigned char *gb_data = (unsigned char*)calloc(0xe00,sizeof(unsigned char)); int ct = 0; for (int rows = 0; rows<14; rows++){ for (int y=0; y<8*16; y++){ int hi, lo; for (int x=0; x<8; x++){ // Magic. Do not touch gb_data[ct] |= (((data[IM_XY(x+(y/8)*8,(y%8)+rows*8)])/64) & 1) << (7-x); gb_data[ct+1] |= ((((data[IM_XY(x+(y/8)*8,(y%8)+rows*8)])/64) >> 1) & 1) << (7-x); } gb_data[ct] ^= 0xff; gb_data[ct+1] ^= 0xff; ct+=2; } } SIZE_T written; if (!WriteProcessMemory(process, vram, gb_data, 0xe00, &written)){ printf("Could not write to the target process (error %i)\n", GetLastError()); break; } if (written == 0){ puts("No data was written to the target process."); break; } free(gb_data); cvReleaseImage(&im_gray); cvReleaseImage(&rdy); cvReleaseImage(&prc); cvWaitKey(10); if (GetAsyncKeyState(27) != 0) break; } cvReleaseCapture(&capture); cvDestroyWindow("Camera_Output"); return 0; }