safer data types multiplication
This commit is contained in:
@@ -4,25 +4,24 @@
|
||||
|
||||
#define HEADER_SIZE 54
|
||||
|
||||
void write_str(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size, unsigned char *value) {
|
||||
int i;
|
||||
void write_str(unsigned char *buffer, unsigned int offset, unsigned int size,
|
||||
unsigned char *value) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = (unsigned char)value[i];
|
||||
}
|
||||
}
|
||||
|
||||
void write_num(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size, unsigned short value) {
|
||||
int i;
|
||||
void write_num(unsigned char *buffer, unsigned int offset, unsigned int size,
|
||||
unsigned int value) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = (unsigned char)((value >> (8 * i)) & 0xFFu);
|
||||
}
|
||||
}
|
||||
|
||||
void write_nul(unsigned char *buffer, unsigned short offset,
|
||||
unsigned short size) {
|
||||
int i;
|
||||
void write_nul(unsigned char *buffer, unsigned int offset, unsigned int size) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < size; i++) {
|
||||
buffer[offset + i] = 0;
|
||||
}
|
||||
@@ -31,10 +30,11 @@ void write_nul(unsigned char *buffer, unsigned short offset,
|
||||
unsigned char *bmp_header(unsigned short width, unsigned short height,
|
||||
unsigned char color_depth) {
|
||||
unsigned char *output = (unsigned char *)malloc(HEADER_SIZE);
|
||||
|
||||
unsigned int data_length = ((unsigned int)width) * ((unsigned int)height) *
|
||||
((unsigned int)color_depth);
|
||||
write_str(output, 0x00, 0x02, (unsigned char *)"BM"); // 0x00(2) BM
|
||||
write_num(output, 0x02, 0x04,
|
||||
HEADER_SIZE + width * height * color_depth); // 0x02(4) file size
|
||||
HEADER_SIZE + data_length); // 0x02(4) file size
|
||||
write_nul(output, 0x06, 0x04); // 0x06(4) application reserved
|
||||
write_num(output, 0x0A, 0x04, HEADER_SIZE); // 0x0A(4) data offset
|
||||
write_num(output, 0x0E, 0x04, 40); // 0x0E(4) DIB header size
|
||||
@@ -44,7 +44,7 @@ unsigned char *bmp_header(unsigned short width, unsigned short height,
|
||||
write_num(output, 0x1C, 0x02, color_depth * 8); // 0x1C(2) bits per pixel
|
||||
write_nul(output, 0x1E, 0x04); // 0x1E(4) BI_RGB, no compression
|
||||
write_num(output, 0x22, 0x04,
|
||||
width * height * color_depth); // 0x22(4) size of raw bitmap data
|
||||
data_length); // 0x22(4) size of raw bitmap data
|
||||
write_num(output, 0x26, 0x04, 2835); // 0x26(4) horizontal print resolution
|
||||
write_num(output, 0x2A, 0x04, 2835); // 0x2A(4) vertical print resolution
|
||||
write_nul(output, 0x2E, 0x04); // 0x2E(4) color in palette
|
||||
@@ -53,18 +53,22 @@ unsigned char *bmp_header(unsigned short width, unsigned short height,
|
||||
return output;
|
||||
}
|
||||
|
||||
unsigned short bmp_data_line_length(unsigned short width,
|
||||
unsigned char color_depth) {
|
||||
unsigned short line_offset = (width * color_depth) % 4;
|
||||
unsigned int bmp_data_line_length(unsigned short width,
|
||||
unsigned char color_depth) {
|
||||
unsigned int data_length =
|
||||
((unsigned int)width) * ((unsigned int)color_depth);
|
||||
unsigned int line_offset = data_length % 4;
|
||||
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
return width * color_depth + line_padding;
|
||||
return data_length + line_padding;
|
||||
}
|
||||
|
||||
void bmp_data_line(unsigned char *buffer, unsigned short width,
|
||||
unsigned char color_depth, unsigned char *data) {
|
||||
unsigned short line_offset = (width * color_depth) % 4;
|
||||
unsigned int data_length =
|
||||
((unsigned int)width) * ((unsigned int)color_depth);
|
||||
unsigned int line_offset = data_length % 4;
|
||||
unsigned short line_padding = line_offset > 0 ? 4 - line_offset : 0;
|
||||
write_str(buffer, 0, width * color_depth, data);
|
||||
write_str(buffer, 0, data_length, data);
|
||||
if (line_padding > 0) {
|
||||
write_nul(buffer, width * color_depth, line_padding);
|
||||
}
|
||||
@@ -78,11 +82,13 @@ void bmp_generate(unsigned short width, unsigned short height,
|
||||
unsigned char *header = bmp_header(width, height, color_depth);
|
||||
fwrite(header, HEADER_SIZE, 1, fptr);
|
||||
free(header);
|
||||
unsigned int data_length =
|
||||
((unsigned int)width) * ((unsigned int)color_depth);
|
||||
unsigned short y;
|
||||
unsigned char *data_buffer = malloc(width * color_depth);
|
||||
unsigned char *data_buffer = malloc(data_length);
|
||||
unsigned char *line_buffer = malloc(bmp_data_line_length(width, color_depth));
|
||||
for (y = 0; y < height; y++) {
|
||||
generate_line(height - y - 1, data_buffer);
|
||||
generate_line(height - y - 1, data_buffer, data_length);
|
||||
bmp_data_line(line_buffer, width, color_depth, data_buffer);
|
||||
fwrite(line_buffer, bmp_data_line_length(width, color_depth), 1, fptr);
|
||||
}
|
||||
|
||||
+6
-3
@@ -1,14 +1,17 @@
|
||||
#include "args.h"
|
||||
#include "bmp.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define COLOR_DEPTH 3
|
||||
|
||||
parameters global_params;
|
||||
|
||||
void generate_line(unsigned short y, unsigned char *data_buffer) {
|
||||
memset(data_buffer, y, global_params.width * COLOR_DEPTH);
|
||||
void generate_line(unsigned short y, unsigned char *data_buffer,
|
||||
unsigned int len) {
|
||||
unsigned int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
data_buffer[i] = global_params.start[i % 3];
|
||||
}
|
||||
}
|
||||
|
||||
void debug_parameters(parameters params) {
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
#ifndef RAND_H
|
||||
#define RAND_H
|
||||
|
||||
void set_seed(float new_seed);
|
||||
void set_seed(unsigned long new_seed);
|
||||
unsigned char rand_uchar(unsigned int max);
|
||||
unsigned short rand_ushort(unsigned int max);
|
||||
|
||||
|
||||
+2
-1
@@ -17,6 +17,7 @@ struct Parameters {
|
||||
|
||||
typedef struct Parameters parameters;
|
||||
|
||||
typedef void line_fn(unsigned short y, unsigned char *data_buffer);
|
||||
typedef void line_fn(unsigned short y, unsigned char *data_buffer,
|
||||
unsigned int len);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user