Convert a text file into an image (or generate an image with fortran)

Click For Summary
SUMMARY

This discussion focuses on converting a text file containing numerical data into an image file using Fortran. The user, Joe, seeks to represent numbers as pixel colors in an image format. The response outlines the differences between image formats, emphasizing the simplicity of creating a RAW BMP image. Key points include the use of 24-bit RGB for pixel representation and the necessity of including header information for image dimensions.

PREREQUISITES
  • Understanding of image file formats (BMP, JPEG, TIFF)
  • Familiarity with binary output in programming languages like Fortran or C
  • Knowledge of pixel color representation (RGB values)
  • Basic understanding of bitstream documentation and image headers
NEXT STEPS
  • Research BMP file format specifications and header requirements
  • Learn about the Discrete Cosine Transform (DCT) for JPEG encoding
  • Explore binary file handling in Fortran or C
  • Investigate color lookup tables for pixel color mapping
USEFUL FOR

Programmers, particularly those working with Fortran or C, graphic designers, and anyone interested in image processing and file format conversions.

Joe1
Messages
11
Reaction score
0
Hello,
I just made a program that puts numbers into a two dimensional array, and then saves them in a text file that looks something like this:
4 4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6
4 4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7
4 4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7
4 4 4 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 7 7 7
(except it is 800 by 800)
is there any way I can make the text file into a picture file and have the numbers represent colors of individual pixels? Otherwise is there a way to input data into some fortran function that will give me an image? Any type of image file is good.
Thanks,
Joe
 
Computer science news on Phys.org
Yes. In fact, you will find that it can be relatively easy. You have to be aware first that there are various formats for images. You may have heard of CompuServ's GIF, or JPEG, or TIFF. These formats are non-RAW, meaning they are encoded (e.g. compressed) in a certain way. JPEG uses the Discrete Cosine Transform (DCT) for example, and if you want to create a JPEG file, you can refer to bitstream documentation available on the web which will include overhead information (which can unfortunately become complex). Algorithms for DCT are also widely available.

However, if you are only wishing to create a RAW picture (which is the easy case), you will be looking at BMP or YUV (for the RAW video case). Again, bitstream documentation is available on the web, but the basics go as follow:

For BMP, there is an option of whether you want 8-bit, 16-bit, 24-bit.

8-bit means you have a total of 256 color choices per pixel on the screen, 16-bit is 2^16 and so on. Normally, 24-bit (RGB) is used. So if you wanted to show a white pixel in 24-bit RGB, you would output:

FF FF FF

The first byte (FF) is for max red, the next for max green, the next for max blue. In pascal or C, use char and putchar() for each byte. Make sure you are using binary output as well. You may need a look up table if you want to find out what color (45 FE 22) is for example.

As usual, header information is required, so that your image software knows how big the image dimensions are. The bitstream is literally a stream. Try opening a BMP file using a hex editor. You will see that the pixel values just go on and on. You can read up on this at the link i posted below.

http://www.fortunecity.com/skyscraper/windows/364/bmpffrmt.html#bmih
 
Last edited by a moderator:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
7K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K