Hello all

I have a problem at hand. I need to work on a C code which will read multiple images in RAW format (greyscale) and do an AND operation of corresponding pixels of all the images. Can someone help out on this? I am a novice and would definitely appreciate any help.

Thanks
J

Dani AI

Generated

Short summary and assumptions: you have N raw 8-bit grayscale files (no header), all the same size (here 512×512). The task is to produce a new raw image whose pixel at (x,y) is the bitwise AND of the corresponding pixels across all input images. Confirm each input file is exactly SIZE*SIZE bytes first — mismatched sizes produce garbage.

AND vs equality: if you want "pixel value identical in every image" use equality checks (compare values). Bitwise AND answers a different question: it yields the bits that are 1 in every image. If you mean the latter, initialize the accumulator to 0xFF (all ones) before ANDing. Initializing to 1 (as in your code) only preserves the least-significant bit and is wrong for multi-bit pixels.

What to fix in the posted code ():

  • fopen(filename, "rb") should be fopen(filename[i], "rb").
  • Imagedata is declared too small; you need either images[NUM_IMAGES][SIZE*SIZE] or allocate per-image buffers, or stream bytes from files.
  • outputData must be a buffer (not a single unsigned char).
  • Use 0xFF as the AND seed, not 1.
  • Check fread/fwrite/fopen return values and close files.
  • Use consistent indexing (flat index or [row][col] consistently).

A compact, memory-friendly approach (streaming — opens all inputs and reads one byte per file per pixel):

#include <stdio.h>
#include <stdlib.h>

#define SIZE 512
#define NUM_IMAGES 2

int main(void) {
    const char *filenames[NUM_IMAGES] = {"92eb.raw", "92eb.raw"};
    FILE *in[NUM_IMAGES];
    for (int i = 0; i < NUM_IMAGES; ++i) {
        in[i] = fopen(filenames[i], "rb");
        if (!in[i]) { perror(filenames[i]); return 1; }
    }
    FILE *out = fopen("output.raw", "wb");
    if (!out) { perror("output.raw"); return 1; }

    for (size_t p = 0; p < (size_t)SIZE * SIZE; ++p) {
        unsigned char acc = 0xFF;
        for (int i = 0; i < NUM_IMAGES; ++i) {
            int c = fgetc(in[i]);
            if (c == EOF) { fprintf(stderr, "short file: %s\n", filenames[i]); return 1; }
            acc &= (unsigned char)c;
        }
        fputc(acc, out);
    }

    for (int i = 0; i < NUM_IMAGES; ++i) fclose(in[i]);
    fclose(out);
    return 0;
}

Quick troubleshooting: if output "looks wrong" verify all files are same byte-count, open them in a hex viewer to inspect the first few bytes, and try both the equality test and the AND method to ensure you’re solving the right problem. This addresses the issues raised by and builds on ’s reading example while fixing the bugs in your posted code.

Recommended Answers

All 8 Replies

So you have pixels represented as some number of shades of gray. And you want to AND them with something? Why AND? Is this some sort of mask where all ON (black? white?) is a mask to include the pixel and OFF is a mask to not include. In that case you could say:

for each row,
    for each column,
        result = pixel[row][col] & mask[row][col]; // '&' being bitwise-and

but if the mask is not all ON or all OFF bits (0x0 or 0xFF, say) then the resulting pixels will not be a gray scale but some odd shade. Like, if the mask is 0x5A and you mask that with a gray representing 20% gray you get a strange gray.

Its like i will be altering the image at variuos postions (on copies of the original image), and an AND operation is performed so that if it is a 1, then the particular pixel ahs not been altered. THe pixel will take values from 0 to 256 (8bits).


So you have pixels represented as some number of shades of gray. And you want to AND them with something? Why AND? Is this some sort of mask where all ON (black? white?) is a mask to include the pixel and OFF is a mask to not include. In that case you could say:

for each row,
    for each column,
        result = pixel[row][col] & mask[row][col]; // '&' being bitwise-and

but if the mask is not all ON or all OFF bits (0x0 or 0xFF, say) then the resulting pixels will not be a gray scale but some odd shade. Like, if the mask is 0x5A and you mask that with a gray representing 20% gray you get a strange gray.

You could just say....

if (mask_bit == 1)
result = original;
else
resilt = altered_pixel;

Becuase if the mask is one bit and the pixel is 8 bits you'd have to do some fussing with stuff to make AND work. If they are both 8 bits you could say:

result_pixel = mask_pixel & original_pixel;

but now the result is either 0 or original_pixel, you still have to supply the modified pixel.

Maybe some pseudocode or real code would help. What have you got so far?

OK .. i may have not put the problem thru clearly. I have a 512X512 pixel image. On which i make say 5 copies and make slight variations on these 5 images.What i have to do is read each pixel say at [1,1] of each of these 5 images and do an AND operation of all 5. Pls note that the original is not being involved, and no comparison is being made to the original. What is possible is that the alteration could be the same in a particular pixel? But that doesnt matter, if the value of a particular pixel is same in all the images then i neeed the location of the pixel. And operation is one way of finding out, if by ANdin the particular pixel of all the images is 1 then i need the pixel location.

Sorry if i am bothering u, but if you could get me a rough code then it would be great, im not so well versed in C.

Thank you
j

It's no bother at all, but you are right I don't understand what you are attempting. It sounds like you are doing something to these 5 copies and then seeing if doing something to them didn't change them. And it sounds like you are determined to use AND rather than ==, yet I don't get why.

Maybe it is time for someone else on this forum to chime in; I'm just being dense.

I want to use AND coz it is assumed that i dont have the original image. And by doing the AND operation i can know what part of the image(pixels) are the same in all of the images.

I hope this could clear your doubt.

u can use this code;

#include<stdio.h>
int main(void)
{
	FILE *fp;
	unsigned char image[256][256];
             int r,c;

if((fp = fopen("f16.dat","r+")) == NULL)
	{
		printf("Unable to open the specified file.\n");
		getch();
		exit(1);
	}
	else
	{
		for(r=0;r<256;r++)
		{
			for(c=0;c<256;c++)
			{
				image[r][c]=(unsigned char) fgetc (fp);
			}
		}
		fclose(fp);
}
getch();
return(0);
}

you can then alter the values in image array to play with the image.

I wrote a code but the output file is not being written (image file), but when reading the array i am getting the bits for each pixel. Can u take a look at it?

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>


#define size 512
#define num_images 2
using namespace std;


int main() {
char *filename[num_images] = {"92eb.raw","92eb.raw"};


unsigned char Imagedata[2];
unsigned char outputData;


FILE *input, *output;
for (int i = 0; i < num_images; i++) {
if (!(input = fopen(filename,"rb"))) {
cout << "Unable to open file" <<endl;
return 0;
}
fread(Imagedata,sizeof(unsigned char),size * size,input);
}


int result = 1;
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
for (int i = 0; i < num_images; i++) {
result=result & Imagedata[j][k];
}
outputData[j][k] = result;
result = 1;
}
}
if (!(output = fopen("output.raw", "wb"))) {
cout << "Could not open file for writing" << endl;
}


int ret = fwrite(outputData,sizeof(unsigned char), size * size,output);
cout << "Number of items written" << ret << endl;


return 1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.