Certain things have changed since this was originally posted. I've marked them with an Edit header.
I'm writing an image file from a source with a different format. This is data which is 1024x1024 and has 2 bytes per pixel.
The source and destination image formats both consist of a header of a variable number of bytes(containing metadata such as the bytes per pixel etc) which is then followed by the Image data.
Don't know if relevant, but we're using a 40+ year old software (called Mcidas) to display these images
Previously, I read both bytes into an unsigned short
array. Then some bit manipulations concatenated them to a new unsigned short
. I then use the fwrite()
function to write them into the target file.
Edit
I'm now using fread()
on data
directly.
The read operation is working well (tried both fputc()
and fread()
)
Edit
The image data part of hexdumps of the two files completely match. However, for some reason, the image seems to show a bit error in certain bytes. The source file seems to be clean, too.
Here's the Image (a 2MB file):
http://pdsimage.wr.usgs.gov/archive/mess-e_v_h-mdis-2-edr-rawdata-v1.0/MSGRMDS_1001/DATA/2007_156/EW0089565661F.IMG
You could save this as "source.IMG" to test it.
Below is a comparison of the source and final images, with a line scan done on the first line.
Note the sudden jumps for certain pixels, that give rise to contours. These seem to be about 256 in length, which leads us to suspect that there might be a flip of bits somewhere(the sign bit maybe?). The identical hexdumps indicate otherwise however.
Here's the errant code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
unsigned char data_arr[2];
unsigned short data;
data=0;
int pixel_per_line=1024;
char path1[80]="source.IMG";
char path[80]="destination"
fp=fopen(path1,"rb");
bin=fopen(path,"wb");
//fp points to the input file, bin to the output file.
fseek(fp,8193,SEEK_SET);//sets the pointer to the relevant byte in the source file.
//A similar fseek() is used on bin in the original program
while(!feof(fp))
{
//fflush(fp); (Removed the fflush()s now)
int j;
for(j=0;j<pixels_per_line;j++)//iterate through all pixels in one line
{
for(int i=0;i<2;i++)
data_arr[i]=fgetc(fp);
//data=(data_arr[0] << 8) | data_arr[1];// bit manipulations done in the earlier version.
//This fails to give the right output.
data=data_arr[0]*256 + data_arr[1]; // This switches alternate bytes.
if(fwrite(&data,1,sizeof(data),bin)!=2)
printf("Write pains!");
}
//fflush(bin);
}
}
EDIT:
Added the
fopen()
statements and tested withfflush()
removed, the error remains.Added declaration for
unsigned short data
The code should run now.
EDIT
Problem solved for the 2 byte image! All I did was switch alternate bytes. Look at the corrected code. I am not sure why it works though. Is it an endianness issue? Note that bitshifts did not work.
I am having a similar problem for four byte images now. The data is stored as a floating point number. I fread()
the bytes into a float, scale it and cast it into an integer. The results are similarly grainy. Permuting the bytes with bitshifts does not solve the problem.
Can anyone throw some light on this?