OpenCores
URL https://opencores.org/ocsvn/lq057q3dc02/lq057q3dc02/trunk

Subversion Repositories lq057q3dc02

[/] [lq057q3dc02/] [trunk/] [software/] [Java/] [Gen_LCD_Image.java] - Blame information for rev 47

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 jwdonal
//----------------------------------------------------------------------------
2
// Copyright (C) 2007 Jonathon W. Donaldson
3
//                    jwdonal a t opencores DOT org
4
//
5
//  This program is free software; you can redistribute it and/or modify
6
//  it under the terms of the GNU General Public License as published by
7
//  the Free Software Foundation; either version 2 of the License, or
8
//  (at your option) any later version.
9
//
10
//  This program is distributed in the hope that it will be useful,
11
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
//  GNU General Public License for more details.
14
//
15
//  You should have received a copy of the GNU General Public License
16
//  along with this program; if not, write to the Free Software
17
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
//
19
//----------------------------------------------------------------------------
20
//
21 9 jwdonal
//  $Id: Gen_LCD_Image.java,v 1.2 2007-05-29 04:15:10 jwdonal Exp $
22 5 jwdonal
//
23
//  Description: This program parses the RGB COE files in binary, decimal
24
//  or hex format and displays what the image should look like on the LCD
25
//  if the COE files given to this app are the same ones given to the Xilinx
26
//  CoreGen tool for each color's BRAM instance.
27
//  
28
//---------------------------------------------------------------------
29
import java.io.BufferedReader;
30
import java.io.FileReader;
31
import java.io.FileNotFoundException;
32
import java.io.IOException;
33
 
34
import java.awt.image.BufferedImage;
35
import javax.swing.JLabel;
36
import javax.swing.JFrame;
37
import javax.swing.ImageIcon;
38
 
39
public class Gen_LCD_Image {
40
 
41
  //File locations within filenames[] member variable
42
  private static final int RED_LOC = 0;
43
  private static final int GREEN_LOC = 1;
44
  private static final int BLUE_LOC = 2;
45
  private static final int HDR_LOC = 3;
46
 
47
  private String[] filenames;
48
  private BufferedReader hdrFile;
49
  private BufferedReader[] coeFile = new BufferedReader[NUM_COE_FILES];
50
 
51
  private static final int RADIX_BIN = 2;
52
  private static final int RADIX_DEC = 10;
53
  private static final int RADIX_HEX = 16;
54
  private static final int NUM_COE_FILES = 3;
55
  private static final int BRAM_WIDTH_MAX = 8;
56
 
57
  private BufferedImage lcd_image;
58
  private JFrame lcd_frame;
59
 
60
  private int image_width;
61
  private int image_height;
62
  private int bram_width;
63
  private int radix_vals[] = new int[NUM_COE_FILES];
64
 
65
  private String currByteRed;
66
  private String currByteGreen;
67
  private String currByteBlue;
68
 
69
  private int currByteIntRed;
70
  private int currByteIntGreen;
71
  private int currByteIntBlue;
72
 
73
  private String hdrFileStr;
74
 
75
 
76
  /**
77
   *  Constructor for a Gen_LCD_Image object.
78
   *
79
   *  @param  filenames 3 COE files (R,G,and B) and 1 Header File
80
   **/
81
  public Gen_LCD_Image( String[] filenames  ) {
82
 
83
    this.filenames = filenames;
84
 
85
    image_width = 0;
86
    image_height = 0;
87
    bram_width = 0;
88
 
89
    currByteRed = "uninitialized";
90
    currByteGreen = "uninitialized";
91
    currByteBlue = "uninitialized";
92
 
93
    currByteIntRed = 0;
94
    currByteIntGreen = 0;
95
    currByteIntBlue = 0;
96
 
97
    hdrFileStr = "uninitialized";
98
 
99
  }
100
 
101
 
102
  public void openHeaderFile() throws FileNotFoundException {
103
 
104
    //OPEN the header file
105
    hdrFile = new BufferedReader(
106
              new FileReader( filenames[HDR_LOC] ));
107
 
108
  }
109
 
110
  public void openCoeFiles() throws FileNotFoundException {
111
 
112
    //OPEN the COE files for each of the three colors
113
    for( int fileNum = RED_LOC; fileNum < NUM_COE_FILES; fileNum++ ) {
114
 
115
      //open the file
116
      coeFile[fileNum] = new BufferedReader(
117
                         new FileReader( filenames[fileNum]) );
118
 
119
    } //end for loop
120
 
121
  }
122
 
123
 
124
  public void checkCoeFileOrder() throws IOException {
125
 
126
    //OPEN the COE files for each of the three colors
127
    for( int fileNum = 0; fileNum < NUM_COE_FILES; fileNum++ ) {
128
 
129
      if( fileNum == RED_LOC ) {
130
 
131
        if( !coeFile[fileNum].readLine().matches(";.*RED.*") ) {
132
          System.err.println( "The first line of the red COE file did not contain the correct color identifier!  Please ensure that the file contains the word 'RED' somewhere in the first line!" );
133
          System.exit( 1 );
134
        }
135
 
136
      } else if( fileNum == GREEN_LOC ) {
137
 
138
        if( !coeFile[fileNum].readLine().matches(";.*GREEN.*") ) {
139
          System.err.println( "The first line of the green COE file did not contain the correct color identifier!  Please ensure that the file contains the word 'GREEN' somewhere in the first line!" );
140
          System.exit( 1 );
141
        }
142
 
143
      } else { //if BLUE_LOC
144
 
145
        if( !coeFile[fileNum].readLine().matches(";.*BLUE.*") ) {
146
          System.err.println( "The first line of the blue COE file did not contain the correct color identifier!  Please ensure that the file contains the word 'BLUE' somewhere in the first line!" );
147
          System.exit( 1 );
148
        }
149
 
150
      } //end if/else_if/else
151
 
152
    } //end for loop
153
 
154
  } //end checkCoeFileOrder()
155
 
156
 
157
  public void getImageWidth() throws IOException {
158
 
159
    //Fetch the Image Width from the header file stream
160
    do {
161
 
162
      hdrFileStr = hdrFile.readLine();
163
 
164
    } while( !hdrFileStr.matches( ".*Image Width" ) );
165
 
166
    image_width = Integer.parseInt(hdrFileStr.substring(0, hdrFileStr.indexOf( "h" )), 16 );
167
 
168
    System.out.println( "\nImage Width = " + image_width );
169
 
170
  } //end getImageWidth
171
 
172
 
173
  public void getImageHeight() throws IOException {
174
 
175
    //Fetch the Image Height from the header file stream
176
    do {
177
 
178
      hdrFileStr = hdrFile.readLine();
179
 
180
    } while( !hdrFileStr.matches( ".*Image Height" ) );
181
 
182
    image_height = Integer.parseInt(hdrFileStr.substring(0, hdrFileStr.indexOf( "h" )), 16 );
183
 
184
    System.out.println( "\nImage Height = " + image_height );
185
 
186
  } //end getImageHeight
187
 
188
 
189
  public void getBramWidth() throws IOException {
190
 
191
    //Fetch BRAM data width from header file
192
    do {
193
 
194
      hdrFileStr = hdrFile.readLine();
195
 
196
    } while( !hdrFileStr.matches( "Width = .*" ) );
197
 
198
    bram_width = Integer.parseInt(hdrFileStr.substring(hdrFileStr.lastIndexOf(" ")+1, hdrFileStr.length()), 10 );
199
 
200
    System.out.println( "\nBRAM Width = " + bram_width );
201
 
202
  } //getBramWidth
203
 
204
 
205
  public void getRadixValues() throws IOException {
206
 
207 9 jwdonal
    String temp = "unintialized";
208
 
209 5 jwdonal
    for( int fileNum = 0; fileNum < NUM_COE_FILES; fileNum++ ) {
210
 
211
      //Scan for the memory initialization radix value for each file
212
      do {
213
        // a whole lotta nothin until I say stop
214
      } while( !coeFile[fileNum].readLine().matches( "MEMORY_INITIALIZATION_RADIX.*" ) );
215
 
216
      //Capture the RADIX value for each color
217
      if( fileNum == RED_LOC ) {
218
 
219
        temp = coeFile[RED_LOC].readLine();
220
        radix_vals[RED_LOC] = Integer.parseInt( temp.substring(0, temp.length()-1).trim() ); //remove the semi-colon and get rid of any white space
221
        System.out.println( "\nRed radix = " + radix_vals[RED_LOC] );
222
 
223
      } else if( fileNum == GREEN_LOC ) {
224
 
225
        temp = coeFile[GREEN_LOC].readLine();
226
        radix_vals[GREEN_LOC] = Integer.parseInt( temp.substring(0, temp.length()-1).trim() );
227
        System.out.println( "\nGreen radix = " + radix_vals[GREEN_LOC] );
228
 
229
      } else { // BLUE_LOC
230
 
231
        temp = coeFile[BLUE_LOC].readLine();
232
        radix_vals[BLUE_LOC] = Integer.parseInt( temp.substring(0, temp.length()-1).trim() );
233
        System.out.println( "\nBlue radix = " + radix_vals[BLUE_LOC] );
234
 
235
      } //end if/else_if/else
236
 
237
    } //end for loop for each COE file
238
 
239
  }
240
 
241
 
242
  public void checkRadixValues() {
243
 
244
    for( int num = RED_LOC; num < NUM_COE_FILES; num++ ) {
245
 
246
      if( radix_vals[num] != RADIX_BIN &&
247
          radix_vals[num] != RADIX_DEC &&
248
          radix_vals[num] != RADIX_HEX ) {
249
 
250
        System.err.println( "RADIX value `" + radix_vals[num] + "' is not of valid type!  RADIX must be 2, 10, or 16" );
251
        System.exit( 1 );
252
 
253
      }
254
 
255
    } // end for loop
256
 
257
    // make sure all radix values are the same
258
    if( !(radix_vals[RED_LOC] == radix_vals[GREEN_LOC] &&
259
          radix_vals[RED_LOC] == radix_vals[BLUE_LOC])   ) {
260
 
261
      System.err.println( "WARNING! RADIX values in each color file do NOT match!" );
262
      System.err.println( "I received red_radix = " + radix_vals[RED_LOC] +
263
                                   ", green_radix = " + radix_vals[GREEN_LOC] +
264
                                   ", blue_radix = " + radix_vals[BLUE_LOC] );
265
    }
266
 
267
  }
268
 
269
 
270
  public void getImageData() throws IOException {
271
 
272
    //Create a BufferedImage object that is the same size as the actual image.
273
    //This will be used to display the expected image for the LCD
274
    lcd_image = new BufferedImage(image_width, image_height, BufferedImage.TYPE_INT_ARGB);
275
 
276
    for( int fileNum = RED_LOC; fileNum < NUM_COE_FILES; fileNum++ ) {
277
 
278
      //Immediately scan each of the COE file parses down to the line just above
279
      //the first byte of image data.
280
      do {
281
        //a whole lotta nothing - just keep skipping lines until I say stop!
282
      } while( !coeFile[fileNum].readLine().matches("MEMORY_INITIALIZATION_VECTOR.*") );
283
 
284
    }
285
 
286
    //Fetch the data for each of the 3 colors from the files
287
    //and set the pixel colors inside the BufferedImage
288
    for( int rowNum = 0; rowNum < image_height; rowNum++ ) {
289
 
290
      for( int pixNum = 0; pixNum < image_width; pixNum++ ) {
291
 
292
        //RED data control
293
        currByteRed = coeFile[RED_LOC].readLine(); //get next data byte as string
294
        currByteRed = currByteRed.substring( 0, currByteRed.length()-1 ); //remove the last character (, or ;)
295
        currByteIntRed = Integer.parseInt( currByteRed, radix_vals[RED_LOC] ) << BRAM_WIDTH_MAX - bram_width; //Convert the hex string to an integer value (shift as necessary to get the greatest number of most significant bits that the LCD is capable of displaying)
296
 
297
        //GREEN data control
298
        currByteGreen = coeFile[GREEN_LOC].readLine();
299
        currByteGreen = currByteGreen.substring( 0, currByteGreen.length()-1 );
300
        currByteIntGreen = Integer.parseInt( currByteGreen, radix_vals[GREEN_LOC] ) << BRAM_WIDTH_MAX - bram_width;
301
 
302
        //BLUE data control
303
        currByteBlue = coeFile[BLUE_LOC].readLine();
304
        currByteBlue = currByteBlue.substring( 0, currByteBlue.length()-1 );
305
        currByteIntBlue = Integer.parseInt( currByteBlue, radix_vals[BLUE_LOC] ) << BRAM_WIDTH_MAX - bram_width;
306
 
307
        lcd_image.setRGB(pixNum, rowNum,
308
              ( (255 << 24)              |
309
                (currByteIntRed   << 16) |
310
                (currByteIntGreen << 8 ) |
311
                (currByteIntBlue       ) )
312
        );
313
 
314
      } // end column (pixel) loop
315
 
316
    } // end row (line) loop
317
 
318
  } //end getImageData
319
 
320
 
321
  public void showImage() {
322
 
323
    //This creates the JFrame to hold the BufferedImage
324
    lcd_frame = new JFrame ("LCD Display");
325
 
326
    //Insert the image (lcd_image) into the fram
327
    lcd_frame.getContentPane().add( new JLabel( new ImageIcon( lcd_image ) ) );
328
 
329
    //Make the frame the same size as the subcomponents (i.e. the lcd_image)
330
    lcd_frame.pack();
331
 
332
    lcd_frame.setResizable(false);
333
    lcd_frame.setVisible(true); //I think we'd like to see the image, yes? :-)
334
    lcd_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
335
 
336
 
337
  } // end showImage
338
 
339
  public void closeFiles() throws IOException {
340
 
341
    //CLOSE all COE file streams
342
    for( int fileNum = 0; fileNum < NUM_COE_FILES; fileNum++ ) {
343
      coeFile[fileNum].close();
344
    }
345
 
346
    //CLOSE header file stream
347
    hdrFile.close();
348
 
349
  }
350
 
351
 
352
  //--MAIN METHOD--//
353
  public static void main( String[] args ) {
354
 
355
 
356
    if( args.length != 4 ) {
357
      System.err.println( "Usage: java Gen_LCD_Image <r_coe_file_name> <g_coe_file_name> <b_coe_file_name> <hdr_file_name>" );
358
      System.exit(1);
359
    }
360
 
361
    //Create new Gen_LCD_Image object
362
    Gen_LCD_Image myLCDImage = new Gen_LCD_Image( args );
363
 
364
    //NOTE! There are no built-in sanity checks on
365
    //which order these functions are called in.  Make
366
    //sure you call the functions to open the files
367
    //before calling functions that use those files.
368
    //There are other functions that rely on others
369
    //to run first as well.  Just run them in the
370
    //order below and you'll be fine.
371
 
372
    //Open the provided files
373
    try {
374
      myLCDImage.openHeaderFile();
375
      myLCDImage.openCoeFiles();
376
 
377
    } catch( FileNotFoundException FNFE ) {
378
      System.err.println( FNFE.getMessage() );
379
 
380
    }
381
 
382
    //Get preliminary information
383
    try {
384
      myLCDImage.checkCoeFileOrder();
385
      myLCDImage.getImageWidth();
386
      myLCDImage.getImageHeight();
387
      myLCDImage.getBramWidth();
388
      myLCDImage.getRadixValues();
389
 
390
    } catch( IOException IOE ) {
391
      System.err.println( IOE.getMessage() );
392
    }
393
 
394
    //Ensure the radix values are valid
395
    myLCDImage.checkRadixValues();
396
 
397
    //Read the image data out of the COE files
398
    try {
399
      myLCDImage.getImageData();
400
 
401
    } catch( IOException IOE ) {
402
      System.err.println( IOE.getMessage() );
403
    }
404
 
405
    //Display the image to the screen
406
    myLCDImage.showImage();
407
 
408
    //CLose all file streams
409
    try {
410
      myLCDImage.closeFiles();
411
 
412
    } catch( IOException IOE ) {
413
      System.err.println( IOE.getMessage() );
414
    }
415
 
416
  } //end of main method
417
 
418
} // end of Gen_LCD_Image class

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.