Shadowrun: Awakened 29 September 2011 - Build 871
tgafile.cpp
Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
00003 
00004     This file is part of Threading Building Blocks.
00005 
00006     Threading Building Blocks is free software; you can redistribute it
00007     and/or modify it under the terms of the GNU General Public License
00008     version 2 as published by the Free Software Foundation.
00009 
00010     Threading Building Blocks is distributed in the hope that it will be
00011     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00012     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Threading Building Blocks; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     As a special exception, you may use this file as part of a free software
00020     library without restriction.  Specifically, if other files instantiate
00021     templates or use macros or inline functions from this file, or you compile
00022     this file and link it with other files to produce an executable, this
00023     file does not by itself cause the resulting executable to be covered by
00024     the GNU General Public License.  This exception does not however
00025     invalidate any other reasons why the executable file might be covered by
00026     the GNU General Public License.
00027 */
00028 
00029 /*
00030     The original source for this example is
00031     Copyright (c) 1994-2008 John E. Stone
00032     All rights reserved.
00033 
00034     Redistribution and use in source and binary forms, with or without
00035     modification, are permitted provided that the following conditions
00036     are met:
00037     1. Redistributions of source code must retain the above copyright
00038        notice, this list of conditions and the following disclaimer.
00039     2. Redistributions in binary form must reproduce the above copyright
00040        notice, this list of conditions and the following disclaimer in the
00041        documentation and/or other materials provided with the distribution.
00042     3. The name of the author may not be used to endorse or promote products
00043        derived from this software without specific prior written permission.
00044 
00045     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00046     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00047     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00048     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00049     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00050     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00051     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00052     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00053     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00054     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00055     SUCH DAMAGE.
00056 */
00057 
00058 /* 
00059  * tgafile.c - This file contains the code to write 24 bit targa files...
00060  *
00061  *  $Id: tgafile.cpp,v 1.2 2007-02-22 17:54:16 dpoulsen Exp $
00062  */
00063 
00064 #include "machine.h"
00065 #include "types.h"
00066 #include "util.h"
00067 #include "ui.h"
00068 #include "imageio.h"
00069 #include "tgafile.h"
00070 
00071 void createtgafile(char *name, unsigned short width, unsigned short height) {
00072   int filesize;
00073   FILE * ofp;
00074 
00075   filesize = 3*width*height + 18 - 10;
00076   
00077   if (name==NULL) 
00078     exit(1);
00079   else {
00080     ofp=fopen(name, "w+b");
00081     if (ofp == NULL) {
00082       char msgtxt[2048];
00083       sprintf(msgtxt, "Cannot create %s for output!", name);
00084       rt_ui_message(MSG_ERR, msgtxt);
00085       rt_ui_message(MSG_ABORT, "Rendering Aborted.");
00086       exit(1);
00087     } 
00088 
00089     fputc(0, ofp); /* IdLength      */
00090     fputc(0, ofp); /* ColorMapType  */
00091     fputc(2, ofp); /* ImageTypeCode */
00092     fputc(0, ofp); /* ColorMapOrigin, low byte */
00093     fputc(0, ofp); /* ColorMapOrigin, high byte */
00094     fputc(0, ofp); /* ColorMapLength, low byte */
00095     fputc(0, ofp); /* ColorMapLength, high byte */
00096     fputc(0, ofp); /* ColorMapEntrySize */
00097     fputc(0, ofp); /* XOrigin, low byte */
00098     fputc(0, ofp); /* XOrigin, high byte */
00099     fputc(0, ofp); /* YOrigin, low byte */
00100     fputc(0, ofp); /* YOrigin, high byte */
00101     fputc((width & 0xff),         ofp); /* Width, low byte */
00102     fputc(((width >> 8) & 0xff),  ofp); /* Width, high byte */
00103     fputc((height & 0xff),        ofp); /* Height, low byte */
00104     fputc(((height >> 8) & 0xff), ofp); /* Height, high byte */
00105     fputc(24, ofp);   /* ImagePixelSize */
00106     fputc(0x20, ofp); /* ImageDescriptorByte 0x20 == flip vertically */
00107 
00108     fseek(ofp, filesize, 0);
00109     fprintf(ofp, "9876543210"); 
00110 
00111     fclose(ofp);
00112   } 
00113 }    
00114 
00115 void * opentgafile(char * filename) {
00116   FILE * ofp;
00117 
00118   ofp=fopen(filename, "r+b");
00119   if (ofp == NULL) {
00120     char msgtxt[2048];
00121     sprintf(msgtxt, "Cannot open %s for output!", filename);
00122     rt_ui_message(MSG_ERR, msgtxt);
00123     rt_ui_message(MSG_ABORT, "Rendering Aborted.");
00124     exit(1);
00125   } 
00126 
00127   return ofp;
00128 } 
00129 
00130 void writetgaregion(void * voidofp, 
00131                     int iwidth, int iheight,
00132                     int startx, int starty, 
00133                     int stopx, int stopy, char * buffer) {
00134   int y, totalx, totaly;
00135   char * bufpos;
00136   int filepos, numbytes;
00137   FILE * ofp = (FILE *) voidofp;
00138  
00139   totalx = stopx - startx + 1;
00140   totaly = stopy - starty + 1;
00141 
00142   for (y=0; y<totaly; y++) {
00143     bufpos=buffer + (totalx*3)*(totaly-y-1);
00144     filepos=18 + iwidth*3*(iheight - starty - totaly + y + 1) + (startx - 1)*3; 
00145 
00146     if (filepos >= 18) {
00147       fseek(ofp, filepos, 0); 
00148       numbytes = fwrite(bufpos, 3, totalx, ofp);
00149 
00150       if (numbytes != totalx) {
00151         char msgtxt[256];
00152     sprintf(msgtxt, "File write problem, %d bytes written.", numbytes);  
00153         rt_ui_message(MSG_ERR, msgtxt);
00154       }
00155     }
00156     else {
00157       rt_ui_message(MSG_ERR, "writetgaregion: file ptr out of range!!!\n");
00158       return;  /* don't try to continue */
00159     }
00160   }
00161 }
00162 
00163 
00164 int readtga(char * name, int * xres, int * yres, unsigned char **imgdata) {
00165   int format, width, height, w1, w2, h1, h2, depth, flags;
00166   int imgsize, bytesread, i, tmp;
00167   FILE * ifp;
00168 
00169   ifp=fopen(name, "r");  
00170   if (ifp==NULL) {
00171     return IMAGEBADFILE; /* couldn't open the file */
00172   }
00173 
00174   /* read the targa header */
00175   getc(ifp); /* ID length */
00176   getc(ifp); /* colormap type */
00177   format = getc(ifp); /* image type */
00178   getc(ifp); /* color map origin */
00179   getc(ifp); /* color map origin */
00180   getc(ifp); /* color map length */
00181   getc(ifp); /* color map length */
00182   getc(ifp); /* color map entry size */
00183   getc(ifp); /* x origin */
00184   getc(ifp); /* x origin */
00185   getc(ifp); /* y origin */
00186   getc(ifp); /* y origin */
00187   w1 = getc(ifp); /* width (low) */
00188   w2 = getc(ifp); /* width (hi) */
00189   h1 = getc(ifp); /* height (low) */
00190   h2 = getc(ifp); /* height (hi) */
00191   depth = getc(ifp); /* image pixel size */
00192   flags = getc(ifp); /* image descriptor byte */
00193 
00194   if ((format != 2) || (depth != 24)) {
00195     fclose(ifp);
00196     return IMAGEUNSUP; /* unsupported targa format */
00197   }
00198     
00199 
00200   width = ((w2 << 8) | w1);
00201   height = ((h2 << 8) | h1);
00202 
00203   imgsize = 3 * width * height;
00204   *imgdata = (unsigned char *)rt_getmem(imgsize);
00205   bytesread = fread(*imgdata, 1, imgsize, ifp);
00206   fclose(ifp);
00207 
00208   /* flip image vertically */
00209   if (flags == 0x20) {
00210     int rowsize = 3 * width;
00211     unsigned char * copytmp;
00212 
00213     copytmp = (unsigned char *)malloc(rowsize);
00214 
00215     for (i=0; i<height / 2; i++) {
00216       memcpy(copytmp, &((*imgdata)[rowsize*i]), rowsize);
00217       memcpy(&(*imgdata)[rowsize*i], &(*imgdata)[rowsize*(height - 1 - i)], rowsize);
00218       memcpy(&(*imgdata)[rowsize*(height - 1 - i)], copytmp, rowsize);
00219     }
00220 
00221     free(copytmp);       
00222   }
00223 
00224 
00225   /* convert from BGR order to RGB order */
00226   for (i=0; i<imgsize; i+=3) {
00227     tmp = (*imgdata)[i]; /* Blue */
00228     (*imgdata)[i] = (*imgdata)[i+2]; /* Red */
00229     (*imgdata)[i+2] = tmp; /* Blue */    
00230   }
00231 
00232   *xres = width;
00233   *yres = height;
00234 
00235   if (bytesread != imgsize) 
00236     return IMAGEREADERR;
00237 
00238   return IMAGENOERR;
00239 }
00240 
00241 
00242 
00243 
00244 
00245 

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net