File.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * built-in image file handling
  6. *
  7. * history:
  8. * 1995-11-26 fl Created, supports PGM/PPM
  9. * 1996-08-07 fl Write "1" images as PGM
  10. * 1999-02-21 fl Don't write non-standard modes
  11. *
  12. * Copyright (c) 1997-99 by Secret Labs AB.
  13. * Copyright (c) 1995-96 by Fredrik Lundh.
  14. *
  15. * See the README file for information on usage and redistribution.
  16. */
  17. #include "Imaging.h"
  18. #include <ctype.h>
  19. int
  20. ImagingSaveRaw(Imaging im, FILE* fp)
  21. {
  22. int x, y, i;
  23. if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
  24. /* @PIL227: FIXME: for mode "1", map != 0 to 255 */
  25. /* PGM "L" */
  26. for (y = 0; y < im->ysize; y++)
  27. fwrite(im->image[y], 1, im->xsize, fp);
  28. } else {
  29. /* PPM "RGB" or other internal format */
  30. for (y = 0; y < im->ysize; y++)
  31. for (x = i = 0; x < im->xsize; x++, i += im->pixelsize)
  32. fwrite(im->image[y]+i, 1, im->bands, fp);
  33. }
  34. return 1;
  35. }
  36. int
  37. ImagingSavePPM(Imaging im, const char* outfile)
  38. {
  39. FILE* fp;
  40. if (!im) {
  41. (void) ImagingError_ValueError(NULL);
  42. return 0;
  43. }
  44. fp = fopen(outfile, "wb");
  45. if (!fp) {
  46. (void) ImagingError_IOError();
  47. return 0;
  48. }
  49. if (strcmp(im->mode, "1") == 0 || strcmp(im->mode, "L") == 0) {
  50. /* Write "PGM" */
  51. fprintf(fp, "P5\n%d %d\n255\n", im->xsize, im->ysize);
  52. } else if (strcmp(im->mode, "RGB") == 0) {
  53. /* Write "PPM" */
  54. fprintf(fp, "P6\n%d %d\n255\n", im->xsize, im->ysize);
  55. } else {
  56. fclose(fp);
  57. (void) ImagingError_ModeError();
  58. return 0;
  59. }
  60. ImagingSaveRaw(im, fp);
  61. fclose(fp);
  62. return 1;
  63. }