summaryrefslogtreecommitdiff
path: root/src/tests/pic.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/pic.cpp')
-rw-r--r--src/tests/pic.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/tests/pic.cpp b/src/tests/pic.cpp
index 95bb523..4f4139f 100644
--- a/src/tests/pic.cpp
+++ b/src/tests/pic.cpp
@@ -3,8 +3,20 @@
3#include "neural/row.h" 3#include "neural/row.h"
4#include "neural/neuron.h" 4#include "neural/neuron.h"
5 5
6#include <time.h>
7
8#include <bu/random.h>
9#include <bu/sio.h>
10using namespace Bu;
11
12#include <stdio.h>
13#include <png.h>
14#include <zlib.h>
15
6int main( int argc, char *argv[] ) 16int main( int argc, char *argv[] )
7{ 17{
18 Bu::Random::seed( time( NULL ) );
19
8 Neural::Column<float> *c = new Neural::Column<float>(); 20 Neural::Column<float> *c = new Neural::Column<float>();
9 Neural::Row<float> *r1 = new Neural::Row<float>(); 21 Neural::Row<float> *r1 = new Neural::Row<float>();
10 r1->addNode( new Neural::Neuron<float>() ); 22 r1->addNode( new Neural::Neuron<float>() );
@@ -27,6 +39,107 @@ int main( int argc, char *argv[] )
27 r3->addNode( new Neural::Neuron<float>() ); 39 r3->addNode( new Neural::Neuron<float>() );
28 r3->addNode( new Neural::Neuron<float>() ); 40 r3->addNode( new Neural::Neuron<float>() );
29 c->addNode( r3 ); 41 c->addNode( r3 );
42
43 c->finalize( 2 );
44 sio << "Total weights: " << c->getNumWeights() << sio.nl;
45 sio << "Total biases: " << c->getNumBiases() << sio.nl;
46 sio << "Network inputs: " << c->getNumInputs() << sio.nl;
47 sio << "Network outputs: " << c->getNumOutputs() << sio.nl;
48
49 float *pWeights = new float[c->getNumWeights()];
50 float *pBiases = new float[c->getNumBiases()];
51
52 for( int j = 0; j < c->getNumWeights(); j++ )
53 pWeights[j] = (Bu::Random::randNorm()*2.0)-1.0;
54 for( int j = 0; j < c->getNumBiases(); j++ )
55 pBiases[j] = (Bu::Random::randNorm()*2.0)-1.0;
56
57 c->setWeights( pWeights );
58 c->setBiases( pBiases );
59 delete pWeights;
60 delete pBiases;
61
62 float *pIn = new float[c->getNumInputs()];
63 float *pOut = new float[c->getNumOutputs()];
64
65 FILE *fp = fopen("test.png", "wb");
66
67 if (!fp)
68 return 1;
69
70 png_structp png_ptr = png_create_write_struct
71 (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
72
73 if (!png_ptr)
74 return 1;
75
76 png_infop info_ptr = png_create_info_struct(png_ptr);
77 if (!info_ptr)
78 {
79 png_destroy_write_struct(&png_ptr,
80 (png_infopp)NULL);
81 return 1;
82 }
83
84 png_set_IHDR(png_ptr, info_ptr, 500, 500, 8, PNG_COLOR_TYPE_RGB,
85 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
86 PNG_FILTER_TYPE_DEFAULT
87 );
88
89 /* Set the zlib compression level */
90 png_set_compression_level(png_ptr,
91 Z_BEST_COMPRESSION);
92
93 /* Set other zlib parameters for compressing IDAT */
94 png_set_compression_mem_level(png_ptr, 8);
95 png_set_compression_strategy(png_ptr,
96 Z_DEFAULT_STRATEGY);
97 png_set_compression_window_bits(png_ptr, 15);
98 png_set_compression_method(png_ptr, 8);
99 png_set_compression_buffer_size(png_ptr, 8192);
100
101 /* Set zlib parameters for text compression
102 * If you don't call these, the parameters
103 * fall back on those defined for IDAT chunks
104 */
105 png_set_text_compression_mem_level(png_ptr, 8);
106 png_set_text_compression_strategy(png_ptr,
107 Z_DEFAULT_STRATEGY);
108 png_set_text_compression_window_bits(png_ptr, 15);
109 png_set_text_compression_method(png_ptr, 8);
110
111 png_bytep *row_pointers;
112 row_pointers = new png_bytep[500];
113 for( int y = 0; y < 500; y++ )
114 {
115 row_pointers[y] = new png_byte[500*3];
116 for( int x = 0; x < 500; x++ )
117 {
118 pIn[0] = (x/499.0)*2.0-1.0;
119 pIn[1] = (y/499.0)*2.0-1.0;
120 c->process( pIn, pOut );
121 row_pointers[y][x*3+0] = pOut[0]*127+127;
122 row_pointers[y][x*3+1] = pOut[1]*127+127;
123 row_pointers[y][x*3+2] = pOut[2]*127+127;
124 }
125 }
126
127 png_init_io( png_ptr, fp );
128
129 png_set_rows( png_ptr, info_ptr, row_pointers );
130
131 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
132
133 if (setjmp(png_jmpbuf(png_ptr)))
134 {
135 png_destroy_write_struct(&png_ptr, &info_ptr);
136 fclose(fp);
137 return 1;
138 }
139 png_destroy_write_struct( &png_ptr, &info_ptr );
140 fclose( fp );
141
142 delete c;
30 143
31 return 0; 144 return 0;
32} 145}