Example #1
0
void mergeImages(unsigned char *inputBuffer1, unsigned char *inputBuffer2, int pbmHeight, int pbmWidth, int fSize) {

	// Iteration and bit counters
	int i;
	int bitRemainder = ((pbmWidth % 8) != 0 ? 8 - (pbmWidth % 8) : 0);
	int readBits = pbmHeight * (pbmWidth + bitRemainder) + 8;

	unsigned char *outBuffer = malloc(readBits);
	memset(outBuffer, 0, readBits);

	for (i = 0; i < readBits; i++) {
		if (getBitVal(inputBuffer1, i) == 1 && (getBitVal(inputBuffer2, i) == 0)) {
			setBitVal(outBuffer, i);
		}
		else if (getBitVal(inputBuffer1, i) == 1 && (getBitVal(inputBuffer2, i) == 1)) {
			setBitVal(outBuffer, i);
		}
		else if (getBitVal(inputBuffer1, i) == 0 && (getBitVal(inputBuffer2, i) == 1)) {
			setBitVal(outBuffer, i);
		}
	}
	
	fprintf(stdout, "P4\n");
	fprintf(stdout, "%d %d", pbmWidth, pbmHeight);

	for (i = 0; i < readBits / CHAR_BIT; i++) {
		fprintf(stdout, "%c", outBuffer[i]);
	}
	
}
Example #2
0
int main(int argc, char* argv[])
{
    int a = 0x03;

    a= setBitVal(3, 5, a);
    printf("%02x\n",a);

    a = clearBitVal(0, 3, a);
    printf("%02x\n",a); 

    a = 0x0f;
    a = testIfSet(0,7,a);
    printf("%02x\n",(unsigned)a); 

   a= 0x55;
   a = toggleBits(0,7,a);
   printf("%02x\n",(unsigned)a); 
    
    return 0;
}
Example #3
0
void decryptPbm(unsigned char *inputBuffer, int pbmHeight, int pbmWidth, int bitRemainder) {

	// Calculate new PBM output height / width / bit remainder
	int newHeight = (pbmHeight / 2);
	int newWidth = (pbmWidth / 2);
	int newBitRemainder = ((newWidth % 8) != 0 ? 8 - (newWidth % 8) : 0);
	int newTotalBits = newHeight * (newWidth + newBitRemainder); // Calculate number of bits to write in new image

	unsigned char *outPbmImage = malloc(newTotalBits / CHAR_BIT);
	memset(outPbmImage, 0, newTotalBits / CHAR_BIT);

	// i is for inputBuffer
	// l is for outPbmImage
	// hitCount is for locating the end of the PBM image based on the newWidth, not pbmWidth since the input is double the width and i is used * 2
	int i, hitCount = 0;
	int l = 0;
	int tLeft = 0;
	int tRight = 0;
	int bLeft = 0;
	int bRight = 0;
	
	// Iterate through inputBuffer until end of bits
	// Since the new output image should be 1/4 the size, we will multiply by 4 here
	for (i = 0, l = 0; i < (newTotalBits * 4); i++, l++) {

		// Like the encrypt function, we need to account for the trailing bits at the end of the PBM image width
		if (i != 0 && hitCount % newWidth == 0) {
			// When we hit the newWidth, we need to go to the next block of bits, a 2x2 block becomes 1 bit in the output image
			// Divide by 2 because we are hitting each block with i * 2
			i += (pbmWidth + (bitRemainder * 2)) / 2;
			// The output iterator needs to skip the trailing bits and go to the next row
			l += newBitRemainder;
			hitCount = 0;
			
		}
		// Assign bit values for eacch pixel block for comparison later
		tLeft = getBitVal(inputBuffer, i * 2);
		tRight = getBitVal(inputBuffer, (i * 2) + 1);
		bLeft = getBitVal(inputBuffer, ((i * 2) + (pbmWidth + bitRemainder)));
		bRight = getBitVal(inputBuffer, ((i * 2) + (pbmWidth + bitRemainder) + 1));

		if (tLeft == 1 && tRight == 1 && bLeft == 1 && bRight == 1) {
			setBitVal(outPbmImage, l);
		}
	
		hitCount++;

	}

	// Finally print outPbmImage to stdout

	fprintf(stdout, "P4\n");
	fprintf(stdout, "%d %d\n", newWidth, newHeight);

	for (i = 0; i < (newTotalBits / CHAR_BIT); i++) {
		fprintf(stdout, "%c", outPbmImage[i]);
	}

	// Clean up
	free(outPbmImage);
	free(inputBuffer);

}