Create a pseudo-random color BMP file

Assignment Help Other Subject
Reference no: EM132230074

Program Assignment - The BMP (bitmap) Image-file Format

Read this whole section before starting, especially the bottom part of this assignment.

The BMP (bitmap) format is a standard image-file format developed by Microsoft and used by Windows Operating systems and others. This document describes how to construct a 24-bit BMP file. A 24-bit BMP file uses 24 bits to denote the color of each pixel in an image: an 8-bit value (0-255) for the blue component, an 8-bit value (0-255) for the green component, and an 8-bit value (0-255) for the red component of a pixel. Using BGR to denote the blue, green, and red components of a pixel, if the pixel is bright blue, it would have (B,G,R)=(255,0,0). If a pixel is bright green it would have (B,G,R)=(0,255,0). If the pixel is bright red, it would have (B,G,R)=(0,0,255).

Black is formed by the absence of color; so if a pixel is black it would have (B,G,R)=(0,0,0). White is formed by the superposition of the brightest blue, green, and red; so a white pixel would have (B,G,R)=(255,255,255).

Your 24-bit BMP image file will have the following 3 parts.

1) A bitmap file header (14 bytes) is the 1st 14 bytes in a BMP image-file. The bitmap-file header, called a BITMAPFILEHEADER, contains information about the type, size, and layout of the bitmap file. (More info below)

2) A bitmap information header (40 bytes) follows the BITMAPFILEHEADER in the *.bmp image file. The bitmap-information header specifies the dimensions, compression type, and color format for the bitmap file. (More info below)

3) Image-data, a sequence of bytes that define the BGR value at each pixel in the image. The image-data stores the BGR values of each pixel in order: beginning at the bottom row of the image, going across the row, then the 1st pixel of the next higher row of the image, etc.

The bytes in an image file are each unsigned values 0-255. We can use data type "unsigned char" to hold a single byte containing values 0-255.

unsigned char uc;

To assign a value, e.g. 23, to an unsigned char uc; you can simply use uc=23. You will be writing bytes (unsigned char values) into a file one byte at a time using a "%c" format. You can use e.g. fprintf(fin,"%c",&uc); to write a single byte to a file.

Now let's examine the first 14 bytes in your BMP file, i.e. the BITMAPFILEHEADER.

1) A bitmap file header (14 bytes) is the 1st 14 bytes in a BMP image-file. The bitmap-file header, called a BITMAPFILEHEADER, contains information about the type, size, and layout of the bitmap file.

The 1st two bytes in the file must be the values 66 and 77 to identify the file as being of type BMP. The next 4 bytes must correctly identify the size of the BMP file in bytes as an unsigned integer, least significant byte LSB 1st. The next 4 bytes must be zeros. The final 4 bytes of the BITMAPFILEHEADER should hold the number of bytes to skip over to reach the image data; i.e. 54 bytes. This value is again stored as an unsigned integer, least significant byte LSB 1st.

Now, let's examine the next 40 bytes in your BMP file, i.e. the BITMAPINFOHEADER.

2) A bitmap information header (40 bytes) follows the BITMAPFILEHEADER in the *.bmp image file. The bitmap-information header specifies the dimensions, compression type, and color format for the bitmap file. For this example, let's assume the size of the image is width=height=256.

To generate the image-data, you would store the blue-green-red value of each pixel as 3 consecutive bytes. Here, the image data will occupy 3*width*height bytes. If, for example, the width=height=256, then the image data will occupy 3*256*256=196,608 bytes.

The decimal value 196,608 can be represented as a 4-byte integer value as the 4 bytes (least-significant-byte 1st) 0, 0, 3, 0 which denotes the decimal value 0+0*256+3*256*256+0*256*256*256=196,608.

The total number of bytes in the BMP file will be 14+40+3*256*256=196,662.

The decimal value 196,662 can represented as a 4-byte integer value as the 4 bytes (least significant byte 1st) 54, 0, 3, 0 which denotes the decimal value 54+0*256+3*256*256+0*256*256*256=196,662. The first 14 bytes of your BMP file would therefore be 66, 77, 54, 0, 3, 0, 0, 0, 0, 0, 54, 0, 0, 0.

Of course, a different size image would have correspondingly different values for the header.

So for this example, the next 40 bytes in your BMP file would therefore be

40, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.

Now, let's examine the image data.

3) Image-data: a sequence of bytes that define the BGR value at each pixel in the image. The image-data will store the BGR values of each pixel in order: beginning at the bottom row of the image, going across the row, then the 1st pixel of the next higher row, etc.

Now for the specifications of your program.

Your program will create a pseudo-random color BMP (bitmap) file called "random.bmp" in the directory from which your program is executed.

1) The width and height of the image will be chosen by the program user. However, your program will "round-up" the width so that the width is an integer multiple of 4. For example, if the user enters 2 for the width, your program will round the width to 4. If the user enters 5 for the width, your program will round-up the width to 8. If the user enters 12 for the width, your program will leave the width at 12. Notify the user regarding the resulting size of image that your program will create.

2) Once the size of the image is established, open the output file, and fprintf( ) the appropriate 14-byte BITMAPFILEHEADER and 40-byte BITMAPINFOHEADER to the file.

Use nested for-loops to fprintf( ) the set of pseudo-random BGR image-data values into the file.

3) Use the rand() function to generate the random BGR values. The pseudo-random values from rand( ) will be within the range 0-RAND_MAX. Convert these values to the byte value range 0-255 to serve as BGR 1-byte unsigned char values 0-255.

Before the 1st rand( ) call occurs in your program, seed the rand( ) function (one and only one time) using the srand( ) function.

Set the argument of srand( ) to the time-of-day obtained from your computer using the following syntax.

The statement is srand( (unsigned int)time(NULL) );

You will need to include header file time.h at the top of your program.

#include<time.h>

in order to use the function call time(NULL).

4) Finally, fclose( ) the file.

5) So, now you understand a bit about the Bitmap image format, but there is one problem. It has to do with writing characters into a file.

Note - Answer question 1-5.

Attachment:- Assignment File.rar

Reference no: EM132230074

Questions Cloud

Why the perspective may be more effective : Suggest another perspective that might be used to assess this person, and provide a rationale as to why the perspective may be more effective.
National intelligence program-military intelligence program : The overall budget for Intelligence operations is not classified and is released by ODNI. Between National Intelligence Program-Military Intelligence Program,
Relates to human resource management challenges : You learned about understanding employee behavior as it relates to human resource management challenges.
How the given practices impact assessment : In this Assignment, you will consider the ACA's Code of Ethics and how these practices impact assessment. This professional code identifies.
Create a pseudo-random color BMP file : Your program will create a pseudo-random color BMP (bitmap) file called "random.bmp" in the directory from which your program is executed
Role of transportation in the overall global supply chain : Understand the role of transportation in the overall global supply chain. The major principles of transportation economics.
Components needed to form market in fast food industry : What are some of the components needed to form a market in the fast food industry?
How do you interpret the concept : We see that society and culture influence the words that we speak, and the words that we speak influence society and culture.
Repeated transactional price negotiations : Repeated transactional price negotiations continue to render supplier B as the highest cost provider.

Reviews

len2230074

2/8/2019 1:14:53 AM

Instructions: answer question 1-5. IMPORTANT: Recall, if you fprintf() the ascii character code (10) for newline '\n' into a file, fprintf(fp,"%c",10); the operating system will put 2-bytes into the file: carriage return (asci code 13) then linefeed (10). This is a problem, it is quite probable that you will generate a pseudo-random BGR value =10 at some point in your program. Whenever this happens, reset the pseudo-random BGR value to 11 before you fprintf( ) it to the file.

len2230074

2/8/2019 1:14:47 AM

If your program is written correctly, after you run your program, you can use your mouse to double-click on your output file random.bmp and the default application for displaying images on your computer will display a red image exactly the same as the red image above. If your program is written correctly, after you run your program, you can use your mouse to double-click on your output file and the default application for displaying images on your computer will display a pseudo-random color image.

Write a Review

Other Subject Questions & Answers

  Cross-cultural opportunities and conflicts in canada

Short Paper on Cross-cultural Opportunities and Conflicts in Canada.

  Sociology theory questions

Sociology are very fundamental in nature. Role strain and role constraint speak about the duties and responsibilities of the roles of people in society or in a group. A short theory about Darwin and Moths is also answered.

  A book review on unfaithful angels

This review will help the reader understand the social work profession through different concepts giving the glimpse of why the social work profession might have drifted away from its original purpose of serving the poor.

  Disorder paper: schizophrenia

Schizophrenia does not really have just one single cause. It is a possibility that this disorder could be inherited but not all doctors are sure.

  Individual assignment: two models handout and rubric

Individual Assignment : Two Models Handout and Rubric,    This paper will allow you to understand and evaluate two vastly different organizational models and to effectively communicate their differences.

  Developing strategic intent for toyota

The following report includes the description about the organization, its strategies, industry analysis in which it operates and its position in the industry.

  Gasoline powered passenger vehicles

In this study, we examine how gasoline price volatility and income of the consumers impacts consumer's demand for gasoline.

  An aspect of poverty in canada

Economics thesis undergrad 4th year paper to write. it should be about 22 pages in length, literature review, economic analysis and then data or cost benefit analysis.

  Ngn customer satisfaction qos indicator for 3g services

The paper aims to highlight the global trends in countries and regions where 3G has already been introduced and propose an implementation plan to the telecom operators of developing countries.

  Prepare a power point presentation

Prepare the power point presentation for the case: Santa Fe Independent School District

  Information literacy is important in this environment

Information literacy is critically important in this contemporary environment

  Associative property of multiplication

Write a definition for associative property of multiplication.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd