Question: In this assignment, you are asked to manipulate image from a 24-bit uncompressed bmp file. (The format of bmp files is given in http://en.wikipedia.org/wiki/BMP_file_format). To
In this assignment, you are asked to manipulate image from a 24-bit uncompressed bmp file. (The format of bmp files is given in http://en.wikipedia.org/wiki/BMP_file_format). To help with reading and writing the image file, you are given library functions and an example that deal with the bmp file (in a zip file on my faculty website). In the example, the image is flipped horizontally. You are asked to implement the verticalflip(), enlarge() and rotate() functions: the first flips the image vertically, the second is used to enlarge the image by an integer scale factor; the last one is used to rotate the image either clockwise or counter-clockwise by a certain degree (which must be a multiple of 90) depending whether the rotation degree is positive or negative. Positive is clockwise and negative is counterclockwise. The program should take the follow command-line options: %./bmptool [-s scale | -r degree | -f | -v ] [-o output_file] [input_file] Where -s means to scale the image by the given 'scale' (a positive integer: 1, 2, 3, ...), -r means to rotate the image by 'degree' (which must be a multiple of 90, clockwise if positive, counter-clockwise if negative), -v means to flip the image vertically and -f means to flip the image horizontally. You can assume for each type (-s, -r, -f, or -v), the command line has at most one option. That is, the user is not supposed to do something like "./bmptool -s 2 -s 4". If that happens, you can either prompt with an error, or take either 2 or 4 as the scale. However, the user may present a combination, say, './bmptool -r 90 -s 4'. If multiple option types are present, the order for processing the image is that you do scale first, then rotate, and then flip vertically and finally flip horizontally. You are required to use getopt() to process the command-line. If '-o output_file' is missing, use standard output. If 'input_file' is missing, use standard input. The options to use standard input or standard output will only be used when chaining commands. Make sure the program returns 0 on success. In that case, one can 'chain' the commands using pipes, like: % bmptool -s 4 1.bmp | bmptool -r -90 | bmptool -f -o 2.bmp Your program needs to provide necessary sanity-check for command line arguments and handle various error conditions and prompt the user with helpful information. You need to use getopt() to process the command line arguments. Test your program with various combinations to make sure it works as expected. You must use dynamic memory to store the content of the new image before writing out to file. You need to reclaim memory afterwards to prevent from memory leaks.
FIles provided:
main.c:
#include
#include "bmplib.h"
#include
#include
int enlarge(PIXEL* original, int rows, int cols, int scale,
PIXEL** new, int* newrows, int* newcols)
{
/* THIS IS THE METHOD THAT YOU SHOULD WRITE */
return 0;
}
int rotate(PIXEL* original, int rows, int cols, int rotation,
PIXEL** new, int* newrows, int* newcols)
{
/* THIS IS THE METHOD THAT YOU SHOULD WRITE */
return 0;
}
int verticalflip (PIXEL *original, PIXEL **new, int rows, int cols)
{
/* THIS IS THE METHOD THAT YOU SHOULD WRITE */
return 0;
}
int flip (PIXEL *original, PIXEL **new, int rows, int cols)
{
int row, col;
if ((rows <= 0) || (cols <= 0)) return -1;
*new = (PIXEL*)malloc(rows*cols*sizeof(PIXEL));
for (row=0; row < rows; row++)
for (col=0; col < cols; col++) {
PIXEL* o = original + row*cols + col;
PIXEL* n = (*new) + row*cols + (cols-1-col);
*n = *o;
}
return 0;
}
int main()
{
int r, c;
PIXEL *b, *nb;
readFile("example.bmp", &r, &c, &b);
flip(b, &nb, r, c);
writeFile("result.bmp", r, c, nb);
free(b);
free(nb);
return 0;
}
bmplib.c:
#include
#include
#include
#include
#include
#include
#include "bmplib.h"
static int myread(int fd, char* buf, unsigned int size)
{
int r = 0;
while(r < size) {
int x = read(fd, &buf[r], size-r);
if(x < 0) return x;
else r += x;
}
return size;
}
static int mywrite(int fd, char* buf, unsigned int size)
{
int w = 0;
while(w < size) {
int x = write(fd, &buf[w], size-w);
if(x < 0) return x;
else w += x;
}
return size;
}
int readFile (char *filename, int *rows, int *cols, PIXEL** bitmap)
{
int fd, ret;
unsigned int start;
if(filename) {
if((fd = open(filename, O_RDONLY)) < 0) {
perror("Can't open bmp file to read");
return -1;
}
} else fd = STDIN_FILENO;
ret = readHeader (fd, rows, cols, &start);
if(ret) return ret;
*bitmap = (PIXEL*)malloc(sizeof(PIXEL)*(*rows)*(*cols));
ret = readBits (fd, *bitmap, *rows, *cols, start);
if(ret) return ret;
if(filename) close(fd);
return 0;
}
int writeFile (char* filename, int rows, int cols, PIXEL* bitmap)
{
int fd, ret;
unsigned int start = DEFAULT_BITMAP_OFFSET;
if(filename) {
if((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666)) < 0) {
perror("Can't open bmp file to write");
return -1;
}
} else fd = STDOUT_FILENO;
ret = writeHeader (fd, rows, cols, start);
if(ret) return ret;
ret = writeBits(fd, rows, cols, bitmap, start);
if(ret) return ret;
if(filename) close(fd);
return 0;
}
int readHeader(int fd, int *rows, int *cols, unsigned int *start)
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
if(myread(fd, ((char*)&bmfh)+2, sizeof(bmfh)-2) <= 0) {
perror("Can't read BITMAPFILEHEADER");
return -2;
}
if(myread(fd, (char*)&bmih, sizeof(bmih)) <= 0) {
perror("Can't read BITMAPINFOHEADER");
return -3;
}
if(bmih.biCompression != 0) {
fprintf(stderr, "Can't read compressed bmp");
return -4;
}
if(bmih.biBitCount != 24) {
fprintf(stderr, "Can't handle bmp other than 24-bit");
return -5;
}
*rows = bmih.biHeight;
*cols = bmih.biWidth;
*start = bmfh.bfOffBits;
return 0;
}
int writeHeader(int fd, int rows, int cols, unsigned int start)
{
unsigned int fileSize;
unsigned int headerSize;
unsigned int paddedCols;
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
memset (&bmfh, 0, sizeof(bmfh));
memset (&bmih, 0, sizeof(bmih));
paddedCols = ((cols/4)*4 !=cols ? ((cols+4)/4)*4 : cols);
headerSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
fileSize = rows*paddedCols*sizeof(PIXEL)+headerSize;
bmfh.bfType = 0x4D42;
bmfh.bfSize = fileSize;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfOffBits = start;
bmih.biSize = 40;
bmih.biWidth = cols;
bmih.biHeight = rows;
bmih.biPlanes = 1;
bmih.biBitCount = 24;
bmih.biCompression = 0;
bmih.biSizeImage = 0;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;
if(mywrite(fd, ((char*)&bmfh)+2, sizeof(bmfh)-2) < 0) {
perror("Can't write BITMAPFILEHEADER");
return -2;
}
if(mywrite(fd, (char*)&bmih, sizeof(bmih)) < 0) {
perror("Can't write BITMAPINFOHEADER");
return -3;
}
return 0;
}
int readBits(int fd, PIXEL* bitmap, int rows, int cols, unsigned int start)
{
int row;
char padding[3];
int padAmount;
char useless[DEFAULT_BITMAP_OFFSET];
padAmount = ((cols * sizeof(PIXEL)) % 4) ? (4 - ((cols * sizeof(PIXEL)) % 4)) : 0;
start -= sizeof(BITMAPFILEHEADER)-2+sizeof(BITMAPINFOHEADER);
if(start > 0 && myread(fd, useless, start) < 0) {
perror("Can't lseek to bitmap");
return -6;
}
/*
if(lseek (fd, start, SEEK_SET) < 0) {
perror("Can't lseek to bitmap");
return -6;
}
*/
for (row=0; row < rows; row++) {
if(myread(fd, (char*)(bitmap+(row*cols)), cols*sizeof(PIXEL)) < 0) {
perror("Can't read bitmap");
return -7;
}
if(padAmount > 0) {
if(myread(fd, padding, padAmount) < 0) {
perror("Can't read bitmap");
return -8;
}
}
}
return 0;
}
int writeBits(int fd, int rows, int cols, PIXEL* bitmap, unsigned int start)
{
int row;
char padding[3];
int padAmount;
char useless[DEFAULT_BITMAP_OFFSET];
padAmount = ((cols * sizeof(PIXEL)) % 4) ? (4 - ((cols * sizeof(PIXEL)) % 4)) : 0;
memset(padding, 0, 3);
start -= sizeof(BITMAPFILEHEADER)-2+sizeof(BITMAPINFOHEADER);
if(start > 0) {
memset(useless, 0, start);
if(mywrite(fd, useless, start) < 0) {
perror("Can't lseek to bitmap");
return -6;
}
}
/*
if(lseek (fd, start, SEEK_SET) < 0) {
perror("Can't lseek to bitmap");
return -6;
}
*/
for (row=0; row < rows; row++) {
if(mywrite(fd, (char*)(bitmap+(row*cols)), cols*sizeof(PIXEL)) < 0) {
perror("Can't write bitmap");
return -7;
}
if(padAmount > 0) {
if(mywrite(fd, padding, padAmount) < 0) {
perror("Can't write bitmap");
return -8;
}
}
}
return 0;
}
bmplib.h:
#ifndef BMPLIB_H #define BMPLIB_H
typedef struct { unsigned short padding; unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned int bfOffBits; } BITMAPFILEHEADER;
typedef struct { unsigned int biSize; int biWidth; int biHeight; unsigned short biPlanes; unsigned short biBitCount; unsigned int biCompression; unsigned int biSizeImage; unsigned int biXPelsPerMeter; unsigned int biYPelsPerMeter; unsigned int biClrUsed; unsigned int biClrImportant; } BITMAPINFOHEADER;
typedef struct { unsigned char r; unsigned char g; unsigned char b; } PIXEL;
#define DEFAULT_BITMAP_OFFSET 1078
/* Read an uncompressed 24-bit bmp from a file named 'filename' (if null, it's standard input); return the number of 'rows', the number of 'cols', and the 'bitmap' as an array of PIXELs. The function return 0 if successful. */ int readFile (char* filename, int* rows, int* cols, PIXEL** bitmap);
/* Write an uncompressed 24-bit bmp to a file named 'filename' (if null, it's standard output); the dimension of the bmp is the number of 'rows' by the number of 'cols', and the 'bitmap' contains an array of PIXELs. The function return 0 if successful. */ int writeFile (char* filename, int rows, int cols, PIXEL* bitmap);
/* Read bmp file header from file 'fd', return the number of 'rows', the number of 'cols', and the 'start' position of the bitmap. The function returns 0 if successful. */ int readHeader(int fd, int *rows, int *cols, unsigned int *start);
/* Write bmp file header to file 'fd'; the dimention of the bitmap is the number of 'rows' by the number of 'cols', and it starts at the 'start' position. The function returns 0 if successful. */ int writeHeader(int fd, int rows, int cols, unsigned int start);
/* Read the 'bitmap' from file 'fd'; the dimention of the bitmap is the number of 'rows' by the number of 'cols', and it starts at the 'start' position. The function returns 0 if successful. */ int readBits(int fd, PIXEL* bitmap, int rows, int cols, unsigned int start);
/* Write the 'bitmap' to file 'fd'; the dimention of the bitmap is the number of 'rows' by the number of 'cols', and it starts at the 'start' position. The function returns 0 if successful. */ int writeBits(int fd, int rows, int cols, PIXEL* bitmap, unsigned int start);
#endif /*BMPLIB_H*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
