Pillow/src/libImaging/Dib.c

301 lines
7.9 KiB
C
Raw Normal View History

2010-07-31 06:52:47 +04:00
/*
* The Python Imaging Library
* $Id$
*
* imaging display object for Windows
*
* history:
* 1996-05-12 fl Created
* 1996-05-17 fl Up and running
* 1996-05-21 fl Added palette stuff
* 1996-05-26 fl Added query palette and mode inquery
* 1997-09-21 fl Added draw primitive
* 1998-01-20 fl Use StretchDIBits instead of StretchBlt
* 1998-12-30 fl Plugged a resource leak in DeleteDIB (from Roger Burnham)
*
* Copyright (c) Secret Labs AB 1997-2001.
* Copyright (c) Fredrik Lundh 1996.
*
* See the README file for information on usage and redistribution.
*/
#include "Imaging.h"
#ifdef _WIN32
2010-07-31 06:52:47 +04:00
#include "ImDib.h"
char*
ImagingGetModeDIB(int size_out[2])
{
/* Get device characteristics */
HDC dc;
char* mode;
dc = CreateCompatibleDC(NULL);
mode = "P";
if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)) {
2016-03-16 14:23:45 +03:00
mode = "RGB";
if (GetDeviceCaps(dc, BITSPIXEL) == 1)
mode = "1";
2010-07-31 06:52:47 +04:00
}
if (size_out) {
2016-03-16 14:23:45 +03:00
size_out[0] = GetDeviceCaps(dc, HORZRES);
size_out[1] = GetDeviceCaps(dc, VERTRES);
2010-07-31 06:52:47 +04:00
}
DeleteDC(dc);
return mode;
}
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 &&
2016-03-16 14:23:45 +03:00
strcmp(mode, "RGB") != 0)
return (ImagingDIB) ImagingError_ModeError();
2010-07-31 06:52:47 +04:00
/* Create DIB context and info header */
2016-03-16 14:47:18 +03:00
/* malloc check ok, small constant allocation */
2010-07-31 06:52:47 +04:00
dib = (ImagingDIB) malloc(sizeof(*dib));
if (!dib)
2016-03-16 14:23:45 +03:00
return (ImagingDIB) ImagingError_MemoryError();
2016-03-16 14:47:18 +03:00
/* malloc check ok, small constant allocation */
2010-07-31 06:52:47 +04:00
dib->info = (BITMAPINFO*) malloc(sizeof(BITMAPINFOHEADER) +
256 * sizeof(RGBQUAD));
if (!dib->info) {
free(dib);
2016-03-16 14:23:45 +03:00
return (ImagingDIB) ImagingError_MemoryError();
2010-07-31 06:52:47 +04:00
}
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) {
2016-03-16 14:23:45 +03:00
free(dib->info);
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
2010-07-31 06:52:47 +04:00
}
dib->bitmap = CreateDIBSection(dib->dc, dib->info, DIB_RGB_COLORS,
&dib->bits, NULL, 0);
if (!dib->bitmap) {
free(dib->info);
2016-03-16 14:23:45 +03:00
free(dib);
return (ImagingDIB) ImagingError_MemoryError();
2010-07-31 06:52:47 +04:00
}
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)
2016-03-16 14:23:45 +03:00
dib->pack = dib->unpack = (ImagingShuffler) memcpy;
2010-07-31 06:52:47 +04:00
else {
2016-03-16 14:23:45 +03:00
dib->pack = ImagingPackBGR;
dib->unpack = ImagingPackBGR;
2010-07-31 06:52:47 +04:00
}
/* 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) {
2016-03-16 14:23:45 +03:00
for (i = 0; i < 256; i++) {
palette[i].rgbRed =
palette[i].rgbGreen =
palette[i].rgbBlue = i;
palette[i].rgbReserved = 0;
2010-07-31 06:52:47 +04:00
}
2016-03-16 14:23:45 +03:00
SetDIBColorTable(dib->dc, 0, 256, palette);
2010-07-31 06:52:47 +04:00
}
/* Create an associated palette (for 8-bit displays only) */
if (strcmp(ImagingGetModeDIB(NULL), "P") == 0) {
2016-03-16 14:23:45 +03:00
char palbuf[sizeof(LOGPALETTE)+256*sizeof(PALETTEENTRY)];
LPLOGPALETTE pal = (LPLOGPALETTE) palbuf;
int i, r, g, b;
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* Load system palette */
pal->palVersion = 0x300;
pal->palNumEntries = 256;
GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry);
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
if (strcmp(mode, "L") == 0) {
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* 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 */
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
i = 10;
for (r = 0; r < 236; r++) {
pal->palPalEntry[i].peRed =
pal->palPalEntry[i].peGreen =
pal->palPalEntry[i].peBlue = i;
i++;
}
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
dib->palette = CreatePalette(pal);
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
} else if (strcmp(mode, "RGB") == 0) {
2010-07-31 06:52:47 +04:00
#ifdef CUBE216
2016-03-16 14:23:45 +03:00
/* 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++;
}
2010-07-31 06:52:47 +04:00
#else
2016-03-16 14:23:45 +03:00
/* Colour DIB. Alternate palette. */
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
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++;
}
2010-07-31 06:52:47 +04:00
#endif
2016-03-16 14:23:45 +03:00
dib->palette = CreatePalette(pal);
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
}
2010-07-31 06:52:47 +04:00
}
return dib;
}
void
ImagingPasteDIB(ImagingDIB dib, Imaging im, int xy[4])
{
/* Paste image data into a bitmap */
/* FIXME: check size! */
int y;
for (y = 0; y < im->ysize; y++)
2016-03-16 14:23:45 +03:00
dib->pack(dib->bits + dib->linesize*(dib->ysize-(xy[1]+y)-1) +
xy[0]*dib->pixelsize, im->image[y], im->xsize);
2010-07-31 06:52:47 +04:00
}
void
ImagingExposeDIB(ImagingDIB dib, void *dc)
2010-07-31 06:52:47 +04:00
{
/* Copy bitmap to display */
if (dib->palette != 0)
2016-03-16 14:23:45 +03:00
SelectPalette((HDC) dc, dib->palette, FALSE);
2010-07-31 06:52:47 +04:00
BitBlt((HDC) dc, 0, 0, dib->xsize, dib->ysize, dib->dc, 0, 0, SRCCOPY);
}
void
ImagingDrawDIB(ImagingDIB dib, void *dc, int dst[4], int src[4])
2010-07-31 06:52:47 +04:00
{
/* Copy bitmap to printer/display */
if (GetDeviceCaps((HDC) dc, RASTERCAPS) & RC_STRETCHDIB) {
/* stretchdib (printers) */
StretchDIBits((HDC) dc, dst[0], dst[1], dst[2]-dst[0], dst[3]-dst[1],
src[0], src[1], src[2]-src[0], src[3]-src[1], dib->bits,
dib->info, DIB_RGB_COLORS, SRCCOPY);
} else {
/* stretchblt (displays) */
if (dib->palette != 0)
SelectPalette((HDC) dc, dib->palette, FALSE);
StretchBlt((HDC) dc, dst[0], dst[1], dst[2]-dst[0], dst[3]-dst[1],
dib->dc, src[0], src[1], src[2]-src[0], src[3]-src[1],
SRCCOPY);
}
}
int
ImagingQueryPaletteDIB(ImagingDIB dib, void *dc)
2010-07-31 06:52:47 +04:00
{
/* Install bitmap palette */
int n;
if (dib->palette != 0) {
2016-03-16 14:23:45 +03:00
/* Realize associated palette */
HPALETTE now = SelectPalette((HDC) dc, dib->palette, FALSE);
n = RealizePalette((HDC) dc);
2010-07-31 06:52:47 +04:00
2016-03-16 14:23:45 +03:00
/* Restore palette */
SelectPalette((HDC) dc, now, FALSE);
2010-07-31 06:52:47 +04:00
} else
2016-03-16 14:23:45 +03:00
n = 0;
2010-07-31 06:52:47 +04:00
return n; /* number of colours that was changed */
}
void
ImagingDeleteDIB(ImagingDIB dib)
{
/* Clean up */
if (dib->palette)
2016-03-16 14:23:45 +03:00
DeleteObject(dib->palette);
2010-07-31 06:52:47 +04:00
if (dib->bitmap) {
SelectObject(dib->dc, dib->old_bitmap);
2016-03-16 14:23:45 +03:00
DeleteObject(dib->bitmap);
2010-07-31 06:52:47 +04:00
}
if (dib->dc)
2016-03-16 14:23:45 +03:00
DeleteDC(dib->dc);
2010-07-31 06:52:47 +04:00
free(dib->info);
}
#endif /* _WIN32 */