Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- unsigned char *readBMP(char *filename, int *size) {
- int width, height;
- unsigned char *data;
- unsigned char info[54];
- FILE *file = fopen(filename, "rb");
- if (file == NULL)
- return 0;
- fread(info, sizeof(unsigned char), 54, file); // read the 54-byte header
- // extract image height and width from header
- width = *(int *) &info[18];
- height = *(int *) &info[22];
- *size = 3 * width * height;
- data = (unsigned char *) malloc(*size * sizeof(unsigned char)); // allocate 3 bytes per pixel
- fread(data, sizeof(unsigned char), (size_t) *size, file); // read the rest of the data at once
- for (int i = 0; i < *size; i += 3) {
- unsigned char tmp = data[i];
- data[i] = data[i + 2];
- data[i + 2] = tmp;
- }
- fclose(file);
- return data;
- }
- int main() {
- int size = 0;
- char filename[] = "output.bmp";
- unsigned char *data = readBMP(filename, &size);
- for (int i = 0; i < size; i++) {
- printf("%d. %d\n", i + 1, (int) data[i]);
- if ((i + 1) % 3 == 0)
- printf("\n");
- }
- free(data);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement