D
D
dlinyj2013-05-16 21:27:45
C++ / C#
dlinyj, 2013-05-16 21:27:45

Formation of a 16-bit PGM file

I decided to present them in the form of graphic files for experiments with surfaces (many thanks for the idea Eddy_Em . To do this, I took an excellent simple PGM file format. Format description . In general, everything works fine with the 8-bit format:

#include <stdio.h>
#define width 255
#define height 255
int main()
{
  unsigned int i,j;
  FILE *img_file = NULL;
  img_file = fopen("dat/greyscale.pgm", "w");
  char tmp=0;
  fprintf(img_file, "P5 \n%d %d \n255 \n", width, height);
  for (i = 0; i < width; i++) {
    for (j = 0; j < height; j++) {
      tmp=i;
      fwrite(&tmp, sizeof(char), 1, img_file);
    }
  }
  if (img_file != NULL) 	fclose(img_file);
  return 0;
}


The result is excellent. The size of the output file is 65,043 bytes



But it's worth trying to switch to a two-byte format, everything goes wrong.
I replace the data variable with short

unsigned short tmp=0;


In the file header I say that we now have 16 significant bits:
fprintf(img_file, "P5 \n%d %d \n65535 \n", width, height);


And now I'm writing short

fwrite(&tmp, sizeof(unsigned short), 1, img_file);


Full version of the code
#include <stdio.h>

#define width 255
#define height 255
int main()
{
  unsigned int i,j;
  FILE *img_file = NULL;
  img_file = fopen("dat/greyscale.pgm", "w");
  unsigned short tmp=0;
  fprintf(img_file, "P5 \n%d %d \n65535 \n", width, height);
  for (i = 0; i < width; i++) {
    for (j = 0; j < height; j++) {
      tmp=i;
      fwrite(&tmp, sizeof(unsigned short), 1, img_file);
    }
  }
  if (img_file != NULL) 	fclose(img_file);
  return 0;
}



I get an erroneous file, 130,070 bytes in size, which looks like a black square when opened. The HEX editor shows that the result is correct.

Even if the byte order is not correct, the output should still not be a black square, but some kind of overflow.

What to do and who is to blame? How do I generate a 16-bit grayscale file? Can use some other uncompressed format (if so, which one?).

UPD The problem was solved as follows, the output to the file was implemented strongly-typed

{
       tmp=(char)(i&0xFF);
       fwrite(&tmp, sizeof(char), 1, img_file);
       tmp=(char)((i>>8)&0xFF);
       fwrite(&tmp, sizeof(char), 1, img_file);
}

Graphic editors show the resulting file in black, but you can already work with it and the FFT is done ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eddy_Em, 2013-05-16
@dlinyj

If you want to store accurate data in a graphic file, I strongly advise you to use the FITS format!
An excellent description + examples of working with cfitsio are on the Internet. Well, I myself immodestly can offer to read my LiveJournal on this subject.
If you don't like FITS, you can choose tiff (perhaps even with compression) or png. I worked with both formats: everything is fine, there are no difficulties.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question