home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / tutorials / custEducation / opengl2 / librgb / rgbWriteImageFile.c < prev   
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.7 KB  |  93 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*
  19.  *    rgbWriteImageFile.c
  20.  *    Modified version of Paul Haeberli's writeimg -
  21.  *    abgr ordering switched to rgba for OpenGL and prototypes added
  22.  *                - David Marsland - 1993.
  23.  *
  24.  *    This function takes an array of the form read by glReadPixels and
  25.  *    puts it in a file in the .rgb format.
  26.  *
  27.  *        Write out an RGB image file.  This example uses three functions
  28.  *    from the image library: 
  29.  *
  30.  *        iopen, putrow, and iclose.    
  31.  *
  32.  *     The function iopen is called to describe the xsize and ysize of the 
  33.  *    RGB image to be written out.  
  34.  *
  35.  *    The function putrow writes a row of image data to the image file. It is
  36.  *    called with an array of shorts with values in the range [0..255].
  37.  *    
  38.  *    The function iclose is called to close the image file.
  39.  *
  40.  *    This function puts an array of the form read by lrectread and
  41.  *    puts it in a file in the .rgb format.
  42.  *
  43.  *                Paul Haeberli - 1987
  44.  *
  45.  */
  46.  
  47. #include "rgbImageFile.h"
  48. #include <gl/image.h>
  49.  
  50. unsigned short rbuf[4096];
  51. unsigned short gbuf[4096];
  52. unsigned short bbuf[4096];
  53.  
  54. void
  55. rgbWriteImageFile( char *name, int xsize, int ysize, unsigned int *parray)
  56. {
  57.     int x,y;
  58.     unsigned int cur_col;
  59.     IMAGE *image;
  60.  
  61.     /* This program and librgbImageFile both rely on an unsigned
  62.      * int being 4 unsigned contiguous bytes.  The following test 
  63.      * checks to see if this is the case. 
  64.      */
  65.     if ( sizeof( unsigned int ) != 4  )
  66.     {
  67.         fprintf (stderr, "Warning: sizeof( unsigned int ) != 4,\
  68.             this program and librgbImageFile must be modified\n"
  69.             );
  70.  
  71.         exit (1);
  72.     }
  73.  
  74.     image = iopen(name,"w",RLE(1),3,xsize,ysize,3);
  75.     for(y=0; y<ysize; y++) {
  76.     /*
  77.         fill rbuf, gbuf, and bbuf with pixel values 
  78.     */
  79.     for (x=0;x<xsize;x++)
  80.     {
  81.         cur_col = parray[x];
  82.         bbuf[x] = (cur_col >> 8)&0xff;
  83.         gbuf[x] = (cur_col >> 16)&0xff;
  84.         rbuf[x] = (cur_col >> 24)&0xff;
  85.     }
  86.     putrow(image,rbuf,y,0);        /* red row */
  87.     putrow(image,gbuf,y,1);        /* green row */
  88.     putrow(image,bbuf,y,2);        /* blue row */
  89.     parray += xsize;
  90.     }
  91.     iclose(image);
  92. }
  93.