use Mode struct for windows display code

This commit is contained in:
Yay295 2024-04-22 20:43:49 -05:00
parent e53aa29d20
commit 20986c82aa
3 changed files with 26 additions and 30 deletions

View File

@ -47,7 +47,7 @@ typedef struct {
static PyTypeObject ImagingDisplayType;
static ImagingDisplayObject *
_new(const char *mode, int xsize, int ysize) {
_new(const Mode * const mode, int xsize, int ysize) {
ImagingDisplayObject *display;
if (PyType_Ready(&ImagingDisplayType) < 0) {
@ -240,7 +240,7 @@ static struct PyMethodDef methods[] = {
static PyObject *
_getattr_mode(ImagingDisplayObject *self, void *closure) {
return Py_BuildValue("s", self->dib->mode);
return Py_BuildValue("s", self->dib->mode->name);
}
static PyObject *
@ -288,13 +288,14 @@ static PyTypeObject ImagingDisplayType = {
PyObject *
PyImaging_DisplayWin32(PyObject *self, PyObject *args) {
ImagingDisplayObject *display;
char *mode;
char *mode_name;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) {
if (!PyArg_ParseTuple(args, "s(ii)", &mode_name, &xsize, &ysize)) {
return NULL;
}
const Mode * const mode = findMode(mode_name);
display = _new(mode, xsize, ysize);
if (display == NULL) {
return NULL;
@ -305,12 +306,9 @@ PyImaging_DisplayWin32(PyObject *self, PyObject *args) {
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]);
const Mode * const mode = ImagingGetModeDIB(size);
return Py_BuildValue("s(ii)", mode->name, size[0], size[1]);
}
/* -------------------------------------------------------------------- */

View File

@ -25,20 +25,17 @@
#include "ImDib.h"
char *
const Mode *
ImagingGetModeDIB(int size_out[2]) {
/* Get device characteristics */
HDC dc;
char *mode;
const HDC dc = CreateCompatibleDC(NULL);
dc = CreateCompatibleDC(NULL);
mode = "P";
const Mode *mode = IMAGING_MODE_P;
if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)) {
mode = "RGB";
mode = IMAGING_MODE_RGB;
if (GetDeviceCaps(dc, BITSPIXEL) == 1) {
mode = "1";
mode = IMAGING_MODE_1;
}
}
@ -53,7 +50,7 @@ ImagingGetModeDIB(int size_out[2]) {
}
ImagingDIB
ImagingNewDIB(const char *mode, int xsize, int ysize) {
ImagingNewDIB(const Mode * const mode, int xsize, int ysize) {
/* Create a Windows bitmap */
ImagingDIB dib;
@ -61,10 +58,12 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
int i;
/* Check mode */
if (strcmp(mode, "1") != 0 && strcmp(mode, "L") != 0 && strcmp(mode, "RGB") != 0) {
if (mode != IMAGING_MODE_1 && mode != IMAGING_MODE_L && mode != IMAGING_MODE_RGB) {
return (ImagingDIB)ImagingError_ModeError();
}
const int pixelsize = mode == IMAGING_MODE_RGB ? 3 : 1;
/* Create DIB context and info header */
/* malloc check ok, small constant allocation */
dib = (ImagingDIB)malloc(sizeof(*dib));
@ -83,7 +82,7 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
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.biBitCount = pixelsize * 8;
dib->info->bmiHeader.biCompression = BI_RGB;
/* Create DIB */
@ -103,12 +102,12 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
return (ImagingDIB)ImagingError_MemoryError();
}
strcpy(dib->mode, mode);
dib->mode = mode;
dib->xsize = xsize;
dib->ysize = ysize;
dib->pixelsize = strlen(mode);
dib->linesize = (xsize * dib->pixelsize + 3) & -4;
dib->pixelsize = pixelsize;
dib->linesize = (xsize * pixelsize + 3) & -4;
if (dib->pixelsize == 1) {
dib->pack = dib->unpack = (ImagingShuffler)memcpy;
@ -132,7 +131,7 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
}
/* Create an associated palette (for 8-bit displays only) */
if (strcmp(ImagingGetModeDIB(NULL), "P") == 0) {
if (ImagingGetModeDIB(NULL) == IMAGING_MODE_P) {
char palbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
LPLOGPALETTE pal = (LPLOGPALETTE)palbuf;
int i, r, g, b;
@ -142,7 +141,7 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
pal->palNumEntries = 256;
GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry);
if (strcmp(mode, "L") == 0) {
if (mode == IMAGING_MODE_L) {
/* Grayscale DIB. Fill all 236 slots with a grayscale ramp
* (this is usually overkill on Windows since VGA only offers
* 6 bits grayscale resolution). Ignore the slots already
@ -156,8 +155,7 @@ ImagingNewDIB(const char *mode, int xsize, int ysize) {
}
dib->palette = CreatePalette(pal);
} else if (strcmp(mode, "RGB") == 0) {
} else if (mode == IMAGING_MODE_RGB) {
#ifdef CUBE216
/* Colour DIB. Create a 6x6x6 colour cube (216 entries) and

View File

@ -27,7 +27,7 @@ struct ImagingDIBInstance {
UINT8 *bits;
HPALETTE palette;
/* Used by cut and paste */
char mode[4];
const Mode *mode;
int xsize, ysize;
int pixelsize;
int linesize;
@ -37,11 +37,11 @@ struct ImagingDIBInstance {
typedef struct ImagingDIBInstance *ImagingDIB;
extern char *
extern const Mode *
ImagingGetModeDIB(int size_out[2]);
extern ImagingDIB
ImagingNewDIB(const char *mode, int xsize, int ysize);
ImagingNewDIB(const Mode * const mode, int xsize, int ysize);
extern void
ImagingDeleteDIB(ImagingDIB im);