ex0: style format
This commit is contained in:
3
.clang-format
Normal file
3
.clang-format
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
BasedOnStyle: LLVM
|
||||||
|
ColumnLimit: 0
|
||||||
|
Cpp11BracedListStyle: false
|
@@ -1,63 +1,60 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "bitmap.h"
|
#include "bitmap.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// save 24-bits bmp file, buffer must be in bmp format: upside-down
|
// save 24-bits bmp file, buffer must be in bmp format: upside-down
|
||||||
void savebmp(char *name, uchar *buffer, int x, int y) {
|
void savebmp(char *name, uchar *buffer, int x, int y) {
|
||||||
FILE *f=fopen(name,"wb");
|
FILE *f = fopen(name, "wb");
|
||||||
if(!f) {
|
if (!f) {
|
||||||
printf("Error writing image to disk.\n");
|
printf("Error writing image to disk.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
unsigned int size=x*y*3+54;
|
unsigned int size = x * y * 3 + 54;
|
||||||
uchar header[54]={'B','M',size&255,(size>>8)&255,(size>>16)&255,size>>24,0,
|
uchar header[54] = {
|
||||||
0,0,0,54,0,0,0,40,0,0,0,x&255,x>>8,0,0,y&255,y>>8,0,0,1,0,24,0,0,0,0,0,0,
|
'B', 'M', size & 255, (size >> 8) & 255, (size >> 16) & 255, size >> 24, //
|
||||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
|
0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, x & 255, x >> 8, 0, 0, y & 255, //
|
||||||
fwrite(header,1,54,f);
|
y >> 8, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //
|
||||||
fwrite(buffer,1,x*y*3,f);
|
0, 0, 0, 0, 0, 0, 0, 0, //
|
||||||
fclose(f);
|
};
|
||||||
|
fwrite(header, 1, 54, f);
|
||||||
|
fwrite(buffer, 1, x * y * 3, f);
|
||||||
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// read bmp file and store image in contiguous array
|
// read bmp file and store image in contiguous array
|
||||||
void readbmp(char* filename, uchar* array) {
|
void readbmp(char *filename, uchar *array) {
|
||||||
FILE* img = fopen(filename, "rb"); //read the file
|
FILE *img = fopen(filename, "rb"); // read the file
|
||||||
uchar header[54];
|
uchar header[54];
|
||||||
size_t result = fread(header, sizeof(uchar), 54, img); // read the 54-byte header
|
size_t result =
|
||||||
|
fread(header, sizeof(uchar), 54, img); // read the 54-byte header
|
||||||
|
|
||||||
if (result != 54)
|
if (result != 54) {
|
||||||
{
|
perror("Error reading file.\n");
|
||||||
perror("Error reading file.\n");
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// extract image height and width from header
|
// extract image height and width from header
|
||||||
int width = *(int*)&header[18];
|
int width = *(int *)&header[18];
|
||||||
int height = *(int*)&header[22];
|
int height = *(int *)&header[22];
|
||||||
int padding=0;
|
int padding = 0;
|
||||||
while ((width*3+padding) % 4!=0) padding++;
|
while ((width * 3 + padding) % 4 != 0)
|
||||||
|
padding++;
|
||||||
|
|
||||||
int widthnew=width*3+padding;
|
int widthnew = width * 3 + padding;
|
||||||
uchar* data = calloc(widthnew, sizeof(uchar));
|
uchar *data = calloc(widthnew, sizeof(uchar));
|
||||||
|
|
||||||
for (int i=0; i<height; i++ ) {
|
for (int i = 0; i < height; i++) {
|
||||||
result = fread( data, sizeof(uchar), widthnew, img);
|
result = fread(data, sizeof(uchar), widthnew, img);
|
||||||
|
|
||||||
if (result != (size_t) widthnew)
|
if (result != (size_t)widthnew) {
|
||||||
{
|
perror("Error reading file.\n");
|
||||||
perror("Error reading file.\n");
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (int j=0; j<width*3; j+=3) {
|
for (int j = 0; j < width * 3; j += 3) {
|
||||||
array[3 * i * width + j + 0] = data[j+0];
|
array[3 * i * width + j + 0] = data[j + 0];
|
||||||
array[3 * i * width + j + 1] = data[j+1];
|
array[3 * i * width + j + 1] = data[j + 1];
|
||||||
array[3 * i * width + j + 2] = data[j+2];
|
array[3 * i * width + j + 2] = data[j + 2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(img); //close the file
|
fclose(img); // close the file
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
#ifndef BITMAP_H
|
#ifndef BITMAP_H
|
||||||
#define BITMAP_H
|
#define BITMAP_H
|
||||||
|
|
||||||
|
|
||||||
typedef unsigned char uchar;
|
typedef unsigned char uchar;
|
||||||
|
|
||||||
void savebmp(char *name, uchar *buffer, int x, int y);
|
void savebmp(char *name, uchar *buffer, int x, int y);
|
||||||
|
@@ -1,22 +1,19 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include "bitmap.h"
|
#include "bitmap.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define XSIZE 2560 // Size of before image
|
#define XSIZE 2560 // Size of before image
|
||||||
#define YSIZE 2048
|
#define YSIZE 2048
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
uchar *image = calloc(XSIZE * YSIZE * 3, 1); // Three uchars per pixel (RGB)
|
||||||
|
readbmp("before.bmp", image);
|
||||||
|
|
||||||
|
// Alter the image here
|
||||||
|
;
|
||||||
|
|
||||||
int main(void)
|
savebmp("after.bmp", image, XSIZE, YSIZE);
|
||||||
{
|
|
||||||
uchar *image = calloc(XSIZE * YSIZE * 3, 1); // Three uchars per pixel (RGB)
|
|
||||||
readbmp("before.bmp", image);
|
|
||||||
|
|
||||||
// Alter the image here
|
free(image);
|
||||||
;
|
return 0;
|
||||||
|
|
||||||
savebmp("after.bmp", image, XSIZE, YSIZE);
|
|
||||||
|
|
||||||
free(image);
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user