2929#include " include/stb_image_write.h"
3030
3131// Our modules
32- #include " address_map .hpp"
32+ #include " important_defines .hpp"
3333#include " ips_filter_tlm.hpp"
3434#include " sobel_edge_detector_tlm.hpp"
3535#include " img_receiver_tlm.hpp"
3636#include " img_transmiter_tlm.hpp"
3737
3838std::string filename;
3939bool debug_session = false ;
40+ int source_image = 0 ;
41+ int output_image = 0 ;
4042
4143/* *
4244 * @class Simulator
@@ -106,33 +108,236 @@ SC_MODULE(Simulator) {
106108 delete timer;
107109 }
108110
109- void load_img_from_memory ()
111+ void load_img_from_memory (int source_image_)
112+ {
113+ switch (source_image_)
114+ {
115+ case 0 :
116+ load_img_from_memory_from_fname (" ../../tools/datagen/src/imgs/car_rgb_noisy_image.jpg" , IMG_INPUT_ADDRESS_LO, sizeof (char ), false );
117+ break ;
118+ case 1 :
119+ load_img_from_memory_from_fname (" ../router/grayImagePrevMem.jpg" , IMG_INPROCESS_A_ADDRESS_LO, sizeof (char ), true );
120+ break ;
121+ case 2 :
122+ load_img_from_memory_from_fname (" ../router/filteredImagePrevMem.jpg" , IMG_COMPRESSED_ADDRESS_LO, sizeof (char ), true );
123+ break ;
124+ case 3 :
125+ load_img_from_memory_from_fname (" ../router/detectedImagePrevMemX.jpg" , IMG_INPROCESS_B_ADDRESS_LO, sizeof (short int ), true );
126+ load_img_from_memory_from_fname (" ../router/detectedImagePrevMemY.jpg" , IMG_INPROCESS_C_ADDRESS_LO, sizeof (short int ), true );
127+ break ;
128+ case 4 :
129+ load_img_from_memory_from_fname (" ../router/detectedImagePrevMem.jpg" , IMG_INPROCESS_A_ADDRESS_LO, sizeof (char ), true );
130+ break ;
131+ default :
132+ printf (" WARNING: The image source option I <image source> (0...4) provided %0d didn't match any expected source, the original image will be loaded\n " , source_image_);
133+ load_img_from_memory_from_fname (" ../../tools/datagen/src/imgs/car_rgb_noisy_image.jpg" , IMG_INPUT_ADDRESS_LO, sizeof (char ), false );
134+ break ;
135+ }
136+ }
137+
138+ void load_img_from_memory_from_fname (const char *filename, sc_dt::uint64 address, int pixel_byte_depth, bool use_memory)
110139 {
111140 int width, height, channels, pixel_count;
112- unsigned char *noisy_img, *noisy_img2 ;
141+ unsigned char *img_pointer ;
113142
114- noisy_img = stbi_load (" inputs/car_sobel_x_result.png" , &width, &height, &channels, 0 );
115- pixel_count = width * height * channels;
116- printf (" Pixel Count %0d, Width: %0d, Height: %0d \n " , pixel_count, width, height);
117- receiver_DUT->backdoor_write (noisy_img, pixel_count, IMG_INPUT_ADDRESS_LO);
143+ img_pointer = stbi_load (filename, &width, &height, &channels, 0 );
144+
145+ if (img_pointer == NULL )
146+ {
147+ printf (" WARNING: The image source was not found and will not be loaded\n " );
148+ return ;
149+ }
118150
119- noisy_img = stbi_load (" inputs/car_sobel_y_result.png" , &width, &height, &channels, 0 );
120151 pixel_count = width * height * channels;
121- receiver_DUT->backdoor_write (noisy_img, pixel_count, IMG_INPUT_ADDRESS_LO + pixel_count);
152+ printf (" Pixel Count %0d, Width: %0d, Height: %0d, Channels: %0d\n " , pixel_count, width, height, channels);
153+
154+ if (pixel_byte_depth == 1 )
155+ {
156+ unsigned char pixel_value[3 ];
157+ unsigned char *pixel_value_ptr;
158+ pixel_value[0 ] = 0 ;
159+ pixel_value[1 ] = 0 ;
160+ pixel_value[2 ] = 0 ;
161+
162+ for (int i = 0 ; i < height; i++)
163+ {
164+ pixel_value_ptr = img_pointer + (i * channels * width);
165+ if (use_memory == true )
166+ {
167+ MainMemory->backdoor_write (pixel_value_ptr, width * channels, address + (i * channels * IMAG_COLS));
168+ }
169+ else
170+ {
171+ receiver_DUT->backdoor_write (pixel_value_ptr, width * channels, address + (i * channels * IMAG_COLS));
172+ }
173+ }
174+
175+ pixel_value_ptr = pixel_value;
176+
177+ // Fill remaining rows with black pixels
178+ for (int i = height; i < IMAG_ROWS; i++)
179+ {
180+ for (int j = 0 ; j < IMAG_COLS; j++)
181+ {
182+ if (use_memory == true )
183+ {
184+ MainMemory->backdoor_write (pixel_value_ptr, channels, address + ((i * channels * IMAG_COLS) + (j * channels)));
185+ }
186+ else
187+ {
188+ receiver_DUT->backdoor_write (pixel_value_ptr, channels, address + ((i * channels * IMAG_COLS) + (j * channels)));
189+ }
190+ }
191+ }
192+
193+ // Fill remaining columns with black pixels
194+ for (int j = width; j < IMAG_COLS; j++)
195+ {
196+ for (int i = 0 ; i < IMAG_ROWS; i++)
197+ {
198+ if (use_memory == true )
199+ {
200+ MainMemory->backdoor_write (pixel_value_ptr, channels, address + ((i * channels * IMAG_COLS) + (j * channels)));
201+ }
202+ else
203+ {
204+ receiver_DUT->backdoor_write (pixel_value_ptr, channels, address + ((i * channels * IMAG_COLS) + (j * channels)));
205+ }
206+ }
207+ }
208+ }
209+ else
210+ {
211+ unsigned char pixel_value = 0 ;
212+ unsigned char *pixel_value_ptr;
213+
214+ for (int i = 0 ; i < height; i++)
215+ {
216+ for (int j = 0 ; j < width; j++)
217+ {
218+ pixel_value_ptr = img_pointer + ((i * width) + j);
219+ MainMemory->backdoor_write (pixel_value_ptr, 1 , address + ((i * IMAG_COLS * pixel_byte_depth) + (j * pixel_byte_depth)));
220+ }
221+ }
222+
223+ pixel_value_ptr = &pixel_value;
224+
225+ // Fill remaining rows with black pixels
226+ for (int i = height; i < IMAG_ROWS; i++)
227+ {
228+ for (int j = 0 ; j < IMAG_COLS; j++)
229+ {
230+ MainMemory->backdoor_write (pixel_value_ptr, 1 , address + ((i * IMAG_COLS * pixel_byte_depth) + (j * pixel_byte_depth)));
231+ }
232+ }
233+
234+ // Fill remaining columns with black pixels
235+ for (int j = width; j < IMAG_COLS; j++)
236+ {
237+ for (int i = 0 ; i < IMAG_ROWS; i++)
238+ {
239+ MainMemory->backdoor_write (pixel_value_ptr, 1 , address + ((i * IMAG_COLS * pixel_byte_depth) + (j * pixel_byte_depth)));
240+ }
241+ }
242+ }
122243 }
123244
124- void save_img_from_memory ()
245+ void save_img_from_memory (int output_image_ )
125246 {
126- int width, height, channels, pixel_count;
247+ int channels, pixel_count;
127248 unsigned char *img_ptr;
128249
129- channels = 1 ;
130- width = 640 ;
131- height = 452 ;
132- pixel_count = width * height * channels;
133- transmiter_DUT->backdoor_read (img_ptr, pixel_count, IMG_OUTPUT_ADDRESS_LO);
134-
135- stbi_write_png (" outputs/car_filtered_image.png" , width, height, channels, img_ptr, width*channels);
250+ if (output_image_ == 0 )
251+ {
252+ channels = 3 ;
253+ pixel_count = IMAG_COLS * IMAG_ROWS * channels;
254+ receiver_DUT->backdoor_read (img_ptr, pixel_count, IMG_INPUT_ADDRESS_LO);
255+
256+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
257+ }
258+ else
259+ {
260+ channels = 1 ;
261+ pixel_count = IMAG_COLS * IMAG_ROWS;
262+ switch (output_image_) {
263+ case 1 :
264+ MainMemory->backdoor_read (img_ptr, pixel_count, IMG_INPROCESS_A_ADDRESS_LO);
265+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
266+ break ;
267+ case 2 :
268+ MainMemory->backdoor_read (img_ptr, pixel_count, IMG_COMPRESSED_ADDRESS_LO);
269+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
270+ break ;
271+ case 3 :
272+ unsigned char *read_ptr;
273+ short int *value_ptr;
274+
275+ img_ptr = new unsigned char [IMAG_COLS * IMAG_ROWS];
276+
277+ MainMemory->backdoor_read (read_ptr, pixel_count * sizeof (short int ), IMG_INPROCESS_B_ADDRESS_LO);
278+ value_ptr = (short int *)read_ptr;
279+
280+ for (int i = 0 ; i < IMAG_ROWS; i++)
281+ {
282+ for (int j = 0 ; j < IMAG_COLS; j++)
283+ {
284+ if ((*(value_ptr + ((i * IMAG_COLS) + j)) > 255 ) || ((*(value_ptr + ((i * IMAG_COLS) + j)) < -255 )))
285+ {
286+ *(img_ptr + ((i * IMAG_COLS) + j)) = 255 ;
287+ }
288+ else if (*(value_ptr + ((i * IMAG_COLS) + j)) < 0 )
289+ {
290+ *(img_ptr + ((i * IMAG_COLS) + j)) = -*(value_ptr + ((i * IMAG_COLS) + j));
291+ }
292+ else
293+ {
294+ *(img_ptr + ((i * IMAG_COLS) + j)) = *(value_ptr + ((i * IMAG_COLS) + j));
295+ }
296+ }
297+ }
298+ delete[] read_ptr;
299+
300+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
301+
302+ MainMemory->backdoor_read (read_ptr, pixel_count * sizeof (short int ), IMG_INPROCESS_C_ADDRESS_LO);
303+ value_ptr = (short int *)read_ptr;
304+
305+ for (int i = 0 ; i < IMAG_ROWS; i++)
306+ {
307+ for (int j = 0 ; j < IMAG_COLS; j++)
308+ {
309+ if ((*(value_ptr + ((i * IMAG_COLS) + j)) > 255 ) || ((*(value_ptr + ((i * IMAG_COLS) + j)) < -255 )))
310+ {
311+ *(img_ptr + ((i * IMAG_COLS) + j)) = 255 ;
312+ }
313+ else if (*(value_ptr + ((i * IMAG_COLS) + j)) < 0 )
314+ {
315+ *(img_ptr + ((i * IMAG_COLS) + j)) = -*(value_ptr + ((i * IMAG_COLS) + j));
316+ }
317+ else
318+ {
319+ *(img_ptr + ((i * IMAG_COLS) + j)) = *(value_ptr + ((i * IMAG_COLS) + j));
320+ }
321+ }
322+ }
323+ delete[] read_ptr;
324+
325+ stbi_write_png (" output_image_1.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
326+ break ;
327+ case 4 :
328+ MainMemory->backdoor_read (img_ptr, pixel_count, IMG_INPROCESS_A_ADDRESS_LO);
329+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
330+ break ;
331+ default :
332+ printf (" WARNING: The image output option O <image output> (0...4) provided %0d didn't match any expected output, the original image be saved\n " , output_image_);
333+ channels = 3 ;
334+ pixel_count = IMAG_COLS * IMAG_ROWS * channels;
335+ receiver_DUT->backdoor_read (img_ptr, pixel_count, IMG_INPROCESS_A_ADDRESS_LO);
336+ stbi_write_png (" output_image_0.png" , IMAG_COLS, IMAG_ROWS, channels, img_ptr, IMAG_COLS * channels);
337+ break ;
338+ }
339+ }
340+ delete[] img_ptr;
136341 }
137342};
138343
@@ -156,7 +361,16 @@ void process_arguments(int argc, char *argv[]) {
156361
157362 debug_session = false ;
158363
159- while ((c = getopt (argc, argv, " DL:f:?" )) != -1 ) {
364+ if ((c = getopt (argc, argv, " I:" )) == -1 )
365+ {
366+ printf (" WARNING: The image source option I <image source> (0...4) was not provided, the original image will be loaded\n " );
367+ }
368+ if ((c = getopt (argc, argv, " O:" )) == -1 )
369+ {
370+ printf (" WARNING: The image output option O <image output> (0...4) was not provided, the original image will be saved\n " );
371+ }
372+
373+ while ((c = getopt (argc, argv, " DL:I:O:f:?" )) != -1 ) {
160374 switch (c) {
161375 case ' D' :
162376 debug_session = true ;
@@ -185,6 +399,12 @@ void process_arguments(int argc, char *argv[]) {
185399 case ' f' :
186400 filename = std::string (optarg);
187401 break ;
402+ case ' I' :
403+ source_image = std::atoi (optarg);
404+ break ;
405+ case ' O' :
406+ output_image = std::atoi (optarg);
407+ break ;
188408 case ' ?' :
189409 std::cout << " Call ./RISCV_TLM -D -L <debuglevel> (0..3) filename.hex"
190410 << std::endl;
@@ -263,7 +483,7 @@ int sc_main(int argc, char *argv[]) {
263483 sc_trace (wf, top->sobel_edge_detector_DUT ->resultSobelGradientX , " sobel_resultSobelGradientX" );
264484 sc_trace (wf, top->sobel_edge_detector_DUT ->resultSobelGradientY , " sobel_resultSobelGradientY" );
265485
266- top->load_img_from_memory ();
486+ top->load_img_from_memory (source_image );
267487
268488 auto start = std::chrono::steady_clock::now ();
269489 sc_core::sc_start ();
@@ -276,7 +496,7 @@ int sc_main(int argc, char *argv[]) {
276496 std::cout << " Simulated " << int (std::round (instructions)) << " instr/sec" << std::endl;
277497
278498 // Generate output image
279- top->save_img_from_memory ();
499+ top->save_img_from_memory (output_image );
280500
281501 sc_close_vcd_trace_file (wf);
282502 std::cout << " Press Enter to finish" << std::endl;
0 commit comments