home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GSFILE.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  4KB  |  139 lines

  1. /* Copyright (C) 1990 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gsfile.c */
  20. /* Bitmap file writing routines for Ghostscript library */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"            /* for gxdevice.h */
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"
  27.  
  28. /* Dump the contents of a memory device in PPM format. */
  29. int
  30. gs_writeppmfile(gx_device_memory *md, FILE *file)
  31. {    int raster = gx_device_raster((gx_device *)md, 0);
  32.     int height = md->height;
  33.     int depth = md->color_info.depth;
  34.     uint rsize = raster * 3;    /* * 3 just for mapped color */
  35.     byte *row = (byte *)gs_malloc(rsize, 1, "ppm file buffer");
  36.     const char *header;
  37.     int y;
  38.     int code = 0;            /* return code */
  39.     if ( row == 0 )            /* can't allocate row buffer */
  40.         return_error(gs_error_VMerror);
  41.  
  42.     /* A PPM file consists of: magic number, comment, */
  43.     /* width, height, maxvalue, (r,g,b).... */
  44.  
  45.     /* Dispatch on the type of the device -- 1-bit mono, */
  46.     /* 8-bit (gray scale or color), 24- or 32-bit true color. */
  47.  
  48.     switch ( depth )
  49.        {
  50.     case 1:
  51.       header = "P4\n# Ghostscript 1 bit mono image dump\n%d %d\n";
  52.       break;
  53.  
  54.     case 8:
  55.       header = (gx_device_has_color(md) ?
  56.         "P6\n# Ghostscript 8 bit mapped color image dump\n%d %d\n255\n" :
  57.         "P5\n# Ghostscript 8 bit gray scale image dump\n%d %d\n255\n");
  58.       break;
  59.  
  60.     case 24:
  61.       header = "P6\n# Ghostscript 24 bit color image dump\n%d %d\n255\n";
  62.       break;
  63.  
  64.     case 32:
  65.       header = "P6\n# Ghostscript 32 bit color image dump\n%d %d\n255\n";
  66.       break;
  67.  
  68.     default:            /* shouldn't happen! */
  69.       code = gs_error_undefinedresult;
  70.       goto done;
  71.        }
  72.  
  73.     /* Write the header. */
  74.     fprintf(file, header, md->width, height);
  75.  
  76.     /* Dump the contents of the image. */
  77.     for ( y = 0; y < height; y++ )
  78.        {    int count;
  79.         register byte *from, *to, *end;
  80.         (*dev_proc(md, get_bits))((gx_device *)md, y, row, NULL);
  81.         switch ( depth )
  82.            {
  83.         case 8:
  84.            {    /* Mapped color, consult the map. */
  85.             if ( gx_device_has_color(md) )
  86.                {    /* Map color */
  87.                 byte *palette = md->palette.data;
  88.                 from = row + raster + raster;
  89.                 memcpy(from, row, raster);
  90.                 to = row;
  91.                 end = from + raster;
  92.                 while ( from < end )
  93.                    {    register byte *cp =
  94.                       palette + (int)*from++ * 3;
  95.                     to[0] = cp[0];    /* red */
  96.                     to[1] = cp[1];    /* green */
  97.                     to[2] = cp[2];    /* blue */
  98.                     to += 3;
  99.                    }
  100.                 count = raster * 3;
  101.                }
  102.             else
  103.                {    /* Map gray scale */
  104.                 register byte *palette = md->palette.data;
  105.                 from = to = row, end = row + raster;
  106.                 while ( from < end )
  107.                     *to++ = palette[(int)*from++ * 3];
  108.                 count = raster;
  109.                }
  110.            }
  111.             break;
  112.         case 32:
  113.            {    /* This case is different, because we must skip */
  114.             /* every fourth byte. */
  115.             from = to = row, end = row + raster;
  116.             while ( from < end )
  117.                {    /* from[0] is unused */
  118.                 to[0] = from[1];    /* red */
  119.                 to[1] = from[2];    /* green */
  120.                 to[2] = from[3];    /* blue */
  121.                 from += 4;
  122.                 to += 3;
  123.                }
  124.             count = to - row;
  125.            }
  126.             break;
  127.         default:
  128.             count = raster;
  129.            }
  130.         if ( fwrite(row, 1, count, file) < count )
  131.            {    code = gs_error_ioerror;
  132.             goto done;
  133.            }
  134.        }
  135.  
  136. done:    gs_free((char *)row, rsize, 1, "ppm file buffer");
  137.     return code;
  138. }
  139.