0

我正在尝试将 PBM 图像从 5x5 扩展到 10x10,但我遇到了一些障碍。

我已经阅读了标题和高度/宽度以及以下内容

    int fSize = size(inFile);
    char *inputBuffer = malloc(fSize);


    int pbmHeight, pbmWidth;
    memset(inputBuffer, 0, fSize);

    if (!fgets(inputBuffer, sizeof(inputBuffer), inFile)) {
        fprintf(stderr, "Unable to read image format.\n");
        exit(-1);
    }

    if (inputBuffer[0] != 'P' || inputBuffer[1] != '4') {
        fprintf(stderr, "Invalid image format.\n");
        exit(-1);
    }

    if (fscanf(inFile, "%d %d", &pbmWidth, &pbmHeight) != 2) {
        fprintf(stderr, "Invalid image size.\n");
        exit(-1);
    }

    int i;
    int bitRemainder = ((pbmWidth % 8) != 0 ? 8 - (pbmWidth % 8) : 0);

然后读取数据

    while (fgetc(inFile) != '\n');
    fread(inputBuffer, pbmHeight * pbmWidth, 1, inFile);

我有第二个字符 *secondPBM,其中包含另一个相同高度/宽度的 PBM 图像

我想要做的是遍历第一张图像、第二张图像的每一位,进行比较,然后将特定位输出到输出

比如这里是图片1(注意图片宽度是5,但是需要填1个字节)

0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0

这是图2

1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 0 0 0

基于图像 1 和图像 2 位,我需要输出 4 位,因此新图像大小变为 2 * pbmWidth

因此,例如,输出应该是(一个 10x10 图像,6 位设置为 0 以填充剩余字节)(第二张图像中的位值将使用另一段代码设置,并且不完全是列出的内容这里)

1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0
1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0 1 0 0 0 0 0 0

我正在像这样读取第一张和第二张图像的位值

这是我的 getBitValue 和 setBitValue 函数

int getBitVal(unsigned char *keyStrBin, int keyIndex) {

    int keyMod = keyIndex % 8;  
    int keyIn = keyIndex / 8;

return (((keyStrBin[keyIn]) >> (7 - (keyMod))) & 1);
}


void setBitVal(unsigned char *keyC, int keyIndex) {

    int keyMod = keyIndex % 8;  
    int keyIn = keyIndex / 8;
    keyC[keyIn] |= (1 << (7 - (keyMod)));
}


int newWidth = 2 * pbmWidth;
int newHeight = 2 * pbmHeight;
int totalBits = newWidth * newHeight;
int newFileSize = (totalBits % 8 == 0) ? (totalBits / 8) : (totalBits / 8 + 1);
int bitRemainder = ((pbmWidth % 8) != 0 ? 8 - (pbmWidth % 8) : 0);
int newbitRemainder = ((newWidth % 8) != 0 ? 8 - (newWidth % 8) : 0);

for (i = 0; k = 0; i < newHeight * (newWidth + newbitRemainder); i++, k++) {
    if (i != 0 && i % (pbmWidth - 1) == 0) {
        i += (bitRemainder - 1);
    }
    if (k != 0 && k % (newWidth - 1) == 0) {
        k += (newbitRemainder - 1);
    }
    if (getBitVal((unsigned char *)inputBuffer, i) == 0 && getBitVal(keyBuffer, k) == 0) {


    // Code to set 1 bit to black in the 1st row, then set another bit to black in the 2nd row. For example, The bit at index 0 becomes a 1, then the bit at index 17 becomes a 1 (Making a 2x2 square)
    // 1 0
    // 0 1
    // Then move on to the 2nd bit (or bit index 1) of the input image, compare.
    }

    else if (getBitVal((unsigned char *)inputBuffer, i) == 0 && getBitVal(keyBuffer, k) == 1) {

    }

    else if (getBitVal((unsigned char *)inputBuffer, i) == 1 && getBitVal(keyBuffer, k) == 0) {

    }

    else if (getBitVal((unsigned char *)inputBuffer, i) == 1 && getBitVal(keyBuffer, k) == 1) {

    }

我似乎无法弄清楚如何在比较 if 语句中设置位。

我试过这个,但它似乎不能正常工作。

setBitVal(pbmOut1Buffer, k * 2);
setBitVal(pbmOut1Buffer, (k * 2) + (newWidth + newbitRemainder) + 1);

对此的任何帮助将不胜感激。谢谢大家。

4

0 回答 0