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

View File

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

View File

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