Use Python PPM save, instead of C

This commit is contained in:
Andrew Murray 2026-04-17 12:33:57 +10:00
parent b893310045
commit 816f537c64
4 changed files with 15 additions and 75 deletions

View File

@ -270,8 +270,11 @@ class TestImage:
im = Image.new("RGB", (10, 10))
im._dump(str(tmp_path / "temp_RGB.ppm"))
im = Image.new("RGBA", (10, 10))
im._dump(str(tmp_path / "temp_RGBA.ppm"))
im = Image.new("HSV", (10, 10))
with pytest.raises(ValueError):
with pytest.raises(OSError):
im._dump(str(tmp_path / "temp_HSV.ppm"))
def test_comparison_with_other_type(self) -> None:

View File

@ -731,24 +731,17 @@ class Image:
def _dump(
self, file: str | None = None, format: str | None = None, **options: Any
) -> str:
suffix = ""
if format:
suffix = f".{format}"
suffix = f".{format}" if format else ""
if not file:
f, filename = tempfile.mkstemp(suffix)
os.close(f)
else:
if file:
filename = file
if not filename.endswith(suffix):
filename = filename + suffix
self.load()
if not format or format == "PPM":
self.im.save_ppm(filename)
filename += suffix
else:
self.save(filename, format, **options)
f, filename = tempfile.mkstemp(suffix)
os.close(f)
self.save(filename, format or "PPM", **options)
return filename

View File

@ -108,7 +108,7 @@
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
/* -------------------------------------------------------------------- */
/* OBJECT ADMINISTRATION */
/* OBJECT ADMINISTRATION */
/* -------------------------------------------------------------------- */
typedef struct {
@ -379,7 +379,7 @@ ImagingError_ValueError(const char *message) {
}
/* -------------------------------------------------------------------- */
/* HELPERS */
/* HELPERS */
/* -------------------------------------------------------------------- */
static int
@ -683,7 +683,7 @@ getink(PyObject *color, Imaging im, char *ink) {
}
/* -------------------------------------------------------------------- */
/* FACTORIES */
/* FACTORIES */
/* -------------------------------------------------------------------- */
static PyObject *
@ -3611,7 +3611,7 @@ _effect_spread(ImagingObject *self, PyObject *args) {
}
/* -------------------------------------------------------------------- */
/* UTILITIES */
/* UTILITIES */
/* -------------------------------------------------------------------- */
static PyObject *
@ -3646,25 +3646,6 @@ _getcodecstatus(PyObject *self, PyObject *args) {
return PyUnicode_FromString(msg);
}
/* -------------------------------------------------------------------- */
/* DEBUGGING HELPERS */
/* -------------------------------------------------------------------- */
static PyObject *
_save_ppm(ImagingObject *self, PyObject *args) {
char *filename;
if (!PyArg_ParseTuple(args, "s", &filename)) {
return NULL;
}
if (!ImagingSavePPM(self->image, filename)) {
return NULL;
}
Py_RETURN_NONE;
}
/* -------------------------------------------------------------------- */
/* methods */
@ -3748,9 +3729,6 @@ static struct PyMethodDef methods[] = {
/* Special effects */
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
/* Misc. */
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},
/* arrow */
{"__arrow_c_schema__", (PyCFunction)ExportArrowSchemaPyCapsule, METH_VARARGS},
{"__arrow_c_array__", (PyCFunction)ExportArrowArrayPyCapsule, METH_VARARGS},

View File

@ -41,37 +41,3 @@ ImagingSaveRaw(Imaging im, FILE *fp) {
return 1;
}
int
ImagingSavePPM(Imaging im, const char *outfile) {
FILE *fp;
if (!im) {
(void)ImagingError_ValueError(NULL);
return 0;
}
fp = fopen(outfile, "wb");
if (!fp) {
PyErr_SetString(PyExc_OSError, "error when accessing file");
return 0;
}
if (im->mode == IMAGING_MODE_1 || im->mode == IMAGING_MODE_L) {
/* Write "PGM" */
fprintf(fp, "P5\n%d %d\n255\n", im->xsize, im->ysize);
} else if (im->mode == IMAGING_MODE_RGB) {
/* Write "PPM" */
fprintf(fp, "P6\n%d %d\n255\n", im->xsize, im->ysize);
} else {
fclose(fp);
(void)ImagingError_ModeError();
return 0;
}
ImagingSaveRaw(im, fp);
fclose(fp);
return 1;
}