I am trying to expand a PBM image of say, 5x5 to 10x10 but I have hit somewhat of a road block.
I have read the header, and height / width with the following
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);
Then read the data
while (fgetc(inFile) != '\n');
fread(inputBuffer, pbmHeight * pbmWidth, 1, inFile);
I have a second char *secondPBM which contains another PBM image of the same height / width
What I am looking to do, is iterate through each bit of the 1st image, the 2nd image, compare, and then output specific bits to an output
For example, here is image 1 (Note the image width is 5, but it needs to fill 1 byte)
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
Here is image 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
Based on image 1, and image 2 bits I need to output 4 bits so the new image size becomes 2 * pbmWidth
So for example, the output should be (a 10x10 image, with 6 bits set to 0 to fill the remaining byte) (The bit values in the 2nd image will be set with another piece of code and and isn't exactly what is listed here)
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
I'm reading the bit value of the 1st and 2nd image like this
Here are my getBitValue and setBitValue functions
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) {
}
I can't seem to figure out how to set the bits in the comparative if statements.
I tried this, but it doesn't seem to work right.
setBitVal(pbmOut1Buffer, k * 2);
setBitVal(pbmOut1Buffer, (k * 2) + (newWidth + newbitRemainder) + 1);
Any help with this would be greatly appreciated. Thank you all in advance.