473,408 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to Turn Array of integers into image file

For a project (homework), I need to manipulate an Array of integers (2D) so that only certain positions take the value 1, and all others take the value 0.

The Array is 800x600 in size, but may vary depending on the instance.

This works fine, the problem lies in the output:

To make output more readable (800x600 Array, dumped into a text file, is annoying to check for errors), I would like to turn this into an Image (.bmp preferred, but it doesn't really matter)

A value of 1 should colour the corresponding pixel black, 0 white. Is there a way to do this without manually building the entire image?

eg
0100001000
0110001100
1111001110
1100010010
1111100000
should turn into a 10x5 pixel image.


If not, does anyone how the .bmp file would have to be created? I know the basics of it's structure, and would have to write it in binary mode, but what values does the header need?

I found some stuff for this, but it was all C++, and I only want to use C.


To make it clear: The assignment is NOT to create the image, technically, the .txt file is ENOUGH. I merely want to make it look nicer. This will not affect my mark in any way (solving the problem is the question, not nice output)
Just to make sure this doesn't get deleted as "Homework Assignment". It isn't.

Hope I can be helped :)
Jun 24 '10 #1

✓ answered by Banfa

lol this is C, since the vast majority of platforms have non-monitor let alone any graphics handling capability there is no cause to provide standard implementations for this sort of operation.

Also writing binary data like that would be/is considered a rather simple job. All you have to do is write a header structure and the data for each pixel. I suspect if you choose the header correctly you could make each pixel a byte which should simply things.

And yes that opens the file correctly.

Or to keep the file size small you could just have 1 bit per pixel.

Anyway here is a good place to start

6 4724
hype261
207 100+
@roflmfao
From wikipedia here is the information about the bitmap file format. To create a bitmap you just need to change your file extension from .txt to .bmp and follow all the rules for the header and scanline padding.

http://en.wikipedia.org/wiki/BMP_file_format
Jun 24 '10 #2
Thanks for that. I then take it there is no easier way to do this?
Jun 24 '10 #3
Banfa
9,065 Expert Mod 8TB
Than just writing out the file in the correct format? No
Jun 24 '10 #4
@Banfa
I thought there might be a header file I am unaware of, or some pre-written .c file including what I need. Anyways, it seems there isn't...

Expand|Select|Wrap|Line Numbers
  1. FILE* output = fopen("output.bmp", "wb");
This correctly opens "output.bmp" in binary mode?
Jun 25 '10 #5
Banfa
9,065 Expert Mod 8TB
lol this is C, since the vast majority of platforms have non-monitor let alone any graphics handling capability there is no cause to provide standard implementations for this sort of operation.

Also writing binary data like that would be/is considered a rather simple job. All you have to do is write a header structure and the data for each pixel. I suspect if you choose the header correctly you could make each pixel a byte which should simply things.

And yes that opens the file correctly.

Or to keep the file size small you could just have 1 bit per pixel.

Anyway here is a good place to start
Jun 25 '10 #6
Ok, will do, thanks. I found some .c file while googling, but wasn't sure what to make of it. Obviously, it wasn't what I needed. The -bmp should be quite simple, as I only need two colors, and no greytones :) So filesize should be small (but it isn't really an issue, there will never be more than one output file :)

This can be closed/archived/whatever now.
Jun 25 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: cppaddict | last post by:
Hi, Is there way to read in a .gif image file as a two-dimensional array of pixel values? If not in the C++ standard library, does anyone know of a free library that supports this? Thanks,...
3
by: William Starr Moake | last post by:
Is there a script to convert image file paths from absolute to just img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The doImage execCommand produces an absolute file path, which...
7
by: Roger Withnell | last post by:
I would like to check that an image file, selected by a user using fileUpload, is within certain parameters (width, height, filesize) and to display the image file so that the user can see the...
11
by: Afifov | last post by:
Hello, I am trying to know how I can generate an image file in C. I want to draw a line on top of a picture (png or JPG), but I don't know where to start. Any help?
2
by: Duy Nguyen | last post by:
how can I save an image file to database?
5
by: albo | last post by:
hi all !! i need help in opening an image file (bmp) and printing it to the screen. thanks
2
by: Peter Oliphant | last post by:
The Image class allows loading a bitmap from a graphic file. So far I've gotten it to work with JPG and BMP files. What other graphic file formats are supported in this way? Is this fixed based...
1
by: nitheshsalian | last post by:
can anyone give me the code snippet to open a image file using c/c++ in windows platform??? i want to store a image file in an array and later do some manipulations on it... i want to use the...
6
by: mfaisalwarraich | last post by:
hi every body, i am using the following code to upload an image. it directly jumps to the error message please tell me what im doing wrong here. <form name="myform"...
1
by: nikhilkajrekar | last post by:
I have byte array i want to save that to image file how i can do that
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.