Пример #1
0
char *initTiff3DFile ( char *filename, unsigned int sz0, unsigned int sz1, unsigned int sz2, unsigned int sz3, int datatype ) {
//int initTiff3DFile ( char *filename, uint32 XSIZE, uint32 YSIZE, uint16 spp, uint16 Npages, int datatype){

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_START(TiffInitData)
    #endif

	uint32 XSIZE  = sz0;
	uint32 YSIZE  = sz1;
	uint16 Npages = sz2;
	uint16 spp    = sz3;

	uint16 bpp=8 * datatype;
	unsigned char *fakeData=new unsigned char[XSIZE * YSIZE];
	
	int check;

	if ( sz3 == 1 )
		spp = sz3; 
	else if ( sz3 < 4 )
		spp = 3;
	else
		return ((char *) "More than 3 channels in Tiff files.");

	char *completeFilename = (char *) 0;
	int fname_len = (int) strlen(filename);
	char *suffix = strstr(filename,".tif");
	while ( suffix && (fname_len - (suffix-filename) > 5) )
		suffix = strstr(suffix+4,".tif");
	//if ( (suffix != 0) && (fname_len - (suffix-filename) <= 5) ) { // a substring ".tif is already at the end of the filename
	if ( suffix ) { // a substring ".tif is already at the very end of the filename
		completeFilename = new char[fname_len+1];
		strcpy(completeFilename,filename);
	}
	else {	
		completeFilename = new char[fname_len+4+1];
		strcpy(completeFilename,filename);
		strcat(completeFilename,".");
		strcat(completeFilename,TIFF3D_SUFFIX);
	}

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_STOP(TiffInitData, itm::CPU, itm::strprintf("generated fake data for 3D tiff \"%s\"", completeFilename))
    TERAFLY_TIME_RESTART(TiffInitData)
    #endif

	TIFF *output;
	output = TIFFOpen(completeFilename,"w");
	if (!output) {
		return ((char *) "Cannot open the file.");
    }

	check = TIFFSetField(output, TIFFTAG_IMAGEWIDTH, XSIZE);
	if (!check) {
		return ((char *) "Cannot set the image width.");
    }

	check = TIFFSetField(output, TIFFTAG_IMAGELENGTH, YSIZE);
	if (!check) {
		return ((char *) "Cannot set the image width.");
    }

	check = TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, bpp); 
	if (!check) {
		return ((char *) "Cannot set the image width.");
    }

	check = TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, spp);
	if (!check) {
		return ((char *) "Cannot set the image width.");
    }

	check = TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, YSIZE); // one page per strip
	if (!check) {
		return ((char *) "Cannot set the image height.");
    }

	check = TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
	if (!check) {
		return ((char *) "Cannot set the compression tag.");
    }

	check = TIFFSetField(output, TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
	if (!check) {
		return ((char *) "Cannot set the planarconfig tag.");
    }

	if ( spp == 1 )
		check = TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);	
	else // spp == 3
		check = TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);	
	if (!check) {
		return ((char *) "Cannot set the photometric tag.");
    }

	/* We are writing single page of the multipage file */
	check = TIFFSetField(output, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
	if (!check) {
		return ((char *) "Cannot set the subfiletype tag.");
    }

	check = TIFFSetField(output, TIFFTAG_PAGENUMBER, 0, Npages); 
	if (!check) {
		return ((char *) "Cannot set the page number.");
    }


	check = (int)TIFFWriteEncodedStrip(output, 0, fakeData, XSIZE * YSIZE);
	if (!check) {
		return ((char *) "Cannot write encoded strip to file.");
    }

	delete[] fakeData;
	delete []completeFilename;

	check = TIFFWriteDirectory(output);
	if (!check) {
		return ((char *) "Cannot write a new directory.");
    }

	TIFFClose(output);

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_STOP(TiffInitData, itm::IO, itm::strprintf("written initialized 3D tiff \"%s\"", completeFilename))
    #endif

	return (char *) 0;
}
Пример #2
0
char *initTiff3DFile ( char *filename, unsigned int sz0, unsigned int sz1, unsigned int sz2, unsigned int sz3, int datatype ) {
//int initTiff3DFile ( char *filename, uint32 XSIZE, uint32 YSIZE, uint16 spp, uint16 Npages, int datatype){

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_START(TiffInitData)
    #endif

	uint32 XSIZE  = sz0;
	uint32 YSIZE  = sz1;
	uint16 Npages = sz2;
	uint16 spp    = sz3;

	uint16 bpp=8 * datatype;
	unsigned char *fakeData=new unsigned char[XSIZE * YSIZE * spp * (bpp/8)];
	
	int check;
 
	if ( sz3 == 1 )
		spp = sz3; 
	else if ( sz3 < 4 )
		spp = 3;
	else
		return ((char *) "More than 3 channels in Tiff files.");

	char *completeFilename = (char *) 0;
	int fname_len = (int) strlen(filename);
	char *suffix = strstr(filename,".tif");
	while ( suffix && (fname_len - (suffix-filename) > 5) )
		suffix = strstr(suffix+4,".tif");
	//if ( (suffix != 0) && (fname_len - (suffix-filename) <= 5) ) { // a substring ".tif is already at the end of the filename
	if ( suffix ) { // a substring ".tif is already at the very end of the filename
		completeFilename = new char[fname_len+1];
		strcpy(completeFilename,filename);
	}
	else {	
		completeFilename = new char[fname_len+4+1];
		strcpy(completeFilename,filename);
		strcat(completeFilename,".");
		strcat(completeFilename,TIFF3D_SUFFIX);
	}

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_STOP(TiffInitData, tf::CPU, tf::strprintf("generated fake data for 3D tiff \"%s\"", completeFilename))
    TERAFLY_TIME_RESTART(TiffInitData)
    #endif

	//disable warning and error handlers to avoid messages on unrecognized tags
	TIFFSetWarningHandler(0);
	TIFFSetErrorHandler(0);

	TIFF *output;
	output = TIFFOpen(completeFilename,"w");
	if (!output) {
		return ((char *) "Cannot open the file.");
    }

	check = TIFFSetField(output, TIFFTAG_IMAGEWIDTH, XSIZE);
	if (!check) {
		return ((char *) "Cannot set the image width.");
    }

	check = TIFFSetField(output, TIFFTAG_IMAGELENGTH, YSIZE);
	if (!check) {
		return ((char *) "Cannot set the image height.");
    }

	check = TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, bpp); 
	if (!check) {
		return ((char *) "Cannot set the image bit per sample.");
    }

	check = TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, spp);
	if (!check) {
		return ((char *) "Cannot set the image sample per pixel.");
    }

	check = TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, (rowsPerStrip == -1) ? YSIZE : rowsPerStrip); 
	if (!check) {
		return ((char *) "Cannot set the image rows per strip.");
    }

	check = TIFFSetField(output, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); 
	if (!check) {
		return ((char *) "Cannot set the image orientation.");
    }

	check = TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
	if (!check) {
		return ((char *) "Cannot set the compression tag.");
    }

	check = TIFFSetField(output, TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
	if (!check) {
		return ((char *) "Cannot set the planarconfig tag.");
    }

	if ( spp == 1 )
		check = TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);	
	else // spp == 3
		check = TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);	
	if (!check) {
		return ((char *) "Cannot set the photometric tag.");
    }

	/* We are writing single page of the multipage file */
	check = TIFFSetField(output, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
	if (!check) {
		return ((char *) "Cannot set the subfiletype tag.");
    }

	check = TIFFSetField(output, TIFFTAG_PAGENUMBER, 0, Npages); 
	if (!check) {
		return ((char *) "Cannot set the page number.");
    }


	check = (int)TIFFWriteEncodedStrip(output, 0, fakeData, XSIZE * YSIZE);
	if (!check) {
		return ((char *) "Cannot write encoded strip to file.");
    }

	if ( rowsPerStrip == -1 ) 
		TIFFWriteEncodedStrip(output, 0, fakeData, XSIZE * YSIZE * spp * (bpp/8));
	else { 
		int check,StripsPerImage,LastStripSize;
		uint32 rps = rowsPerStrip;
		unsigned char *buf = fakeData;

		StripsPerImage =  (YSIZE + rps - 1) / rps;
		LastStripSize = YSIZE % rps;
		if (LastStripSize==0)
			LastStripSize=rps;

		for (int i=0; i < StripsPerImage-1; i++){
			//if (comp==1) {
			//	TIFFReadRawStrip(input, i, buf, spp * rps * img_width * (bpp/8));
			//	buf = buf + spp * rps * img_width * (bpp/8);
			//}
			//else{
				TIFFWriteEncodedStrip(output, i, buf, spp * rps * XSIZE * (bpp/8));
				buf = buf + spp * rps * XSIZE * (bpp/8);
			//}
		}

		//if (comp==1) {
		//	TIFFReadRawStrip(input, StripsPerImage-1, buf, spp * LastStripSize * img_width * (bpp/8));
		//}
		//else{
			TIFFWriteEncodedStrip(output, StripsPerImage-1, buf, spp * LastStripSize * XSIZE * (bpp/8));
		//}
		buf = buf + spp * LastStripSize * XSIZE * (bpp/8);
	}

	delete[] fakeData;
	delete []completeFilename;

	check = TIFFWriteDirectory(output);
	if (!check) {
		return ((char *) "Cannot write a new directory.");
    }

	TIFFClose(output);

    // 2015-01-30. Alessandro. @ADDED performance (time) measurement in all most time-consuming methods.
    #ifdef _VAA3D_TERAFLY_PLUGIN_MODE
    TERAFLY_TIME_STOP(TiffInitData, tf::IO, tf::strprintf("written initialized 3D tiff \"%s\"", completeFilename))
    #endif

	return (char *) 0;
}