Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Last active December 10, 2015 01:09
Show Gist options
  • Select an option

  • Save neuro-sys/4356265 to your computer and use it in GitHub Desktop.

Select an option

Save neuro-sys/4356265 to your computer and use it in GitHub Desktop.
160x200 4bpp Windows BMP to CPC MODE 0 converter.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
unsigned char *file_buf, bmp_buf[16000], cpc_buf[16000];
FILE *fp_in, *fp_out;
unsigned int bmp_offset, bmp_size, file_size;
int i;
if (argc != 3) { fprintf(stdout, "Usage: %s bitmap.bmp output.src\n", argv[0]); return 0;}
fp_in = fopen(argv[1], "r");
if ( (fp_in = fopen(argv[1], "r")) == NULL ){ fprintf(stdout, "Can't open file %s\n", argv[1]); return 0;}
fseek(fp_in, 0L, SEEK_END);
file_size = ftell(fp_in);
rewind(fp_in);
printf("file size: %d bytes\n", file_size);
if ( (file_buf = malloc(file_size * sizeof(char))) == NULL) {
fprintf(stderr, "Cannot allocate memory for %d bytes.\n", file_size*sizeof(char));
abort();
}
if ( (i = fread(file_buf, sizeof(char), file_size, fp_in)) == 0)
fprintf(stderr, "0 byte was read from file %s.\n", argv[1]);
fclose(fp_in);
bmp_offset = *((unsigned int *) (file_buf + 0x0a)); /* Image offset relative to file */
bmp_size = *((unsigned int *) (file_buf + 0x22)); /* Image size */
printf("bmp_offset = %d\nbmp_size = %d\n", bmp_offset, bmp_size);
/* Turn the image upside down */
for (i = 0; i < bmp_size/2; i++)
file_buf[bmp_offset+i] = file_buf[bmp_offset+bmp_size-1-i];
if ( (fp_out = fopen(argv[2], "w")) == NULL ) {
fprintf(stderr, "Can't open file: %d\n", argv[2]);
abort();
}
fwrite(file_buf, sizeof(char), file_size, fp_out);
fclose(fp_out);
free(file_buf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment