Skip to content

Instantly share code, notes, and snippets.

@pinetum
Last active July 12, 2020 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinetum/686aece5a78718e38591c039c823091b to your computer and use it in GitHub Desktop.
Save pinetum/686aece5a78718e38591c039c823091b to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
typedef struct _lbheader{
unsigned short identifier; // 0x0000
unsigned int filesize; // 0x0002
unsigned int reserved; // 0x0006
unsigned int bitmap_dataoffset; // 0x000A
unsigned int bitmap_headersize; // 0x000E
unsigned int width; // 0x0012
unsigned int height; // 0x0016
unsigned short planes; // 0x001A
unsigned short bits_perpixel; // 0x001C
unsigned int compression; // 0x001E
unsigned int bitmap_datasize; // 0x0022
unsigned int hresolution; // 0x0026
unsigned int vresolution; // 0x002A
unsigned int usedcolors; // 0x002E
unsigned int importantcolors; // 0x0032
unsigned int palette; // 0x0036
} __attribute__((packed,aligned(1))) lbheader;
void updateData(int pt_x, int pt_y, char* pt);
int n_weight;
int n_height;
int main(){
FILE* fp = fopen("512_color_lena.bmp", "rb");
FILE* fp_output = fopen("output.bmp", "wb");
lbheader bmp_head;
fread(&bmp_head, sizeof(lbheader), 1, fp);
n_weight = bmp_head.width;
n_height = bmp_head.height;
printf("%d * %d \n", n_weight, n_height);
char* pData = new char[n_weight*3*n_height];
fread(pData, sizeof(unsigned char), n_weight*3*n_height, fp);
for(int x = 0; x < bmp_head.width; x++){
for(int y = 0; y < bmp_head.height; y++){
updateData(x, y, pData);
}
}
fwrite(&bmp_head, sizeof(lbheader), 1, fp_output);
fwrite(pData, sizeof(unsigned char), bmp_head.width*3*bmp_head.height, fp_output);
delete [] pData;
fclose(fp);
fclose(fp_output);
return 0;
}
void updateData(int pt_x, int pt_y, char* pt){
int x = n_weight/2 - pt_x;
int y = pt_y - n_height/2;
double r = sqrt(pow(x, 2)+pow(y, 2));
int n_offset = ((n_height - pt_x)*n_weight + pt_y)*3;
if (r > 256){
*(pt+n_offset) = 255;
*(pt+n_offset+1) = 255;
*(pt+n_offset+2) = 255;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment