I would like to write a program that can ask for an x,y coordinates and hence draw circle on a imported bitmap.
It can draw circles on the imported bitmap and show the image after each edit.
I have done most part about how to read and allocate memories of bitmap,
but I got difficulties with how to draw circles and how to save the result image as output.bmp
I wish I can have help with void SaveFile and void DrawCircle.
Thank you for any help.
Code:
#pragma pack(push, 1)
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef int32_t int32;
typedef int16_t int16;
typedef uint8_t byte;
typedef struct {
byte signatureB;
byte signatureM;
int32 fileSize;
int16 reserved1;
int16 reserved2;
int32 imageOffset;
} BMPHeader;
typedef struct {
int32 size;
int32 imageWidth;
int32 imageHeight;
int16 colorPlanes;
int16 bitsPerPixel;
byte extra[24];
} BMPInfoHeader;
typedef struct {
byte blue;
byte green;
byte red;
} Pixel;
BMPHeader bmpHeader;
BMPInfoHeader bmpInfoHeader;
Pixel* LoadBitmap(char* bmpFilename) {
FILE *filePtr;
Pixel *bitmapImage = NULL;
filePtr = fopen(bmpFilename, "rb");
if (filePtr == NULL) {
printf("Input bitmap not found...it should be in your project directory or same directory as your exe if stand-alone\n");
return NULL;
}
fread(&bmpHeader, sizeof(BMPHeader), 1, filePtr);
if (bmpHeader.signatureB != 'B' || bmpHeader.signatureM != 'M') {
printf("Input file not a BMP... have you saved it properly as a bitmap file?\n");
fclose(filePtr);
return NULL;
}
printf("Reported file size: %d\n", bmpHeader.fileSize);
printf("Offset for image data: %d\n", bmpHeader.imageOffset);
fread(&bmpInfoHeader, sizeof(BMPInfoHeader), 1, filePtr);
if (bmpInfoHeader.bitsPerPixel != 24) {
printf("Input file not 24-bit BMP... Plese save as 24 bit BMP only\n");
fclose(filePtr);
return NULL;
}
printf("Reported image width: %d\n", bmpInfoHeader.imageWidth);
printf("Reported image height: %d\n", bmpInfoHeader.imageHeight);
printf("Number of bits per pixel: %d\n", bmpInfoHeader.bitsPerPixel);
fseek(filePtr, bmpHeader.imageOffset, SEEK_SET);
int imageSize = bmpInfoHeader.imageWidth*bmpInfoHeader.imageHeight*3;
printf("Image size: %d\n", imageSize);
bitmapImage = (Pixel*)malloc(imageSize);
fread(bitmapImage, sizeof(Pixel), bmpInfoHeader.imageWidth*bmpInfoHeader.imageHeight, filePtr);
fclose(filePtr);
return bitmapImage;
}
void SaveFile(char* filename) {
}
void DrawCircle(){
}
void ShowImage(Pixel* image) {
int i, j;
for (i = bmpInfoHeader.imageHeight-1; i >= 0; i--) {
for (j = 0; j < bmpInfoHeader.imageWidth; j++) {
Pixel p = image[i*bmpInfoHeader.imageWidth + j];
double mono = (0.2125 * p.red + 0.7154 * p.green + 0.0721 * p.blue)/255; // convert to monochrome
if (mono < 0.2) {
printf("%c", ' ');
} else if (mono < 0.4) {
printf("%c", 176);
} else if (mono < 0.6) {
printf("%c", 177);
} else {
printf("%c", 178);
}
}
printf("\n");
}
}
int main(void) {
system("chcp 437");
Pixel* image = LoadBitmap("input.bmp");
printf("\nTop-Left Pixel - Blue: %d Green: %d Red: %d\n", image[0].blue, image[0].green, image[0].red); // example
printf("Pixel next to Top-Left Pixel - Blue: %d Green: %d Red: %d\n\n", image[1].blue, image[1].green, image[1].red); // example
printf("Please enter the coordinates of the circle center x,y \n Entering 0 for both x and y to quit and save the output");
int x, y;
scanf("%d%d", &x, &y);
while (x != 0 || y != 0){
char ch = getchar();
DrawCircle();
ShowImage(image);
printf("Please enter the coordinates of the circle center x,y \n Entering 0 for both x and y to quit and save the output");
scanf("%d%d", &x, &y);
}
SaveFile("output.bmp");
}