PyObject* PyImaging_DisplayModeWin32(PyObject* self, PyObject* args) { char *mode; int size[2]; mode = ImagingGetModeDIB(size); return Py_BuildValue("s(ii)", mode, size[0], size[1]); }
ImagingDIB ImagingNewDIB(const char *mode, int xsize, int ysize) { /* Create a Windows bitmap */ ImagingDIB dib; RGBQUAD *palette; int i; /* Check mode */ if (strcmp(mode, "1") != 0 && strcmp(mode, "L") != 0 && strcmp(mode, "RGB") != 0) return (ImagingDIB) ImagingError_ModeError(); /* Create DIB context and info header */ /* malloc check ok, small constant allocation */ dib = (ImagingDIB) malloc(sizeof(*dib)); if (!dib) return (ImagingDIB) ImagingError_MemoryError(); /* malloc check ok, small constant allocation */ dib->info = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)); if (!dib->info) { free(dib); return (ImagingDIB) ImagingError_MemoryError(); } memset(dib->info, 0, sizeof(BITMAPINFOHEADER)); dib->info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); dib->info->bmiHeader.biWidth = xsize; dib->info->bmiHeader.biHeight = ysize; dib->info->bmiHeader.biPlanes = 1; dib->info->bmiHeader.biBitCount = strlen(mode)*8; dib->info->bmiHeader.biCompression = BI_RGB; /* Create DIB */ dib->dc = CreateCompatibleDC(NULL); if (!dib->dc) { free(dib->info); free(dib); return (ImagingDIB) ImagingError_MemoryError(); } dib->bitmap = CreateDIBSection(dib->dc, dib->info, DIB_RGB_COLORS, &dib->bits, NULL, 0); if (!dib->bitmap) { free(dib->info); free(dib); return (ImagingDIB) ImagingError_MemoryError(); } strcpy(dib->mode, mode); dib->xsize = xsize; dib->ysize = ysize; dib->pixelsize = strlen(mode); dib->linesize = (xsize * dib->pixelsize + 3) & -4; if (dib->pixelsize == 1) dib->pack = dib->unpack = (ImagingShuffler) memcpy; else { dib->pack = ImagingPackBGR; dib->unpack = ImagingPackBGR; } /* Bind the DIB to the device context */ dib->old_bitmap = SelectObject(dib->dc, dib->bitmap); palette = dib->info->bmiColors; /* Bind a palette to it as well (only required for 8-bit DIBs) */ if (dib->pixelsize == 1) { for (i = 0; i < 256; i++) { palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = i; palette[i].rgbReserved = 0; } SetDIBColorTable(dib->dc, 0, 256, palette); } /* Create an associated palette (for 8-bit displays only) */ if (strcmp(ImagingGetModeDIB(NULL), "P") == 0) { char palbuf[sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY)]; LPLOGPALETTE pal = (LPLOGPALETTE) palbuf; int i, r, g, b; /* Load system palette */ pal->palVersion = 0x300; pal->palNumEntries = 256; GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry); if (strcmp(mode, "L") == 0) { /* Greyscale DIB. Fill all 236 slots with a greyscale ramp * (this is usually overkill on Windows since VGA only offers * 6 bits greyscale resolution). Ignore the slots already * allocated by Windows */ i = 10; for (r = 0; r < 236; r++) { pal->palPalEntry[i].peRed = pal->palPalEntry[i].peGreen = pal->palPalEntry[i].peBlue = i; i++; } dib->palette = CreatePalette(pal); } else if (strcmp(mode, "RGB") == 0) { #ifdef CUBE216 /* Colour DIB. Create a 6x6x6 colour cube (216 entries) and * add 20 extra greylevels for best result with greyscale * images. */ i = 10; for (r = 0; r < 256; r += 51) for (g = 0; g < 256; g += 51) for (b = 0; b < 256; b += 51) { pal->palPalEntry[i].peRed = r; pal->palPalEntry[i].peGreen = g; pal->palPalEntry[i].peBlue = b; i++; } for (r = 1; r < 22-1; r++) { /* Black and white are already provided by the cube. */ pal->palPalEntry[i].peRed = pal->palPalEntry[i].peGreen = pal->palPalEntry[i].peBlue = r * 255 / (22-1); i++; } #else /* Colour DIB. Alternate palette. */ i = 10; for (r = 0; r < 256; r += 37) for (g = 0; g < 256; g += 32) for (b = 0; b < 256; b += 64) { pal->palPalEntry[i].peRed = r; pal->palPalEntry[i].peGreen = g; pal->palPalEntry[i].peBlue = b; i++; } #endif dib->palette = CreatePalette(pal); } } return dib; }