home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / ImageSave.bproj / paowrite.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-28  |  2.0 KB  |  91 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libc.h>
  4. #include "../common.h"
  5. #include "../strfunc.h"
  6. #include "save.h"
  7.  
  8. static void png_write_p(FILE *fp, const commonInfo *cinf)
  9. {
  10.     int i, n;
  11.     int r, g, b, a;
  12.     int alphaindex = -1;
  13.  
  14.     /* PXO */
  15.     fprintf(fp, "PX\n# %s\n%d %d %d\n",
  16.         key_comm(cinf), cinf->width, cinf->height, cinf->palsteps - 1);
  17.     if (cinf->alpha) {
  18.         alphaindex = cinf->palsteps;
  19.         if (alphaindex >= FIXcount)
  20.             alphaindex = FIXcount - 1;    /* Error */
  21.         fprintf(fp, "1 %d\n", alphaindex + 256);
  22.     }else
  23.         fprintf(fp, "0\n");
  24.     for (n = 0; n < cinf->palsteps; n++) {
  25.         unsigned char *p = cinf->palette[n];
  26.         for (i = 0; i < 3; i++)
  27.             putc(*p++, fp);
  28.     }
  29.     for (n = cinf->width * cinf->height - 1; n >= 0; n--) {
  30.         getPixel(&r, &g, &b, &a);
  31.         putc( (a == AlphaTransp ? alphaindex : mapping(r, g, b)), fp);
  32.     }
  33. }
  34.  
  35. static void png_write_f(FILE *fp, const commonInfo *cinf)
  36. {
  37.     int max = 255, sft = 0, i, n, pn;
  38.     int elm[MAXPLANE];
  39.  
  40.     switch (cinf->bits) {
  41.     case 1: max = 1;  sft = 7;  break;
  42.     case 2: max = 3;  sft = 6;  break;
  43.     case 4: max = 15;  sft = 4;  break;
  44.     case 8: max = 255;  sft = 0;  break;
  45.     }
  46.     pn = cinf->numcolors;
  47.     if (cinf->alpha) pn++;
  48.     /* PAO */
  49.     fprintf(fp, "PA\n# %s\n%d %d %d %d\n",
  50.             key_comm(cinf), cinf->width, cinf->height, max, pn);
  51.     n = cinf->width * cinf->height;
  52.     while (n-- > 0) {
  53.         (void) getPixelA(elm);
  54.         if (sft)
  55.             for (i = 0; i < pn; i++)
  56.                 putc(elm[i] >> sft, fp);
  57.         else
  58.             for (i = 0; i < pn; i++)
  59.                 putc(elm[i], fp);
  60.     }
  61. }
  62.  
  63. int pngwrite(FILE *fp, const commonInfo *cinf, const char *dir, BOOL interl)
  64. {
  65.     int err;
  66.     char ppngPath[MAXFILENAMELEN];
  67.     static char *ppngArg[8] = {
  68.         NULL,        /* 0: Tool Path */
  69.         PAO_PNG,
  70.         NULL,        /* 2: -p (maybe) */
  71.         NULL, NULL };
  72.  
  73.     sprintf(ppngPath, "%s/%s", dir, PAO_PNG);
  74.     ppngArg[0] = ppngPath;
  75.     ppngArg[2] = interl ? "-p" : NULL;    /* Progressive */
  76.  
  77.     if ((fp = openWPipe(fp, ppngArg, &err)) == NULL) {
  78.         (void) fclose(fp);
  79.         return err;
  80.     }
  81.  
  82.     if (cinf->palette && cinf->palsteps > 0)
  83.         png_write_p(fp, cinf);
  84.     else
  85.         png_write_f(fp, cinf);
  86.  
  87.     (void) fclose(fp);
  88.     wait(0);    /* Don't forget */
  89.     return 0;
  90. }
  91.