TarzaNN
TarzaNN neural network simulator
|
00001 /**************************************************************************** 00002 ** 00003 ** Copyright C 2002-2012 Laboratory for Active and Attentive Vision (LAAV), Department of Computer Science and Engineering, York University, Toronto, ON, Canada. 00004 ** All rights reserved. 00005 ** 00006 ** This file is part of the TarzaNN Neural Network Simulator. 00007 ** 00008 ** This file may be distributed and/or modified under the terms of the 00009 ** GNU General Public License version 2 as published by the Free Software 00010 ** Foundation and appearing in the file LICENSE.GPL included in the 00011 ** packaging of this file. 00012 ** 00013 ** See http://www.tarzann.org/gpl/ for GPL licensing information. 00014 ** 00015 ** Contact info@tarzann.org if any conditions of this licensing are 00016 ** not clear to you. 00017 ** 00018 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00019 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00020 ** 00021 ****************************************************************************/ 00022 00023 #ifndef ARRAY2D_H_INCLUDED 00024 #define ARRAY2D_H_INCLUDED 00025 #include <stdio.h> 00026 #include <assert.h> 00027 #include <string.h> 00028 #include <stdint.h> 00029 00030 #include <QDebug> 00031 00032 //#include <execinfo.h> 00033 00034 #include "common.h" 00035 00036 00038 enum 00039 { 00040 kvImageNoFlags = 0, 00041 kvImageLeaveAlphaUnchanged = 1, /* Operate on red, green and blue channels only. Alpha is copied from source to destination. For Interleaved formats only. */ 00042 kvImageCopyInPlace = 2, /* Copy edge pixels */ 00043 kvImageBackgroundColorFill = 4, /* Use the background color for missing pixels */ 00044 kvImageEdgeExtend = 8, /* Extend border data elements */ 00045 kvImageDoNotTile = 16, /* Pass to turn off internal tiling. Use this if you want to do your own tiling, or to use the Min/Max filters in place. */ 00046 kvImageHighQualityResampling = 32 /* Use a higher quality, slower resampling filter for Geometry operations (shear, scale, rotate, affine transform, etc.) */ 00047 }; 00048 00050 template <typename T> 00051 class Array2D{ 00052 protected: 00053 00054 00055 /* Pointer to the top left pixel of the buffer. */ 00056 void* data; 00057 /* The height (in pixels) of the buffer */ 00058 uint32_t height; 00060 uint32_t width; 00061 /* The number of bytes in a pixel row */ 00062 uint32_t rowBytes; 00063 00065 T** rows; 00066 00068 uint32_t memorySize; 00069 00070 Array2D(){ 00071 data=NULL; 00072 width=-1; 00073 height=-1; 00074 rowBytes=-1; 00075 rows=NULL; 00076 memorySize=-1; 00077 } 00078 00079 public: 00080 Array2D( const int nXSize, const int nYSize){ 00081 data=NULL; 00082 width=nXSize; 00083 height=nYSize; 00084 rowBytes=-1; 00085 rows=NULL; 00086 memorySize=-1; 00087 } 00088 00090 ~Array2D(){ 00091 free(data); 00092 free(rows); 00093 } 00094 00095 00096 #ifdef __GNUC__ 00097 __attribute__((always_inline)) 00098 #endif 00099 inline T& operator()(uint32_t col, uint32_t row){ 00100 //if(col>=width || row>=height || col<0 || row<0){ 00101 // printf("ACCESS ERROR1 (%d %d) (%d %d)\n",col, row,width, height); 00102 00103 // void* callstack[128]; 00104 // int i, frames = backtrace(callstack, 128); 00105 // char** strs = backtrace_symbols(callstack, frames); 00106 // for (i = 0; i < frames; ++i) { 00107 // printf("%s\n", strs[i]); 00108 // } 00109 // free(strs); 00110 // testfunc(); 00111 // 00112 //} //--EUGENE 00113 00114 T* temp=rows[row]; 00115 return temp[col]; 00116 } 00117 00118 #ifdef __GNUC__ 00119 __attribute__((always_inline)) 00120 #endif 00121 inline T operator()(uint32_t col, uint32_t row) const{ 00122 /*if(col>=width || row>=height || col<0 || row<0){ 00123 printf("ACCESS ERROR2 (%d %d) (%d %d)\n",col, row,width, height); 00124 00125 void* callstack[128]; 00126 int i, frames = backtrace(callstack, 128); 00127 char** strs = backtrace_symbols(callstack, frames); 00128 for (i = 0; i < frames; ++i) { 00129 printf("%s\n", strs[i]); 00130 } 00131 free(strs); 00132 testfunc(); 00133 }*/ //EUGENE 00134 T* temp=rows[row]; 00135 return (temp)[col]; 00136 } 00137 00138 const uint32_t getXSize(){ 00139 return width; 00140 } 00141 00142 const uint32_t getYSize(){ 00143 return height; 00144 } 00145 00146 void testfunc(){ 00147 printf("testfunc"); 00148 } 00149 00150 void init(){ 00151 rowBytes =width* sizeof(T); 00152 //Widen rowBytes out to a integer multiple of 16 bytes 00153 rowBytes = (rowBytes + 15) & ~15; 00154 //Make sure we are not an even power of 2 wide. 00155 //Will loop a few times for rowBytes <= 16. 00156 while( 0 == (rowBytes & (rowBytes - 1) ) ) 00157 rowBytes += 16; //grow rowBytes 00158 memorySize = rowBytes * height; 00159 //Set up the buffer 00160 data = calloc( 1, memorySize+4); 00161 00162 if (!data){ 00163 qDebug() << "Matrix malloc error" << ERROR_LOCATION; 00164 exit(-ERROR_MATRIX_ALLOC_ERROR); 00165 } 00166 00167 rows=(T**)calloc(1, height*sizeof(T*)); 00168 for (uint32_t i= 0; i<height; i++){ 00169 rows[ i] = &(((T*)(data))[i*rowBytes/sizeof(T)]); 00170 } 00171 } 00172 00173 }; 00174 #endif /*ARRAY2D_H_INCLUDED*/ 00175