Shadowrun: Awakened 29 September 2011 - Build 871
Functions
tgafile.cpp File Reference
#include "machine.h"
#include "types.h"
#include "util.h"
#include "ui.h"
#include "imageio.h"
#include "tgafile.h"
Include dependency graph for tgafile.cpp:

Go to the source code of this file.

Functions

void createtgafile (char *name, unsigned short width, unsigned short height)
void * opentgafile (char *filename)
int readtga (char *name, int *xres, int *yres, unsigned char **imgdata)
void writetgaregion (void *voidofp, int iwidth, int iheight, int startx, int starty, int stopx, int stopy, char *buffer)

Function Documentation

void createtgafile ( char *  name,
unsigned short  width,
unsigned short  height 
)

Definition at line 71 of file tgafile.cpp.

References MSG_ABORT, MSG_ERR, and rt_ui_message().

                                                                            {
  int filesize;
  FILE * ofp;

  filesize = 3*width*height + 18 - 10;
  
  if (name==NULL) 
    exit(1);
  else {
    ofp=fopen(name, "w+b");
    if (ofp == NULL) {
      char msgtxt[2048];
      sprintf(msgtxt, "Cannot create %s for output!", name);
      rt_ui_message(MSG_ERR, msgtxt);
      rt_ui_message(MSG_ABORT, "Rendering Aborted.");
      exit(1);
    } 

    fputc(0, ofp); /* IdLength      */
    fputc(0, ofp); /* ColorMapType  */
    fputc(2, ofp); /* ImageTypeCode */
    fputc(0, ofp); /* ColorMapOrigin, low byte */
    fputc(0, ofp); /* ColorMapOrigin, high byte */
    fputc(0, ofp); /* ColorMapLength, low byte */
    fputc(0, ofp); /* ColorMapLength, high byte */
    fputc(0, ofp); /* ColorMapEntrySize */
    fputc(0, ofp); /* XOrigin, low byte */
    fputc(0, ofp); /* XOrigin, high byte */
    fputc(0, ofp); /* YOrigin, low byte */
    fputc(0, ofp); /* YOrigin, high byte */
    fputc((width & 0xff),         ofp); /* Width, low byte */
    fputc(((width >> 8) & 0xff),  ofp); /* Width, high byte */
    fputc((height & 0xff),        ofp); /* Height, low byte */
    fputc(((height >> 8) & 0xff), ofp); /* Height, high byte */
    fputc(24, ofp);   /* ImagePixelSize */
    fputc(0x20, ofp); /* ImageDescriptorByte 0x20 == flip vertically */

    fseek(ofp, filesize, 0);
    fprintf(ofp, "9876543210"); 

    fclose(ofp);
  } 
}    
void* opentgafile ( char *  filename)

Definition at line 115 of file tgafile.cpp.

References MSG_ABORT, MSG_ERR, and rt_ui_message().

                                    {
  FILE * ofp;

  ofp=fopen(filename, "r+b");
  if (ofp == NULL) {
    char msgtxt[2048];
    sprintf(msgtxt, "Cannot open %s for output!", filename);
    rt_ui_message(MSG_ERR, msgtxt);
    rt_ui_message(MSG_ABORT, "Rendering Aborted.");
    exit(1);
  } 

  return ofp;
} 
int readtga ( char *  name,
int *  xres,
int *  yres,
unsigned char **  imgdata 
)

Definition at line 164 of file tgafile.cpp.

References IMAGEBADFILE, IMAGENOERR, IMAGEREADERR, IMAGEUNSUP, and rt_getmem().

Referenced by readimage().

                                                                          {
  int format, width, height, w1, w2, h1, h2, depth, flags;
  int imgsize, bytesread, i, tmp;
  FILE * ifp;

  ifp=fopen(name, "r");  
  if (ifp==NULL) {
    return IMAGEBADFILE; /* couldn't open the file */
  }

  /* read the targa header */
  getc(ifp); /* ID length */
  getc(ifp); /* colormap type */
  format = getc(ifp); /* image type */
  getc(ifp); /* color map origin */
  getc(ifp); /* color map origin */
  getc(ifp); /* color map length */
  getc(ifp); /* color map length */
  getc(ifp); /* color map entry size */
  getc(ifp); /* x origin */
  getc(ifp); /* x origin */
  getc(ifp); /* y origin */
  getc(ifp); /* y origin */
  w1 = getc(ifp); /* width (low) */
  w2 = getc(ifp); /* width (hi) */
  h1 = getc(ifp); /* height (low) */
  h2 = getc(ifp); /* height (hi) */
  depth = getc(ifp); /* image pixel size */
  flags = getc(ifp); /* image descriptor byte */

  if ((format != 2) || (depth != 24)) {
    fclose(ifp);
    return IMAGEUNSUP; /* unsupported targa format */
  }
    

  width = ((w2 << 8) | w1);
  height = ((h2 << 8) | h1);

  imgsize = 3 * width * height;
  *imgdata = (unsigned char *)rt_getmem(imgsize);
  bytesread = fread(*imgdata, 1, imgsize, ifp);
  fclose(ifp);

  /* flip image vertically */
  if (flags == 0x20) {
    int rowsize = 3 * width;
    unsigned char * copytmp;

    copytmp = (unsigned char *)malloc(rowsize);

    for (i=0; i<height / 2; i++) {
      memcpy(copytmp, &((*imgdata)[rowsize*i]), rowsize);
      memcpy(&(*imgdata)[rowsize*i], &(*imgdata)[rowsize*(height - 1 - i)], rowsize);
      memcpy(&(*imgdata)[rowsize*(height - 1 - i)], copytmp, rowsize);
    }

    free(copytmp);       
  }


  /* convert from BGR order to RGB order */
  for (i=0; i<imgsize; i+=3) {
    tmp = (*imgdata)[i]; /* Blue */
    (*imgdata)[i] = (*imgdata)[i+2]; /* Red */
    (*imgdata)[i+2] = tmp; /* Blue */    
  }

  *xres = width;
  *yres = height;

  if (bytesread != imgsize) 
    return IMAGEREADERR;

  return IMAGENOERR;
}
void writetgaregion ( void *  voidofp,
int  iwidth,
int  iheight,
int  startx,
int  starty,
int  stopx,
int  stopy,
char *  buffer 
)

Definition at line 130 of file tgafile.cpp.

References MSG_ERR, rt_ui_message(), and totaly.

Referenced by thread_io().

                                                         {
  int y, totalx, totaly;
  char * bufpos;
  int filepos, numbytes;
  FILE * ofp = (FILE *) voidofp;
 
  totalx = stopx - startx + 1;
  totaly = stopy - starty + 1;

  for (y=0; y<totaly; y++) {
    bufpos=buffer + (totalx*3)*(totaly-y-1);
    filepos=18 + iwidth*3*(iheight - starty - totaly + y + 1) + (startx - 1)*3; 

    if (filepos >= 18) {
      fseek(ofp, filepos, 0); 
      numbytes = fwrite(bufpos, 3, totalx, ofp);

      if (numbytes != totalx) {
        char msgtxt[256];
    sprintf(msgtxt, "File write problem, %d bytes written.", numbytes);  
        rt_ui_message(MSG_ERR, msgtxt);
      }
    }
    else {
      rt_ui_message(MSG_ERR, "writetgaregion: file ptr out of range!!!\n");
      return;  /* don't try to continue */
    }
  }
}

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