void WriteTIFFfile(char *geneName, int width, int height, float *pixels){
	char filename[150];
	FILE *out;
	float min, max, range;
	int i, j, pix;
	int offset;

	
	strcpy(filename, "output_absolute_maps_directory/map_");
	strcat(filename,geneName);
	strcat(filename, ".tiff");
	
	if((out=fopen(filename,"w"))==NULL){
		printf("[ERROR] Could not open output file %s. Terminating.\n", filename);
		exit(1);
	}

	// NORMALIZE VALUES TO RANGE 0-255
	min=pixels[0];
	max=pixels[0];
	for(i=0; i<width*height; i++){
		if(pixels[i]<min) min=pixels[i];
		if(pixels[i]>max) max=pixels[i];
	}
	
	if(min<-5) printf("[WARNING] Min pixel value for gene %s (%f) is less than -5!\n", geneName, min);
	if(max>5) printf("[WARNING] Max pixel value for gene %s (%f) is greater than +5!\n", geneName, max);
	min = -5;
	max = 5;
	
	range=max-min;
	for(i=0; i<width*height; i++){
		if(pixels[i]){ // exactly 0 means not in wing disc -> leave unchanged
			if(pixels[i]<-5) pixels[i]=-5; //for using an absolute pixel range
			if(pixels[i]>5) pixels[i]=5; //for using an absolute pixel range
			pixels[i] = (pixels[i]-min)/range*255;
		}
	}


/* Write the header */
   WriteHexString(out,"4d4d002a");    /* Big endian & TIFF identifier */
   offset = width * height * 3 + 8;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* Write the binary data */
   for (i=0;i<height;i++) {
      for (j=0;j<width;j++) {
      	if(pixels[i*width + j]==0){
			fputc(255,out);
			fputc(255,out);
			fputc(255,out);
      	} else {
			pix = pixels[i*width + j];
			fputc(255-pix*235/255,out);
			fputc(255-pix*245/255,out);
			fputc(255-pix*131/255,out);
		}
      }
   }

   /* Write the footer */
   WriteHexString(out,"000e");  /* The number of directory entries (14) */

   /* Width tag, short int */
   WriteHexString(out,"0100000300000001");
   fputc((width & 0xff00) / 256,out);    /* Image width */
   fputc((width & 0x00ff),out);
   WriteHexString(out,"0000");

   /* Height tag, short int */
   WriteHexString(out,"0101000300000001");
   fputc((height & 0xff00) / 256,out);    /* Image height */
   fputc((height & 0x00ff),out);
   WriteHexString(out,"0000");

   /* Bits per sample tag, short int */
   WriteHexString(out,"0102000300000003");
   offset = width * height * 3 + 182;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* Compression flag, short int */
   WriteHexString(out,"010300030000000100010000");

   /* Photometric interpolation tag, short int */
   WriteHexString(out,"010600030000000100020000");

   /* Strip offset tag, long int */
   WriteHexString(out,"011100040000000100000008");

   /* Orientation flag, short int */
   WriteHexString(out,"011200030000000100010000");

   /* Sample per pixel tag, short int */
   WriteHexString(out,"011500030000000100030000");

   /* Rows per strip tag, short int */
   WriteHexString(out,"0116000300000001");
   fputc((height & 0xff00) / 256,out);
   fputc((height & 0x00ff),out);
   WriteHexString(out,"0000");

   /* Strip byte count flag, long int */
   WriteHexString(out,"0117000400000001");
   offset = width * height * 3;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* Minimum sample value flag, short int */
   WriteHexString(out,"0118000300000003");
   offset = width * height * 3 + 188;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* Maximum sample value tag, short int */
   WriteHexString(out,"0119000300000003");
   offset = width * height * 3 + 194;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* Planar configuration tag, short int */
   WriteHexString(out,"011c00030000000100010000");

   /* Sample format tag, short int */
   WriteHexString(out,"0153000300000003");
   offset = width * height * 3 + 200;
   putc((offset & 0xff000000) / 16777216,out);
   putc((offset & 0x00ff0000) / 65536,out);
   putc((offset & 0x0000ff00) / 256,out);
   putc((offset & 0x000000ff),out);

   /* End of the directory entry */
   WriteHexString(out,"00000000");

   /* Bits for each colour channel */
   WriteHexString(out,"000800080008");

   /* Minimum value for each component */
   WriteHexString(out,"000000000000");

   /* Maximum value per channel */
   WriteHexString(out,"00ff00ff00ff");

   /* Samples per pixel for each channel */
   WriteHexString(out,"000100010001");


	fclose(out);
	return;
}
Esempio n. 2
0
void writeIfd0(FILE *fptr, DngWriter *writer)
{
    /* Write the header */
    WriteHexString(fptr,"4d4d002a");    /* Big endian & TIFF identifier */
    offset = writer->rawwidht * writer->rawheight * 3 + 8;
    putc((offset & 0xff000000) / 16777216,fptr);
    putc((offset & 0x00ff0000) / 65536,fptr);
    putc((offset & 0x0000ff00) / 256,fptr);
    putc((offset & 0x000000ff),fptr);

    //raw file ?
    /* Write the binary data */
    for (j=0;j<ny;j++) {
        for (i=0;i<nx;i++) {
            //... calculate the RGB value between 0 and 255 ...
            fputc(red,fptr);
            fputc(green,fptr);
            fputc(blue,fptr);
        }
    }

    /* Write the footer */
    WriteHexString(fptr,"000e");  /* The number of directory entries (14) */

    //Use Tiff Tags for ints added To DngWriter Header file

    WriteHexString(fptr,TagGen(TIFFTAG_SUBFILETYPE,Sshort));  /* The number of directory entries (14) */

    //TIFFSetField (tif, TIFFTAG_SUBFILETYPE, 0);

    /* Width tag, short int */
    WriteHexString(fptr,"0100000300000001");
    fputc((writer->rawwidht & 0xff00) / 256,fptr);    /* Image width */
    fputc((writer->rawwidht & 0x00ff),fptr);
    WriteHexString(fptr,"0000");

    /* Height tag, short int */
    WriteHexString(fptr,"0101000300000001");
    fputc((writer->rawheight & 0xff00) / 256,fptr);    /* Image height */
    fputc((writer->rawheight & 0x00ff),fptr);
    WriteHexString(fptr,"0000");

    LOGD("subfiletype");

    assert(TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, writer->rawwidht) != 0);
    LOGD("width");


    assert(TIFFSetField(tif, TIFFTAG_IMAGELENGTH, writer->rawheight) != 0);
    LOGD("height");


    if(writer->rawType > 0)
        assert(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16) != 0);
    else
        assert(TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 10) != 0);
    LOGD("bitspersample");
    assert(TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA) != 0);
    LOGD("PhotometricCFA");
    //assert(TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 480/2) != 0);
    assert(TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE) != 0);
    LOGD("Compression");
    TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, 1);
    LOGD("sampelsperpixel");
    TIFFSetField(tif, TIFFTAG_MAKE, writer->_make);
    LOGD("make");
    TIFFSetField(tif, TIFFTAG_MODEL, writer->_model);
    LOGD("model");
    try
    {
        if(0 == strcmp(writer->_orientation,"0") )
            TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
        if(0 == strcmp(writer->_orientation,"90") )
            TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_RIGHTTOP);
        if(0 == strcmp(writer->_orientation,"180") )
            TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTRIGHT);
        if(0 == strcmp(writer->_orientation,"270") )
            TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_LEFTBOT);
        LOGD("orientation");
    }
    catch(...)
    {
        LOGD("Caught NULL NOT SET Orientation");
    }
    assert(TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) != 0);
    LOGD("planarconfig");
    //assert(TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3) != 0);
    TIFFSetField(tif, TIFFTAG_SOFTWARE, "FreedCam by Troop");
    LOGD("software");
    TIFFSetField(tif, TIFFTAG_DNGVERSION, "\001\003\0\0");
    TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\001\001\0\0");
    LOGD("dngversion");
    TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, "SonyIMX");
    LOGD("CameraModel");
    TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, writer->_imagedescription);
    LOGD("imagedescription");
    TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, writer->colorMatrix2);
    LOGD("colormatrix1");
    TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, writer->neutralColorMatrix);
    LOGD("neutralMatrix");
    TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);

    TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT2, 17);

    TIFFSetField(tif, TIFFTAG_COLORMATRIX2, 9, writer->colorMatrix1);

    static const float cam_foward1[] = {
            // R 	G     	B
            0.6648, 0.2566, 0.0429, 0.197, 0.9994, -0.1964, -0.0894, -0.2304, 1.145
    };

    static const float cam_foward2[] = {
            0.6617, 0.3849, -0.0823, 0.24, 1.1138, -0.3538, -0.0062, -0.1147, 0.946
    };

    static const float cam_nex_foward1[] = {
            // R 	G     	B
            0.6328, 0.0469, 0.2813, 0.1641, 0.7578, 0.0781, -0.0469, -0.6406, 1.5078
    };

    static const float cam_nex_foward2[] = {
            0.7578, 0.0859, 0.1172, 0.2734, 0.8281, -0.1016, 0.0156, -0.2813, 1.0859
    };
    TIFFSetField(tif, TIFFTAG_FOWARDMATRIX1, 9,  writer->fowardMatrix2);
    TIFFSetField(tif, TIFFTAG_FOWARDMATRIX2, 9,  writer->fowardMatrix1);
    static const float testNR[] = {
            0.00051471, 0, 0.00051471,0, 0.00051471, 0};
    TIFFSetField(tif, TIFFTAG_NOISEPROFILE, 6,  writer->noiseMatrix);



    LOGD("colormatrix2");
    //////////////////////////////IFD POINTERS///////////////////////////////////////
    ///GPS//////////
    // TIFFSetField (tif, TIFFTAG_GPSIFD, gpsIFD_offset);
    ///EXIF////////

}