1

我成功地将 24 位 512x512 .bmp 中的像素值读取到 3-D 数组中。以下是我用于名为 lena512.bmp 的测试图像的代码。

...snip ...
FILE *fptr;

int i, j, k;
char *filename = "lena512.bmp";
//char *filename2 = "anotherimage.bmp"; 
//ofstream myfile;
unsigned char databuff[512][512][3];


BmpFileHeader fheader;
BmpInfoHeader iheader;

printf("sizeof: fheader: %d   iheader: %d\n\n", sizeof(fheader),
       sizeof(iheader));
if ((sizeof(fheader) != 14) || (sizeof(iheader) != 40)) {
    printf("Header structs are not properly packed\n");
    return 0;
}

if ((fptr = fopen(filename, "r")) == NULL) {
    printf("Coulsn't open file %s for reading.\n", filename);
    return 1;
};
if (fread(&fheader, sizeof(fheader), 1, fptr) != 1) {
    printf("Couldn't read fheader.\n");
    return 0;
}
if (fread(&iheader, sizeof(iheader), 1, fptr) != 1) {
    printf("Couldn't read iheader.\n");
    return 0;
}
printf("           fheader.type = %04x \n", fheader.type);
printf("           fheader.size = %u   \n", fheader.size);
printf("      fheader.reserved1 = %u   \n", fheader.reserved1);
printf("      fheader.reserved2 = %u   \n", fheader.reserved2);
printf("         fheader.offset = %u   \n", fheader.offset);
printf("           iheader.size = %u   \n", iheader.size);
printf("          iheader.width = %u   \n", iheader.width);
printf("         iheader.height = %u   \n", iheader.height);
printf("         iheader.planes = %u   \n", iheader.planes);
printf("           iheader.bits = %u   \n", iheader.bits);
printf("    iheader.compression = %u   \n", iheader.compression);
printf("      iheader.imagesize = %u   \n", iheader.imagesize);
printf("    iheader.xresolution = %u   \n", iheader.xresolution);
printf("    iheader.yresolution = %u   \n", iheader.yresolution);
printf("        iheader.ncolors = %u   \n", iheader.ncolors);
printf("iheader.importantcolors = %u   \n", iheader.importantcolors);
printf("\n");


if ((iheader.height != 512) || (iheader.width != 512) || (iheader.bits != 24)) {
    printf("This only works for 512x512 24-color bitmaps\n");
    return 0;
}
if (fheader.offset != 54) {
    printf("This only works if the offset is equal to 54\n");
    return 0;
}

for (i = 0; i < iheader.height; i++) {
    for (j = 0; j < iheader.width; j++) {
        if (fread(&databuff[i][j][0], 3, 1, fptr) != 1) {
            printf("Couldn't read colors for element [%d][%d]\n", i, j);
            return 0;
        }
    }
}
//myfile.open("lena512bmp_24bit.txt");
for (i = 0; i < 1; i++) { /* This should only give you the first row */
    printf("Row %2d:\n", i);
    for (j = 0; j < 1; j++) { /* look at the first element on each row */
        printf("  ");
        for (k = 0; k < 3; k++) {
    printf("%02x ", databuff[i][j][k]);
    cout << databuff[i][j][k];

        }
        printf("\n");
    }
    printf("\n\n");
}
//myfile.close();
fclose(fptr);
 return 0;

我还包括一个称为bmp_headers.h该文件的标题,它与我要问的问题不太相关。
这里的输出是37 51 9E(以十六进制显示),因为我的for循环只考虑第一行的前 3 个元素(在图像标题的前 54 个字节之后)。512x512 图像的输出与同一图像的十六进制编辑器中显示的值一致。我希望将此数组中的每个元素转换为一串二进制字符并显示它们。(即37 51 9E==> 00110111 01010001 10011110)。
如果我理解正确,我将不得不将此数组转换为字符串,然后转换为十六进制,然后转换为二进制字符串。有没有更简单的方法来做到这一点?我发现自己现在有点困惑。任何帮助,将不胜感激。

4

1 回答 1

0

好吧,你的问题被标记为 C++,所以这里是如何在 C++ 中做到这一点:

#include <sstream>

template <typename T>
std::string ToBinary(T value)
{
    std::stringstream sstream;
    while (value > 0)
    {
        sstream << static_cast<char>((value % 2) + 48);
        value /= 2;
    }
    std::string result = sstream.str();
    std::reverse(result.begin(), result.end());
    return result;
}

for (i = 0; i < 1; i++) { /* This should only give you the first row */
    std::cout << "Row " << i << std::endl;
    for (j = 0; j < 1; j++) { /* look at the first element on each row */
        std::cout << "  ";
        for (k = 0; k < 3; k++) {
            std::cout << ToBinary(databuff[i][j][k]);
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}
于 2012-06-15T17:39:29.180 回答