Created
May 25, 2019 14:19
-
-
Save zhiyb/d6b7354da6ec0681e778a7a4db517ae8 to your computer and use it in GitHub Desktop.
Ancient BMP file RGB channel conversion code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <inttypes.h> | |
| #define NONE 0 | |
| #define BLUE 1 | |
| #define GREEN 2 | |
| #define RED 3 | |
| int getRGB(void) | |
| { | |
| get: | |
| switch (getchar()) { | |
| case 'R': | |
| case 'r': | |
| return RED; | |
| case 'G': | |
| case 'g': | |
| return GREEN; | |
| case 'B': | |
| case 'b': | |
| return BLUE; | |
| case 'N': | |
| case 'n': | |
| return NONE; | |
| } | |
| goto get; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| uint32_t i, offset, c, r, g, b; | |
| FILE *in, *out; | |
| if (argc != 3) { | |
| fputs("Arg error!\n", stderr); | |
| return 1; | |
| } | |
| in = fopen(argv[1], "r"); | |
| out = fopen(argv[2], "w"); | |
| if (in == NULL || out == NULL) { | |
| fputs("File error!\n", stderr); | |
| return 2; | |
| } | |
| for (i = 0; i < 10; i++) | |
| fputc(fgetc(in), out); | |
| fputc(i = fgetc(in), out); | |
| offset = (uint32_t)i; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 8; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 16; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 24; | |
| for (i = 14; i < offset; i++) | |
| fputc(fgetc(in), out); | |
| puts("Choose colour input channel: [RGB]"); | |
| c = getRGB(); | |
| puts("Set colour: [RRGGBB]"); | |
| scanf("%2X%2X%2X", &r, &g, &b);; | |
| while (1) { | |
| int rgb[4]; | |
| rgb[NONE] = 0; | |
| rgb[BLUE] = fgetc(in); // Blue | |
| rgb[GREEN] = fgetc(in); // Green | |
| rgb[RED] = fgetc(in); // Red | |
| if (rgb[0] == EOF || rgb[1] == EOF || rgb[2] == EOF) | |
| break; | |
| fputc(rgb[c] * b / 255, out); // Blue | |
| fputc(rgb[c] * g / 255, out); // Green | |
| fputc(rgb[c] * r / 255, out); // Red | |
| } | |
| fclose(in); | |
| fclose(out); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <inttypes.h> | |
| #define NONE 0 | |
| #define BLUE 1 | |
| #define GREEN 2 | |
| #define RED 3 | |
| int getRGB(void) | |
| { | |
| get: | |
| switch (getchar()) { | |
| case 'R': | |
| case 'r': | |
| return RED; | |
| case 'G': | |
| case 'g': | |
| return GREEN; | |
| case 'B': | |
| case 'b': | |
| return BLUE; | |
| case 'N': | |
| case 'n': | |
| return NONE; | |
| } | |
| goto get; | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| uint32_t i, offset, r, g, b; | |
| FILE *in, *out; | |
| if (argc != 3) { | |
| fputs("Arg error!\n", stderr); | |
| return 1; | |
| } | |
| in = fopen(argv[1], "r"); | |
| out = fopen(argv[2], "w"); | |
| if (in == NULL || out == NULL) { | |
| fputs("File error!\n", stderr); | |
| return 2; | |
| } | |
| for (i = 0; i < 10; i++) | |
| fputc(fgetc(in), out); | |
| fputc(i = fgetc(in), out); | |
| offset = (uint32_t)i; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 8; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 16; | |
| fputc(i = fgetc(in), out); | |
| offset |= (uint32_t)i << 24; | |
| for (i = 14; i < offset; i++) | |
| fputc(fgetc(in), out); | |
| puts("Choose colour replace method: [RGB]"); | |
| r = getRGB(); | |
| g = getRGB(); | |
| b = getRGB(); | |
| while (1) { | |
| int rgb[4]; | |
| rgb[NONE] = 0; | |
| rgb[BLUE] = fgetc(in); // Blue | |
| rgb[GREEN] = fgetc(in); // Green | |
| rgb[RED] = fgetc(in); // Red | |
| if (rgb[0] == EOF || rgb[1] == EOF || rgb[2] == EOF) | |
| break; | |
| fputc(rgb[b], out); // Blue | |
| fputc(rgb[g], out); // Green | |
| fputc(rgb[r], out); // Red | |
| } | |
| fclose(in); | |
| fclose(out); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment