Shadowrun: Awakened 29 September 2011 - Build 871
imap.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  * imap.c - This file contains code for doing image map type things.  
00060  *
00061  *  $Id: imap.cpp,v 1.2 2007-02-22 17:54:15 dpoulsen Exp $
00062  */
00063 
00064 #include "machine.h"
00065 #include "types.h"
00066 #include "imap.h"
00067 #include "util.h"
00068 #include "imageio.h"
00069 
00070 rawimage * imagelist[MAXIMGS];
00071 int numimages;
00072 
00073 void ResetImages(void) {
00074   int i;
00075   numimages=0;
00076   for (i=0; i<MAXIMGS; i++) {
00077     imagelist[i]=NULL;
00078   }
00079 }
00080 
00081 void LoadImage(rawimage * image) {
00082   if (!image->loaded) {
00083     readimage(image);
00084     image->loaded=1;
00085   }
00086 }
00087 
00088 color ImageMap(rawimage * image, flt u, flt v) {
00089   color col, colx, colx2;
00090   flt x,y, px, py;
00091   int x1, x2, y1, y2;
00092   unsigned char * ptr;
00093   unsigned char * ptr2;
00094 
00095   if (!image->loaded) {   
00096     LoadImage(image);
00097     image->loaded=1;
00098   }
00099 
00100   if ((u <= 1.0) && (u >=0.0) && (v <= 1.0) && (v >= 0.0)) {
00101     x=(image->xres - 1.0) * u; /* floating point X location */
00102     y=(image->yres - 1.0) * v; /* floating point Y location */
00103 
00104     px = x - ((int) x);
00105     py = y - ((int) y);
00106 
00107     x1 = (int) x;
00108     x2 = x1 + 1;
00109 
00110     y1 = (int) y;
00111     y2 = y1 + 1;
00112 
00113     ptr  = image->data + ((image->xres * y1) + x1) * 3; 
00114     ptr2 = image->data + ((image->xres * y1) + x2) * 3; 
00115 
00116     colx.r = (flt) ((flt)ptr[0] + px*((flt)ptr2[0] - (flt) ptr[0])) / 255.0; 
00117     colx.g = (flt) ((flt)ptr[1] + px*((flt)ptr2[1] - (flt) ptr[1])) / 255.0; 
00118     colx.b = (flt) ((flt)ptr[2] + px*((flt)ptr2[2] - (flt) ptr[2])) / 255.0; 
00119 
00120     ptr  = image->data + ((image->xres * y2) + x1) * 3; 
00121     ptr2 = image->data + ((image->xres * y2) + x2) * 3; 
00122 
00123     colx2.r = ((flt)ptr[0] + px*((flt)ptr2[0] - (flt)ptr[0])) / 255.0; 
00124     colx2.g = ((flt)ptr[1] + px*((flt)ptr2[1] - (flt)ptr[1])) / 255.0; 
00125     colx2.b = ((flt)ptr[2] + px*((flt)ptr2[2] - (flt)ptr[2])) / 255.0; 
00126 
00127     col.r = colx.r + py*(colx2.r - colx.r);
00128     col.g = colx.g + py*(colx2.g - colx.g);
00129     col.b = colx.b + py*(colx2.b - colx.b);
00130 
00131   }
00132   else {
00133     col.r=0.0;
00134     col.g=0.0;
00135     col.b=0.0;
00136   }
00137   return col;
00138 } 
00139 
00140 rawimage * AllocateImage(char * filename) { 
00141   rawimage * newimage = NULL;
00142   int i, len, intable;
00143 
00144   intable=0;
00145   if (numimages!=0) {
00146     for (i=0; i<numimages; i++) {
00147       if (!strcmp(filename, imagelist[i]->name)) {
00148         newimage=imagelist[i];
00149         intable=1;
00150       }
00151     }
00152   }
00153 
00154   if (!intable) {
00155     newimage=(rawimage *)rt_getmem(sizeof(rawimage));
00156     newimage->loaded=0;
00157     newimage->xres=0;
00158     newimage->yres=0;
00159     newimage->bpp=0;
00160     newimage->data=NULL;
00161     len=strlen(filename);
00162     if (len > 80) rtbomb("Filename too long in image map!!"); 
00163     strcpy(newimage->name, filename);
00164 
00165     imagelist[numimages]=newimage;  /* add new one to the table       */ 
00166     numimages++;                    /* increment the number of images */
00167   }
00168  
00169   return newimage;
00170 }
00171 
00172 void DeallocateImage(rawimage * image) {
00173   image->loaded=0;
00174   rt_freemem(image->data);
00175 }
00176 
00177 

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