2010-07-31 06:52:47 +04:00
|
|
|
/*
|
|
|
|
* The Python Imaging Library
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* built-in image file handling
|
|
|
|
*
|
|
|
|
* history:
|
|
|
|
* 1995-11-26 fl Created, supports PGM/PPM
|
|
|
|
* 1996-08-07 fl Write "1" images as PGM
|
|
|
|
* 1999-02-21 fl Don't write non-standard modes
|
|
|
|
*
|
|
|
|
* Copyright (c) 1997-99 by Secret Labs AB.
|
|
|
|
* Copyright (c) 1995-96 by Fredrik Lundh.
|
|
|
|
*
|
|
|
|
* See the README file for information on usage and redistribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Imaging.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
int
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingSaveRaw(Imaging im, FILE *fp) {
|
2010-07-31 06:52:47 +04:00
|
|
|
int x, y, i;
|
|
|
|
|
|
|
|
if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
|
|
|
|
/* @PIL227: FIXME: for mode "1", map != 0 to 255 */
|
|
|
|
|
2016-10-03 12:54:23 +03:00
|
|
|
/* PGM "L" */
|
2020-05-10 12:56:36 +03:00
|
|
|
for (y = 0; y < im->ysize; y++) {
|
2016-10-03 12:54:23 +03:00
|
|
|
fwrite(im->image[y], 1, im->xsize, fp);
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
} else {
|
2016-10-03 12:54:23 +03:00
|
|
|
/* PPM "RGB" or other internal format */
|
2020-05-10 12:56:36 +03:00
|
|
|
for (y = 0; y < im->ysize; y++) {
|
|
|
|
for (x = i = 0; x < im->xsize; x++, i += im->pixelsize) {
|
2021-01-03 06:17:51 +03:00
|
|
|
fwrite(im->image[y] + i, 1, im->bands, fp);
|
2020-05-10 12:56:36 +03:00
|
|
|
}
|
|
|
|
}
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-01-03 06:17:51 +03:00
|
|
|
ImagingSavePPM(Imaging im, const char *outfile) {
|
|
|
|
FILE *fp;
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if (!im) {
|
2021-01-03 06:17:51 +03:00
|
|
|
(void)ImagingError_ValueError(NULL);
|
2016-10-03 12:54:23 +03:00
|
|
|
return 0;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fp = fopen(outfile, "wb");
|
|
|
|
if (!fp) {
|
2021-01-03 06:17:51 +03:00
|
|
|
(void)ImagingError_OSError();
|
2016-10-03 12:54:23 +03:00
|
|
|
return 0;
|
2010-07-31 06:52:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
|
2016-10-03 12:54:23 +03:00
|
|
|
/* Write "PGM" */
|
|
|
|
fprintf(fp, "P5\n%d %d\n255\n", im->xsize, im->ysize);
|
2010-07-31 06:52:47 +04:00
|
|
|
} else if (strcmp(im->mode, "RGB") == 0) {
|
|
|
|
/* Write "PPM" */
|
|
|
|
fprintf(fp, "P6\n%d %d\n255\n", im->xsize, im->ysize);
|
|
|
|
} else {
|
2018-03-31 01:42:56 +03:00
|
|
|
fclose(fp);
|
2021-01-03 06:17:51 +03:00
|
|
|
(void)ImagingError_ModeError();
|
2010-07-31 06:52:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImagingSaveRaw(im, fp);
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|