I need help with this C language exercise... Objectives : Solve a programming pr
ID: 3772250 • Letter: I
Question
I need help with this C language exercise...
Objectives:
Solve a programming problem by partitioning functions and incrementally developing solutions
Integrate concepts such as: structures, file I/O, pixel graphics, loops, user interface
Learn about graphics on the PC and graphics file format.
Develop and implement a project plan and a test plan for a software system.
The exercise consist of a console program to process BMP (Bitmap) images. It will prompt the user for the source file name and operation desired, read the file and report statistics, prompt for an output file name, and write the modified image to the output file.
The imaging operations the software should accomplish:
N: negative – produce the equivalent of a photo film negative of the image. Of course, the negative of a negative should produce the original image. To calculate the negative of an RGB 24 pixel, subtract each of its color bytes from 255 (the maximum value that an unsigned byte may contain).
B: blueshift – maximize the blue component of the RGB of every pixel
G: greenshift – maximize the green component of the RGB of every pixel
R: redshift – maximize the red component of the RGB of every pixel
I: intensity – scale all the colors of every pixel by a percentage factor between 0 and 1000%. Stop at maximum intensity (255 decimal for each color)
e: encrypt(decrypt), trivial algorithm – xor each pixel with a user-entered key number, which you increment every pixel (or every n pixels). The re-encrypt of the encrypted image should produce the original image, since the xor operation is reversible.
E: Encrypt(dEcrypt) randomized – xor each pixel with a random key, generated for each pixel by rand() or equivalent. Of course, the encrypt of the encrypted image should produce the original image (assuming rand() was NOT seeded with a unique number, or that unique number is used in both the encryption and decryption. Or if you prefer to come up with your own encryption algorithm, you may.
DO NOT USE CANNED GRAPHICS LIBRARIES.
Background on Windows Bitmaps
Bitmaps may be stored in a large variety of formats, with varying bits per pixel from 8 to 32. They may be compressed or not (such as RLE BMP files, .JPG, .PNG, .GIF, etc…), and may include a color table (palette) that converts 8-bit pixels to 24-bit color. To simplify this exercise, we will operate only on 24-bit RGB true color .BMP files. 24-bt RGB consists of one unsigned byte each for Red, Green, and Blue color. A value of 255 in the Red byte makes a very bright red pixel. A value of zero means no hint of that color component. Black is 0,0,0, and white is 255, 255, 255. Grays have equal color intensities (R==G==B) of less than 255.
Such binary files start out with a file header structure of a few bytes telling the file size and type, followed by a bitmap header telling features of the image (bits/pixel, height, width, compression, where in the file the first pixel starts, etc…). The format of the pixels and files is defined in the WinGDI.h (Windows Graphics Device Interface) header which Microsoft provides. We have extracted the few important lines of that file to a shorter “RGB.h” file to include in your project.
It is confusing to remember whether the Red or Blue byte is the lowest 8 bits of a pixel (called RGBTriple in Windows, or pix in our RGB.h header). But by using C structs, you don’t need to – the compiler takes care of that. If you have a pix pointer p, then the red component can be identified as p->r. For example, the following code sets the red component of the pixel pointed to by p to maximum:
pix* p = &myPixel;
p->r = 255;
If not using a pointer, the syntax would be:
myPixel.r = 255;
Study the RGB.h file to understand the pixel and file structure, and the names to use.
Bitmaps are automatically displayed by Microsoft image viewer or Office Picture Manager when you click on the file name. MS Paint is a convenient way to create, edit, and examine properties of bitmaps. If you use paint, make sure to save the BMP as a 24-bit true color RGB bitmap.
Implementation Recommendations
You will combine code already developed in lab exercise for several parts of the project, such as file I/O, structure access, dynamic memory allocation, bit fiddling, and error checking.
Successive Refinement
Create a quick and dirty demo of the code within the first week, without all the functionality. Every time you change the code, make a backup copy with the date in the name, before changing it, so that you can revert to working code if your change proves detrimental.
Recommended steps in the successive refinement of your program (IE, get your program to do just the steps 1 thru whatever, on several test files):
1. Open a BMP file whose name the user specifies, report file size and BMP bits per pixel.
2. Allocate memory via malloc(), read the file into the memory, write it back to another file whose name also is user-specified.
3. Append the .BMP to the filename(s) if the user forgot to.
4. Prompt the user for desired processing operation, read it into a string.
5. Negative operation.
6. B, G, R color shift operations.
7. Intensity operations.
8. Encrypt/decrypt.
Explanation / Answer
#include <stdio.h>
#include <string.h>
typedef struct {
char type[2];
unsigned int size;
unsigned short int reserved1,
reserved2;
unsigned int offset;
} HEADER;
typedef struct {
unsigned int size;
int width,height;
unsigned short int planes;
unsigned short int bits;
unsigned int compression;
unsigned int imagesize;
int xresolution,yresolution;
unsigned int ncolours;
unsigned int importantcolours;
} INFOHEADER;
int main()
{
char type[2]; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1,
reserved2;
unsigned int offset; /* Offset to image data, bytes */
FILE *f = fopen("C:\tmp\download.bmp", "rb");
HEADER header;
INFOHEADER infoheader;
memset(&header, 0, sizeof(header));
memset(&infoheader, 0, sizeof(infoheader));
fread(header.type, sizeof(type), 1, f);
fread(&header.size, sizeof(int), 1, f);
fread(&header.reserved1, sizeof(short), 1, f);
fread(&header.reserved2, sizeof(short), 1, f);
fread(&header.offset, sizeof(int), 1, f);
fprintf(stdout," header" " type: %c%c" " size: %u" " offset: %u ", header.type[0], header.type[1], header.size, header.offset);
fread(&infoheader, sizeof(infoheader), 1, f);
typedef struct {
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} INFOHEADER;
fprintf(stdout, " size: %u" " width: %d" " height: %d" " planes: %hu" " bits: %hu" " compression: %u" " imagesize: %u" "
xresolution: %d" " yresolution: %d" " ncolours: %u" " importantcolours: %u", infoheader.size, infoheader.width, infoheader.height,
infoheader.planes, infoheader.bits, infoheader.compression, infoheader.imagesize, infoheader.xresolution, infoheader.yresolution,
infoheader.ncolours, infoheader.importantcolours); fclose(f);
return 0;
}