c - 尝试构建一个函数来计算图像中的唯一像素

标签 c colors pixel bmp

..这是有用的东西。然而..我在网上看不到任何相关内容..这似乎有点问题。这就是为什么..我尝试自己构建该功能:

typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned long dword;

typedef struct
{
    byte R;
    byte G;
    byte B;
} RGB;

dword
getpixels
(char *FILE_NAME)
{
    dword WIDTH = 500; // example dimension
    dword HEIGHT = 500; // example dimension
    FILE* fp = fopen(FILE_NAME, "rb");
    #define HEADERS_SIZE 54

    byte color[3];
    byte colorTable[50000][3]; // maximum 50000 pixels
    int val = (-1), valr;

    dword l;
    dword count = 0;

    fseek(fp, HEADERS_SIZE, SEEK_SET); // move iterator to where the pixels start fromS

    // alternate : fread(&valr, 1, 1, fp) == 1
    while( (valr = fgetc(fp)) != EOF ) // runs the code while this is true
    {
        val++; // increment index
        if(val > 2) val = 0;
        color[val] = valr;

        for(l=0; l<50000; l++) {
        if(val == 2 && color[0] != colorTable[l][0] && color[1] != colorTable[l][1] && color[2] != colorTable[l][2])
        {
            colorTable[l][0] = color[0];
            colorTable[l][1] = color[1];
            colorTable[l][2] = color[2];
            count++;
        }
        }

        fseek(fp, WIDTH%4, SEEK_CUR); // skip padding
    }
    fclose(fp);
    return count;
}

正如您可能已经感觉到的那样..该功能不起作用,因为..我不知道。这就是我真正要问的。我做错了什么?

最佳答案

尝试如下操作(未经测试,使用后果自负)。我重命名了一些变量以使代码更加不言自明。

编辑:更新为实际上包括我在第一个版本中错过的颜色计数器(假设这是您想要从问题中得到的)。请记住,这很慢,因为您只是在颜色表中进行缓慢的线性查找。对于具有 50000 种颜色的 500x500 图像,您可能需要执行 100 亿次循环。

int numColors = 0;
int colorIndex = 0;
int inputColor;
int colorCounts[50000];  //Be aware of possible overflow

memset(colorCounts, 0, sizeof(int)*50000);  // Initialize counters

while( (inputColor = fgetc(fp)) != EOF ) 
{
    color[colorIndex] = inputColor;
    colorIndex++;

    fseek(fp, WIDTH%4, SEEK_CUR);

    if (colorIndex < 3) continue;  // or use an if block
    colorIndex = 0;

    for (l = 0; l < numColors; ++l) 
    {
        if (color[0] == colorTable[l][0] && 
            color[1] == colorTable[l][1] && 
            color[2] == colorTable[l][2])
        { 
            ++colorCounts[l]; // Update counter
            break;
        }
    }

    if (l >= numColors)
    {
       if (numColors >= 50000) exit(1);  //Or do something else appropriate
       colorTable[numColors][0] = color[0];
       colorTable[numColors][1] = color[1];
       colorTable[numColors][2] = color[2];

       colorCounts[l] = 1;   //Initialize counter

        ++numColors;
    }

}

编辑 2:打印颜色表以帮助您调试的代码(添加到函数末尾以显示找到的颜色):

for (l = 0; l < numColors; ++l)
{
    printf("%d) %02X %02X %02X\n", l, colorTable[l][0], colorTable[l][1], colorTable[l][2]);
}

关于c - 尝试构建一个函数来计算图像中的唯一像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26572659/

相关文章:

c - 按 block 输出的 MPI 矩阵

c - 在黑客排名中显示段错误

c - Ncurses 在光标后显示不需要的文本

c - fgets() 在字符串中包含新行

c# - 如何显示所有可能颜色的网格?

css - 在同一类声明中两次使用 CSS 属性

android - 更改笔划颜色会更改以前的笔划颜色

css - <li> 元素(文本和元素符号)是白色的

java - 获取灰度或黑白图像每个像素的像素值?

java - 如何创建 ARGB_8888 像素值?