static VOID ImageView_DrawImage(HWND hwnd) { GpGraphics *graphics; UINT uImgWidth, uImgHeight; UINT height = 0, width = 0, x = 0, y = 0; PAINTSTRUCT ps; RECT rect; HDC hdc; hdc = BeginPaint(hwnd, &ps); if (!hdc) { DPRINT1("BeginPaint() failed\n"); return; } GdipCreateFromHDC(hdc, &graphics); if (!graphics) { DPRINT1("GdipCreateFromHDC() failed\n"); return; } GdipGetImageWidth(image, &uImgWidth); GdipGetImageHeight(image, &uImgHeight); if (GetClientRect(hwnd, &rect)) { FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH)); if ((rect.right >= uImgWidth)&&(rect.bottom >= uImgHeight)) { width = uImgWidth; height = uImgHeight; } else { height = uImgHeight * (UINT)rect.right / uImgWidth; if (height <= rect.bottom) { width = rect.right; } else { width = uImgWidth * (UINT)rect.bottom / uImgHeight; height = rect.bottom; } } y = (rect.bottom / 2) - (height / 2); x = (rect.right / 2) - (width / 2); DPRINT("x = %d\ny = %d\nWidth = %d\nHeight = %d\n\nrect.right = %d\nrect.bottom = %d\n\nuImgWidth = %d\nuImgHeight = %d\n", x, y, width, height, rect.right, rect.bottom, uImgWidth, uImgHeight); Rectangle(hdc, x - 1, y - 1, x + width + 1, y + height + 1); GdipDrawImageRect(graphics, image, x, y, width, height); } GdipDeleteGraphics(graphics); EndPaint(hwnd, &ps); }
PDIBITMAP DibLoadImage(LPTSTR lpFilename) { PDIBITMAP lpBitmap; GpBitmap *bitmap; BitmapData lock; if (GdipCreateBitmapFromFile(lpFilename, &bitmap) != Ok) { return NULL; } lpBitmap = HeapAlloc(GetProcessHeap(), 0, sizeof(DIBITMAP)); if (lpBitmap == NULL) { GdipDisposeImage((GpImage*)bitmap); return NULL; } lpBitmap->info = HeapAlloc(GetProcessHeap(), 0, sizeof(BITMAPINFO)); if (lpBitmap->info == NULL) { HeapFree(GetProcessHeap(), 0, lpBitmap); GdipDisposeImage((GpImage*)bitmap); return NULL; } if (GdipGetImageWidth((GpImage*)bitmap, &lpBitmap->width) != Ok || GdipGetImageHeight((GpImage*)bitmap, &lpBitmap->height) != Ok) { HeapFree(GetProcessHeap(), 0, lpBitmap->info); HeapFree(GetProcessHeap(), 0, lpBitmap); GdipDisposeImage((GpImage*)bitmap); return NULL; } lpBitmap->bits = HeapAlloc(GetProcessHeap(), 0, lpBitmap->width * lpBitmap->height * 4); if (!lpBitmap->bits) { HeapFree(GetProcessHeap(), 0, lpBitmap->info); HeapFree(GetProcessHeap(), 0, lpBitmap); GdipDisposeImage((GpImage*)bitmap); return NULL; } ZeroMemory(lpBitmap->info, sizeof(BITMAPINFO)); lpBitmap->info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpBitmap->info->bmiHeader.biWidth = lpBitmap->width; lpBitmap->info->bmiHeader.biHeight = -(INT)lpBitmap->height; lpBitmap->info->bmiHeader.biPlanes = 1; lpBitmap->info->bmiHeader.biBitCount = 32; lpBitmap->info->bmiHeader.biCompression = BI_RGB; lpBitmap->info->bmiHeader.biSizeImage = lpBitmap->width * lpBitmap->height * 4; lock.Width = lpBitmap->width; lock.Height = lpBitmap->height; lock.Stride = lpBitmap->width * 4; lock.PixelFormat = PixelFormat32bppPARGB; lock.Scan0 = lpBitmap->bits; lock.Reserved = 0; if (GdipBitmapLockBits(bitmap, NULL, ImageLockModeRead | ImageLockModeUserInputBuf, PixelFormat32bppPARGB, &lock) != Ok) { HeapFree(GetProcessHeap(), 0, lpBitmap->bits); HeapFree(GetProcessHeap(), 0, lpBitmap->info); HeapFree(GetProcessHeap(), 0, lpBitmap); GdipDisposeImage((GpImage*)bitmap); return NULL; } GdipBitmapUnlockBits(bitmap, &lock); GdipDisposeImage((GpImage*)bitmap); return lpBitmap; }
void win_emoji_show(int x, int y, wchar * efn, int elen, ushort lattr) { GpStatus s; static GdiplusStartupInput gi = (GdiplusStartupInput){1, NULL, FALSE, FALSE}; static ULONG_PTR gis = 0; if (!gis) { s = GdiplusStartup(&gis, &gi, NULL); gpcheck("startup", s); } bool use_stream = true; IStream * fs = 0; if (use_stream) { s = GdipCreateStreamOnFile(efn, 0 /* FileMode.Open */, &fs); gpcheck("stream", s); } else s = NotImplemented; GpImage * img = 0; if (s == Ok) { s = GdipLoadImageFromStream(fs, &img); gpcheck("load stream", s); } else { // This is reported to generate a memory leak, so rather use the stream. s = GdipLoadImageFromFile(efn, &img); gpcheck("load file", s); } int col = PADDING + x * cell_width; int row = PADDING + y * cell_height; if ((lattr & LATTR_MODE) >= LATTR_BOT) row -= cell_height; int w = elen * cell_width; if ((lattr & LATTR_MODE) != LATTR_NORM) w *= 2; int h = cell_height; if ((lattr & LATTR_MODE) >= LATTR_TOP) h *= 2; if (cfg.emoji_placement) { uint iw, ih; s = GdipGetImageWidth(img, &iw); gpcheck("width", s); s = GdipGetImageHeight(img, &ih); gpcheck("height", s); // consider aspect ratio so that ih/iw == h/w; // if EMPL_FULL, always adjust w // if ih/iw > h/w, make w smaller // if iw/ih > w/h, make h smaller if (cfg.emoji_placement == EMPL_FULL && ih * w != h * iw) { w = h * iw / ih; } else if (ih * w > h * iw) { int w0 = w; w = h * iw / ih; if (cfg.emoji_placement == EMPL_MIDDLE) { // horizontally center col += (w0 - w) / 2; } } else if (iw * h > w * ih) { int h0 = h; h = w * ih / iw; // vertically center row += (h0 - h) / 2; } } GpGraphics * gr; s = GdipCreateFromHDC(GetDC(wnd), &gr); gpcheck("hdc", s); s = GdipDrawImageRectI(gr, img, col, row, w, h); gpcheck("draw", s); s = GdipFlush(gr, FlushIntentionFlush); gpcheck("flush", s); s = GdipDeleteGraphics(gr); gpcheck("delete gr", s); s = GdipDisposeImage(img); gpcheck("dispose img", s); if (fs) { // Release stream resources, close file. fs->lpVtbl->Release(fs); } }
/* * SfExtractDropper * * Purpose: * * Extract Sirefef/ZeroAccess from image resource. * * CNG variant * */ UINT SfExtractDropper( LPWSTR lpCommandLine ) { BOOL cond = FALSE, bSuccess = FALSE; ULONG c, uKey = 0, imagesz; WCHAR szInputFile[MAX_PATH + 1]; WCHAR szOutputFile[MAX_PATH + 1]; WCHAR szKey[MAX_PATH]; PVOID ImageBase = NULL, EncryptedData = NULL, DecryptedData = NULL; IStream *pImageStream = NULL; ULONG_PTR gdiplusToken = 0; GdiplusStartupInput input; GdiplusStartupOutput output; PVOID BitmapPtr = NULL; GdiPlusBitmapData BitmapData; GdiPlusRect rect; SIZE_T sz; PULONG ptr, i_ptr; //input file c = 0; RtlSecureZeroMemory(szInputFile, sizeof(szInputFile)); GetCommandLineParam(lpCommandLine, 1, (LPWSTR)&szInputFile, MAX_PATH, &c); if (c == 0) { SfcuiPrintText(g_ConOut, T_SFEXTRACTUSAGE, g_ConsoleOutput, FALSE); return (UINT)-1; } //output file c = 0; RtlSecureZeroMemory(&szOutputFile, sizeof(szOutputFile)); GetCommandLineParam(lpCommandLine, 2, (LPWSTR)&szOutputFile, MAX_PATH, &c); if (c == 0) { _strcpy(szOutputFile, TEXT("extracted.bin")); } //key c = 0; RtlSecureZeroMemory(&szKey, sizeof(szKey)); GetCommandLineParam(lpCommandLine, 3, (LPWSTR)&szKey, MAX_PATH, &c); if ((c == 0) || (c > 10)) { SfcuiPrintText(g_ConOut, T_SFEXTRACTUSAGE, g_ConsoleOutput, FALSE); return (UINT)-1; } c = 0; if (locase_w(szKey[1]) == 'x') { c = 2; } uKey = hextoul(&szKey[c]); do { ImageBase = SfuCreateFileMappingNoExec(szInputFile); if (ImageBase == NULL) break; c = 0; EncryptedData = SfLdrQueryResourceData(1, ImageBase, &c); if ((EncryptedData == NULL) || (c == 0)) break; pImageStream = SHCreateMemStream((BYTE *)EncryptedData, (UINT)c); if (pImageStream == NULL) break; RtlSecureZeroMemory(&input, sizeof(input)); RtlSecureZeroMemory(&output, sizeof(output)); input.GdiplusVersion = 1; if (GdiplusStartup(&gdiplusToken, &input, &output) != GdiplusOk) break; BitmapPtr = NULL; if (GdipCreateBitmapFromStream(pImageStream, &BitmapPtr) != GdiplusOk) break; RtlSecureZeroMemory(&rect, sizeof(rect)); if ( (GdipGetImageWidth(BitmapPtr, (UINT *)&rect.Width) == GdiplusOk) && (GdipGetImageHeight(BitmapPtr, (UINT *)&rect.Height) == GdiplusOk) ) { RtlSecureZeroMemory(&BitmapData, sizeof(BitmapData)); if (GdipBitmapLockBits(BitmapPtr, &rect, ImageLockModeRead, PixelFormat32bppARGB, &BitmapData) == GdiplusOk) { c = (rect.Width * rect.Height); imagesz = sizeof(ULONG) * c; sz = imagesz; DecryptedData = NULL; NtAllocateVirtualMemory(NtCurrentProcess(), &DecryptedData, 0, &sz, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (DecryptedData) { i_ptr = (PULONG)BitmapData.Scan0; ptr = DecryptedData; while (c > 0) { *ptr = *i_ptr ^ uKey; ptr++; i_ptr++; c--; } bSuccess = (SfuWriteBufferToFile(szOutputFile, DecryptedData, imagesz, FALSE, FALSE) == imagesz); sz = 0; NtFreeVirtualMemory(NtCurrentProcess(), &DecryptedData, &sz, MEM_RELEASE); } GdipBitmapUnlockBits(BitmapPtr, &BitmapData); } } } while (cond); if (bSuccess == FALSE) { SfcuiPrintText(g_ConOut, T_SFEXTRACTFAIL, g_ConsoleOutput, FALSE); } else { SfcuiPrintText(g_ConOut, szOutputFile, g_ConsoleOutput, TRUE); SfcuiPrintText(g_ConOut, T_SFEXTRACTED, g_ConsoleOutput, TRUE); } if (BitmapPtr != NULL) { GdipDisposeImage(&BitmapPtr); } if (gdiplusToken != 0) { GdiplusShutdown(gdiplusToken); } if (pImageStream != NULL) { pImageStream->lpVtbl->Release(pImageStream); } if (ImageBase != NULL) { NtUnmapViewOfSection(NtCurrentProcess(), ImageBase); } return 0; }
int ReadTextureFromFile( struct Texture * t, const char * filename ) { int i; HGLOBAL hMem; LPVOID pMemImage; IStream *pStm; struct GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GpBitmap *pImg; PixelFormat PixelFormat; //Read in data FILE * f = fopen( filename, "rb"); if( !f ) { fprintf( stderr, "Error: Could not open %s\n", filename ); return -1; } fseek(f,0,SEEK_END); int l = ftell( f ); unsigned char * buffer = malloc( l ); fseek(f,0,SEEK_SET); fread(buffer, l, 1, f ); fclose( f ); //Prepare GDI+ imaging hMem = GlobalAlloc( GMEM_MOVEABLE, l ); pMemImage = GlobalLock( hMem); memcpy( pMemImage, buffer, l ); GlobalUnlock( hMem ); //XXX: This requries OLE32, do we really want it? CreateStreamOnHGlobal( hMem, TRUE, &pStm ); gdiplusStartupInput.GdiplusVersion = 1; gdiplusStartupInput.DebugEventCallback = NULL; gdiplusStartupInput.SuppressBackgroundThread = FALSE; gdiplusStartupInput.SuppressExternalCodecs = FALSE; GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL ); if( GdipCreateBitmapFromStream( pStm, &pImg ) ) { fprintf( stderr, "Error: cannot decode: %s\n", filename ); return -2; } GdipGetImagePixelFormat( (GpImage *)pImg, &PixelFormat ); UINT width; UINT height; GdipGetImageHeight( (GpImage *)pImg, &height ); GdipGetImageWidth( (GpImage *)pImg, &width ); GpRect r; r.X = 0; r.Y = 0; r.Width = width; r.Height = height; BitmapData bd; enum TextureType format; GLenum type; //Almost always GL_TEXTURE_2D (Could be GL_TEXTURE_3D) GLuint texture; int slot; //which texture slot (For multi-texturing) uint8_t * rawdata; //May be other types, too! t->width = width; t->height = height; t->type = GL_TEXTURE_2D; //Detect if has alpha or not... int ps; if( PixelFormat & PixelFormatAlpha ) { GdipBitmapLockBits(pImg,&r,ImageLockModeRead,PixelFormat32bppARGB,&bd); ps = 4; t->format = TTRGBA; } else { GdipBitmapLockBits(pImg,&r,ImageLockModeRead,PixelFormat24bppRGB,&bd); ps = 3; t->format = TTRGB; } t->rawdata = malloc( ps * width * height ); int x, y; if( ps == 3 ) { for( y = 0; y < height; y++ ) for( x = 0; x < width; x++ ) { t->rawdata[(x+y*width)*3+0] = ((unsigned char*)bd.Scan0)[x*3+y*bd.Stride+2]; t->rawdata[(x+y*width)*3+1] = ((unsigned char*)bd.Scan0)[x*3+y*bd.Stride+1]; t->rawdata[(x+y*width)*3+2] = ((unsigned char*)bd.Scan0)[x*3+y*bd.Stride+0]; } } else //ps = 4 { for( y = 0; y < height; y++ ) for( x = 0; x < width; x++ ) { t->rawdata[(x+y*width)*4+0] = ((unsigned char*)bd.Scan0)[x*4+y*bd.Stride+2]; t->rawdata[(x+y*width)*4+1] = ((unsigned char*)bd.Scan0)[x*4+y*bd.Stride+1]; t->rawdata[(x+y*width)*4+2] = ((unsigned char*)bd.Scan0)[x*4+y*bd.Stride+0]; t->rawdata[(x+y*width)*4+3] = ((unsigned char*)bd.Scan0)[x*4+y*bd.Stride+3]; } } GdipBitmapUnlockBits(pImg, &bd ); GdipDisposeImage( (GpImage *)pImg ); GdiplusShutdown( gdiplusToken ); return 0; }