Answer the question
In order to leave comments, you need to log in
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;
}
unsigned short tmp=0;
fprintf(img_file, "P5 \n%d %d \n65535 \n", width, height);
fwrite(&tmp, sizeof(unsigned short), 1, img_file);
#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;
}
{
tmp=(char)(i&0xFF);
fwrite(&tmp, sizeof(char), 1, img_file);
tmp=(char)((i>>8)&0xFF);
fwrite(&tmp, sizeof(char), 1, img_file);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question