C ++ Reading and Editing Raster Image Pixels

I am trying to create a program that reads an image (starting with raster images because they are the simplest), looking for an image by a specific parameter (i.e. one pixel (255, 0, 0), followed by one of (0, 0, 255 )) and somehow changes the pixels (only inside the program, and does not save the original file.), and then displays it.

I use Windows, a simple GDI for preferences, and I want to avoid custom libraries just because I really want to understand what I'm doing for this test program.

Can anyone help? Anyway? Site? Advice? Code example?

thank

+3
source share
2

" " ( ), :

char *filename = "filename";
FILE *input = fopen(filename, "rb+");
if (!input)
{
    printf("Could not open input file '%s'\n", filename);
    system("PAUSE");
    return 1;
}

fseek(input, 0, SEEK_END);
size_t size = ftell(input);
rewind(input);

char *buffer = (char *) malloc(size);
if (!buffer)
{
    printf("Out of memory, could not allocate %u bytes\n", size);
    system("PAUSE");
    return 1;
}

if (fread(buffer, 1, size, input) != size)
{
    printf("Could not read from input file '%s'\n", filename);
    free(buffer);
    fclose(input);
    system("PAUSE");
    return 1;
}
fclose(input);

// we loaded the file from disk into memory now

for (size_t i = 0; (i + 3) < size; i += 4)
{
    if (buffer[i + 0] == 0xFF && buffer[i + 1] == 0x00 && buffer[i + 2] == 0x00 && buffer[i + 3] == 0x00)
    {
        printf("Pixel %i is red\n", (i / 4));
    }
}

free(buffer);

, C, ++. C. , if , , /. if "if (compare_4bytes (buffer, 0xFF000000))".

, , . , .. .bmp/​​.png/.jpg - , , . , " ".

BMP, , ( ), /. . , .

- () (http://corona.sourceforge.net/), DevIL (http://openil.sourceforge.net/) :)

+4

" " BMP, Wikipedia

+2

Source: https://habr.com/ru/post/1743646/


All Articles