Skip to content

Instantly share code, notes, and snippets.

@a10y
Created July 28, 2012 05:37
Show Gist options
  • Save a10y/3191947 to your computer and use it in GitHub Desktop.
Save a10y/3191947 to your computer and use it in GitHub Desktop.
Sum of Two Floating Point Binary Images
#include <stdio.h>
#include <stdlib.h>
#define SIZE 128 //If you have an input image that is NxN, SIZE = N.
//Adjust to suit your needs.
int main(int argc, char* argv[]){
FILE* imageA = fopen(argv[1], "rb"); //Input image 1 (filename read from argv[1])
FILE* imageB = fopen(argv[2], "rb"); //Input image 2 (filename read from argv[2])
FILE* imageSum = fopen(argv[3], "w+b"); //Output image (filename read from argv[3])
//Float arrays that contain the data of the images
float *afloat[SIZE], *bfloat[SIZE], *cfloat[SIZE];
int k;
for (k=0; k < SIZE; k++){
//Allocate our data arrays
afloat[k] = (float*)malloc(sizeof(float) * SIZE);
bfloat[k] = (float*)malloc(sizeof(float) * SIZE);
cfloat[k] = (float*)malloc(sizeof(float) * SIZE);
//Read our input image files into their corresponding data arrays
fread(afloat[k], 1, sizeof(float) * SIZE, imageA);
fread(bfloat[k], 1, sizeof(float) * SIZE, imageB);
}
//Explicitly close the input file handles
fclose(imageA);
fclose(imageB);
int i, j;
for (i=0; i < SIZE; i++){
for (j=0; j < SIZE; j++){
//Do the addition of the two images for every point,
//and store the resulting point in the output buffer
cfloat[i][j] = afloat[i][j] + bfloat[i][j];
}
}
for (i=0; i < SIZE; i++){
//Write the output buffer to binary file
fwrite(cfloat[i], 1, sizeof(float) * SIZE, imageSum);
}
//Close the file handle for the output file
fclose(imageSum);
for (i=0; i < SIZE; i++){
//Free ALL the memory!
free(afloat[i]);
free(bfloat[i]);
free(cfloat[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment