Ejemplo n.º 1
0
static int
cpStrips(TIFF* in, TIFF* out)
{
    tsize_t bufsize  = TIFFStripSize(in);
    unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

    if (buf) {
	tstrip_t s, ns = TIFFNumberOfStrips(in);
	tsize_t *bytecounts;

	TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
	for (s = 0; s < ns; s++) {
	    if (bytecounts[s] > bufsize) {
		buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[s]);
		if (!buf)
		    goto bad;
		bufsize = bytecounts[s];
	    }
	    if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 ||
		TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) {
		_TIFFfree(buf);
		return 0;
	    }
	}
	_TIFFfree(buf);
	return 1;
    }

bad:
	TIFFError(TIFFFileName(in),
		  "Can't allocate space for strip buffer.");
	return 0;
}
Ejemplo n.º 2
0
static int
cpStrips(TIFF* in, TIFF* out)
{
	tsize_t bufsize  = TIFFStripSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		tstrip_t s, ns = TIFFNumberOfStrips(in);
		uint32 *bytecounts;

		if (!TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts)) {
			fprintf(stderr, "tiffsplit: strip byte counts are missing\n");
			return (0);
		}
		for (s = 0; s < ns; s++) {
			if (bytecounts[s] > (uint32)bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, bytecounts[s]);
				if (!buf)
					return (0);
				bufsize = bytecounts[s];
			}
			if (TIFFReadRawStrip(in, s, buf, bytecounts[s]) < 0 ||
			    TIFFWriteRawStrip(out, s, buf, bytecounts[s]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
Ejemplo n.º 3
0
static int
cpStrips(TIFF* in, TIFF* out)
{
	tmsize_t bufsize  = TIFFStripSize(in);
	unsigned char *buf = (unsigned char *)_TIFFmalloc(bufsize);

	if (buf) {
		tstrip_t s, ns = TIFFNumberOfStrips(in);
		uint64 *bytecounts;

		TIFFGetField(in, TIFFTAG_STRIPBYTECOUNTS, &bytecounts);
		for (s = 0; s < ns; s++) {
			if (bytecounts[s] > (uint64)bufsize) {
				buf = (unsigned char *)_TIFFrealloc(buf, (tmsize_t)bytecounts[s]);
				if (!buf)
					return (0);
				bufsize = (tmsize_t)bytecounts[s];
			}
			if (TIFFReadRawStrip(in, s, buf, (tmsize_t)bytecounts[s]) < 0 ||
			    TIFFWriteRawStrip(out, s, buf, (tmsize_t)bytecounts[s]) < 0) {
				_TIFFfree(buf);
				return (0);
			}
		}
		_TIFFfree(buf);
		return (1);
	}
	return (0);
}
Ejemplo n.º 4
0
void tifgetstripsdata(Mytiff * mytiff,char * buf){
/* buf is already allocated */
   int i;
   char * bstart;

   bstart=buf;
   for (i=0;i<mytiff->stripnum;i++){
      TIFFReadRawStrip(mytiff->tiffp,i,bstart,mytiff->stripsize);
      bstart+=mytiff->stripsize;
   }
#ifdef FIX_SLITS
   {
      int slitrows;
      int thisrow;
      int startrow;
      slitrows=SLITROWS;
      startrow=mytiff->ht - slitrows;

      for (i=0;i<slitrows;i++){
         thisrow=startrow+i;
         //printf("thisrow %i startrow %i\n",thisrow,startrow);
         memcpy(buf+(thisrow)*(mytiff->wd * mytiff->bytes), buf+startrow*(mytiff->wd * mytiff->bytes) ,mytiff->wd * mytiff->bytes);
      }
   }
#endif /*FIX_SLITS */
}
Ejemplo n.º 5
0
/****************** Will hopefully remove this function. ***********************
 * Read image strips and set global variablels.
 *	- NUMBER_OF_ROWS / IMAGE_LENGTH
 *	- NUMBER_OF_COLS / IMAGE_WIDTH
 *	- IMAGE
 */
void stripIO(TIFF * tif)
{
	uint16 config;
	TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
	
	int stripSize, stripMax;
	stripSize = TIFFStripSize (tif);
	stripMax  = TIFFNumberOfStrips (tif);
	
	int bytesPerStrip;
	TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bytesPerStrip);
	
	printf("stripSize : %d  stripMax : %d bytesPerStrip : %d\n", stripSize, stripMax, bytesPerStrip);
	
	unsigned long bufferSize;
	bufferSize = stripSize * stripMax;
	
	char * buffer;
	if((buffer = (char *) _TIFFmalloc(bufferSize)) == NULL){
	    puts("Not enough memory to allocate buffer.");
	    exit(42);
	}
	
	int stripCount, result;
	unsigned long imageOffset = 0;
	for(stripCount = 0; stripCount < stripMax; stripCount++)
	{
	    if((result = TIFFReadRawStrip(tif, stripCount, buffer + imageOffset, stripSize)) == -1){
		    puts("Read error on input strip.");
	    }
	}
	
	int i;
	for(stripCount = 0; stripCount < stripMax; stripCount++){
	    for(i = 0; i < stripSize; i++){
		    //printf("%d ", atoi(&buffer[stripCount*stripSize + stripSize]));
	    }
	    //printf("\n");
	}
	
	_TIFFfree(buffer);
}
Ejemplo n.º 6
0
char *readTiff3DFile2Buffer ( void *fhandler, unsigned char *img, unsigned int img_width, unsigned int img_height, unsigned int first, unsigned int last, int b_swap ) {
	uint32 rps;
    uint16 spp, bpp, photo, comp, planar_config;
    int check, StripsPerImage,LastStripSize;

    TIFF *input = (TIFF *) fhandler;

	check=TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rps);
	if (!check)
	{
		return ((char *) "Image length of undefined.");
	}	
	//rps=600;
    
	check=TIFFGetField(input, TIFFTAG_BITSPERSAMPLE, &bpp); 
	if (!check)
	{
		return ((char *) "Undefined bits per sample.");
	}

	check=TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &spp);
	if (!check)
	{
		return ((char *) "Undefined samples per pixel.");
	}

	check=TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &photo);
	if (!check)
	{
		return ((char *) "Cannot determine photometric interpretation.");
	}

	check=TIFFGetField(input, TIFFTAG_COMPRESSION, &comp);
	if (!check)
	{
		return ((char *) "Cannot determine compression technique.");
	}

	check=TIFFGetField(input, TIFFTAG_PLANARCONFIG, &planar_config);
	if (!check)
	{
		return ((char *) "Cannot determine planar configuration.");
	}


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

	check=TIFFSetDirectory(input, first);
	if (!check)
	{
		return ((char *) "Cannot open the requested first strip.");
	}

	unsigned char *buf = img;
	int page=0;
	do{

		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{
				TIFFReadEncodedStrip(input, i, buf, spp * rps * img_width * (bpp/8));
				buf = buf + spp * rps * img_width * (bpp/8);
			}
		}

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

		page++;
	
	}while ( page < static_cast<int>(last-first+1) && TIFFReadDirectory(input));//while (TIFFReadDirectory(input));

	// input file is assumedo ti be already open and it is provided as an handler; the file should be closed by caller
	//TIFFClose(input);  

	if ( page < static_cast<int>(last-first+1) ){
		return ((char *) "Cannot read all the pages.");
	}

	// swap the data bytes if necessary 	
	if (b_swap)
	{
		int i;
		size_t total = img_width * img_height * spp * (last-first+1);
		if (bpp/8 == 2)
		{
			for (i=0;i<total; i++)
			{
				swap2bytes((void *)(img+2*i));
			}
		}
		else if (bpp/8 == 4)
		{
			for (i=0;i<total; i++)
			{
				swap4bytes((void *)(img+4*i));
			}
		}
	}

	return (char *) 0;
}
Ejemplo n.º 7
0
char *readTiff3DFile2Buffer ( void *fhandler, unsigned char *img, unsigned int img_width, unsigned int img_height, unsigned int first, unsigned int last, 
																		   int b_swap, int downsamplingFactor, int starti, int endi, int startj, int endj ) {
	uint32 rps;
    uint16 spp, bpp, orientation, photo, comp, planar_config;
    int check, StripsPerImage,LastStripSize;
	uint32 XSIZE;
	uint32 YSIZE;

    TIFF *input = (TIFF *) fhandler;

	check=TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rps);
	if (!check)
	{
		return ((char *) "Undefined rows per strip.");
	}	
    
	//check=TIFFGetField(input, TIFFTAG_ORIENTATION, &orientation); 
	//if (!check)
	//{
	//	return ((char *) "Image orientation undefined.");
	//}	
    
	check=TIFFGetField(input, TIFFTAG_BITSPERSAMPLE, &bpp); 
	if (!check)
	{
		return ((char *) "Undefined bits per sample.");
	}

	check=TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &spp);
	if (!check)
	{
		return ((char *) "Undefined samples per pixel.");
	}

	check=TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &photo);
	if (!check)
	{
		return ((char *) "Cannot determine photometric interpretation.");
	}

	check=TIFFGetField(input, TIFFTAG_COMPRESSION, &comp);
	if (!check)
	{
		return ((char *) "Cannot determine compression technique.");
	}

	check=TIFFGetField(input, TIFFTAG_PLANARCONFIG, &planar_config);
	if (!check)
	{
		return ((char *) "Cannot determine planar configuration.");
	}

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

	unsigned char *buf = img;
	int page=0;

	if ( downsamplingFactor == 1 ) { // read without downsampling

		starti = (starti == -1) ? 0 : starti;
		endi   = (endi == -1) ? img_height-1 : endi;
		startj = (startj == -1) ? 0 : startj;
		endj   = (endj == -1) ? img_width-1 : endj;

		if ( starti < 0 || endi >= img_height || startj < 0 || endj >= img_width || starti >= endi || startj >= endj )
		{
			return ((char *) "Wrong substack indices.");
		}

		if ( starti == 0 && endi == (img_height-1) && startj == 0 && endj == (img_width-1) ) { // read whole images from files 

			check=TIFFSetDirectory(input, first);
			if (!check)
			{
				return ((char *) "Cannot open the requested first strip.");
			}

			do{

				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{
						TIFFReadEncodedStrip(input, i, buf, spp * rps * img_width * (bpp/8));
						buf = buf + spp * rps * img_width * (bpp/8);
					}
				}

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

				page++;
	
			}while ( page < static_cast<int>(last-first+1) && TIFFReadDirectory(input));//while (TIFFReadDirectory(input));

		}
		else { // read only a subregion of images from files

			check=TIFFGetField(input, TIFFTAG_IMAGEWIDTH, &XSIZE);
			if (!check)
			{
				return ((char *) "Image width of undefined.");
			}		    
			check=TIFFGetField(input, TIFFTAG_IMAGELENGTH, &YSIZE);
			if (!check)
			{
				return ((char *) "Image length of undefined.");
			}

			unsigned char *rowbuf = new unsigned char[spp * rps * XSIZE * (bpp/8)];
			unsigned char *bufptr;

			do{
				check=TIFFSetDirectory(input, first + page);
				if (!check)
				{
					return ((char *) "Cannot open next requested strip.");
				}

				int stripIndex = (starti / rps) - 1; // the strip preceeding the first one
				for (int i=starti; i <= endi; i++) {
					if ( floor(i / rps) > stripIndex ) { // read a new strip
						stripIndex = (int)floor(i / rps);
						if (comp==1) {
							TIFFReadRawStrip(input, stripIndex, rowbuf, spp * ((stripIndex < StripsPerImage) ? rps :LastStripSize) * XSIZE * (bpp/8));
						}
						else{
							TIFFReadEncodedStrip(input, stripIndex, rowbuf, spp * ((stripIndex < StripsPerImage) ? rps :LastStripSize) * XSIZE * (bpp/8));
						}
					}
					bufptr = rowbuf + (i % rps) * (spp * XSIZE * (bpp/8));
					if ( bpp == 8 )
						for (int j=0, j1=startj; j<=(endj-startj); j++, j1++) {
							for (int c=0; c<spp; c++) {
								buf[j * spp + c] = bufptr[j1 * spp + c];
							}
						}
					else
						for (int j=0 , j1=startj; j<=(endj-startj); j++, j1++) {
							for (int c=0; c<spp; c++) {
								((uint16 *)buf)[j * spp + c] = ((uint16 *)bufptr)[j1 * spp + c];
							}
						}
					buf = buf + spp * (endj-startj+1) * (bpp/8);
				}

				page++;

			}while ( page < static_cast<int>(last-first+1) );

			delete []rowbuf;

		}

		// input file is assumedo ti be already open and it is provided as an handler; the file should be closed by caller
		//TIFFClose(input); 

		if ( page < static_cast<int>(last-first+1) ){
			return ((char *) "Cannot read all the pages.");
		}
	}
	else { // read with downsampling

		// preliminary checks
		check=TIFFGetField(input, TIFFTAG_IMAGEWIDTH, &XSIZE);
		if (!check)
		{
			return ((char *) "Image width of undefined.");
		}		    
		check=TIFFGetField(input, TIFFTAG_IMAGELENGTH, &YSIZE);
		if (!check)
		{
			return ((char *) "Image length of undefined.");
		}

		if ( (int)ceil((double)XSIZE/downsamplingFactor) < img_width ) 
		{
			return ((char *) "Requested image width too large.");
		}
		if ( (int)ceil((double)YSIZE/downsamplingFactor) < img_height ) 
		{
			return ((char *) "Requested image height too large.");
		}

		unsigned char *rowbuf = new unsigned char[spp * rps * XSIZE * (bpp/8)];
		unsigned char *bufptr;

		do{
			check=TIFFSetDirectory(input, ((first + page) * downsamplingFactor));
			if (!check)
			{
				return ((char *) "Cannot open next requested strip.");
			}

			int stripIndex = -1; // the strip preceeding the first one
			for (int i=0; i < img_height; i++) {
				if ( floor(i * downsamplingFactor / (double)rps) > stripIndex ) { // read a new strip
					stripIndex = (int)floor(i * downsamplingFactor / (double)rps);
					if (comp==1) {
						TIFFReadRawStrip(input, stripIndex, rowbuf, spp * ((stripIndex < StripsPerImage) ? rps :LastStripSize) * XSIZE * (bpp/8));
					}
					else{
						TIFFReadEncodedStrip(input, stripIndex, rowbuf, spp * ((stripIndex < StripsPerImage) ? rps :LastStripSize) * XSIZE * (bpp/8));
					}
				}
				bufptr = rowbuf + ((i * downsamplingFactor) % rps) * (spp * XSIZE * (bpp/8));
				if ( bpp == 8 )
					for (int j=0; j<img_width; j++) {
						for (int c=0; c<spp; c++) {
							buf[j * spp + c] = bufptr[j * spp * downsamplingFactor + c];
						}
					}
				else
					for (int j=0; j<img_width; j++) {
						for (int c=0; c<spp; c++) {
							((uint16 *)buf)[j * spp + c] = ((uint16 *)bufptr)[j * spp * downsamplingFactor + c];
						}
					}
				buf = buf + spp * img_width * (bpp/8);
			}

			page++;

		}while ( page < static_cast<int>(last-first+1) );

		delete []rowbuf;
	}
    
	// swap the data bytes if necessary 	
	if (b_swap)
	{
		int i;
		size_t total = img_width * img_height * spp * (last-first+1);
		if (bpp/8 == 2)
		{
			for (i=0;i<total; i++)
			{
                iim::swap2bytes((void *)(img+2*i));
			}
		}
		else if (bpp/8 == 4)
		{
			for (i=0;i<total; i++)
			{
                iim::swap4bytes((void *)(img+4*i));
			}
		}
	}

	return (char *) 0;
}
Ejemplo n.º 8
0
int
PS_Lvl2page(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
	uint16 fillorder;
	int use_rawdata, tiled_image, breaklen;
	uint32 chunk_no, num_chunks, *bc;
	unsigned char *buf_data, *cp;
	tsize_t chunk_size, byte_count;

#if defined( EXP_ASCII85ENCODER )
	int			ascii85_l;		/* Length, in bytes, of ascii85_p[] data */
	uint8		*	ascii85_p = 0;		/* Holds ASCII85 encoded data */
#endif

	PS_Lvl2colorspace(fd, tif);
	use_rawdata = PS_Lvl2ImageDict(fd, tif, w, h);

	fputs("%%BeginData:\n", fd);
	fputs("exec\n", fd);

	tiled_image = TIFFIsTiled(tif);
	if (tiled_image) {
		num_chunks = TIFFNumberOfTiles(tif);
		TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);
	} else {
		num_chunks = TIFFNumberOfStrips(tif);
		TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
	}

	if (use_rawdata) {
		chunk_size = bc[0];
		for (chunk_no = 1; chunk_no < num_chunks; chunk_no++)
			if (bc[chunk_no] > chunk_size)
				chunk_size = bc[chunk_no];
	} else {
		if (tiled_image)
			chunk_size = TIFFTileSize(tif);
		else
			chunk_size = TIFFStripSize(tif);
	}
	buf_data = (unsigned char *)_TIFFmalloc(chunk_size);
	if (!buf_data) {
		TIFFError(filename, "Can't alloc %u bytes for %s.",
			chunk_size, tiled_image ? "tiles" : "strips");
		return(FALSE);
	}

#if defined( EXP_ASCII85ENCODER )
	if ( ascii85 ) {
	    /*
	     * Allocate a buffer to hold the ASCII85 encoded data.  Note
	     * that it is allocated with sufficient room to hold the
	     * encoded data (5*chunk_size/4) plus the EOD marker (+8)
	     * and formatting line breaks.  The line breaks are more
	     * than taken care of by using 6*chunk_size/4 rather than
	     * 5*chunk_size/4.
	     */

	    ascii85_p = _TIFFmalloc( (chunk_size+(chunk_size/2)) + 8 );

	    if ( !ascii85_p ) {
		_TIFFfree( buf_data );

		TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );
		return ( FALSE );
	    }
	}
#endif

	TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
	for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {
		if (ascii85)
			Ascii85Init();
		else
			breaklen = 36;
		if (use_rawdata) {
			if (tiled_image)
				byte_count = TIFFReadRawTile(tif, chunk_no,
						  buf_data, chunk_size);
			else
				byte_count = TIFFReadRawStrip(tif, chunk_no,
						  buf_data, chunk_size);
			if (fillorder == FILLORDER_LSB2MSB)
			    TIFFReverseBits(buf_data, byte_count);
		} else {
			if (tiled_image)
				byte_count = TIFFReadEncodedTile(tif,
						chunk_no, buf_data,
						chunk_size);
			else
				byte_count = TIFFReadEncodedStrip(tif,
						chunk_no, buf_data,
						chunk_size);
		}
		if (byte_count < 0) {
			TIFFError(filename, "Can't read %s %d.",
				tiled_image ? "tile" : "strip", chunk_no);
			if (ascii85)
				Ascii85Put('\0', fd);
		}

		if (ascii85) {
#if defined( EXP_ASCII85ENCODER )
			ascii85_l = Ascii85EncodeBlock(ascii85_p, 1, buf_data, byte_count );

			if ( ascii85_l > 0 )
				fwrite( ascii85_p, ascii85_l, 1, fd );
#else
			for (cp = buf_data; byte_count > 0; byte_count--)
				Ascii85Put(*cp++, fd);
#endif
		}
		else
		{
			for (cp = buf_data; byte_count > 0; byte_count--) {
				putc(hex[((*cp)>>4)&0xf], fd);
				putc(hex[(*cp)&0xf], fd);
				cp++;

				if (--breaklen <= 0) {
					putc('\n', fd);
					breaklen = 36;
				}
			}
		}

		if ( !ascii85 ) {
			if ( level2 )
				putc( '>', fd );
			putc('\n', fd);
		}
#if !defined( EXP_ASCII85ENCODER )
		else
			Ascii85Flush(fd);
#endif
	}

#if defined( EXP_ASCII85ENCODER )
	if ( ascii85_p )
	    _TIFFfree( ascii85_p );
#endif
       
	_TIFFfree(buf_data);
	fputs("%%EndData\n", fd);
	return(TRUE);
}
Ejemplo n.º 9
0
void
PSRawDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
{
	uint32 *bc;
	uint32 bufsize;
	int breaklen = MAXLINE, cc;
	uint16 fillorder;
	unsigned char *tf_buf;
	unsigned char *cp, c;
	tstrip_t s;

#if defined( EXP_ASCII85ENCODER )
	int			ascii85_l;		/* Length, in bytes, of ascii85_p[] data */
	uint8		*	ascii85_p = 0;		/* Holds ASCII85 encoded data */
#endif

	(void) w; (void) h;
	TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
	TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);

	/*
	 * Find largest strip:
	 */

	bufsize = bc[0];

	for ( s = 0; ++s < tf_numberstrips; ) {
		if ( bc[s] > bufsize )
			bufsize = bc[s];
	}

	tf_buf = (unsigned char*) _TIFFmalloc(bufsize);
	if (tf_buf == NULL) {
		TIFFError(filename, "No space for strip buffer");
		return;
	}

#if defined( EXP_ASCII85ENCODER )
	if ( ascii85 ) {
	    /*
	     * Allocate a buffer to hold the ASCII85 encoded data.  Note
	     * that it is allocated with sufficient room to hold the
	     * encoded data (5*bufsize/4) plus the EOD marker (+8)
	     * and formatting line breaks.  The line breaks are more
	     * than taken care of by using 6*bufsize/4 rather than
	     * 5*bufsize/4.
	     */

	    ascii85_p = _TIFFmalloc( (bufsize+(bufsize/2)) + 8 );

	    if ( !ascii85_p ) {
		_TIFFfree( tf_buf );

		TIFFError( filename, "Cannot allocate ASCII85 encoding buffer." );
		return;
	    }
	}
#endif

	for (s = 0; s < tf_numberstrips; s++) {
		cc = TIFFReadRawStrip(tif, s, tf_buf, bc[s]);
		if (cc < 0) {
			TIFFError(filename, "Can't read strip");
			break;
		}
		if (fillorder == FILLORDER_LSB2MSB)
			TIFFReverseBits(tf_buf, cc);
		if (!ascii85) {
			for (cp = tf_buf; cc > 0; cc--) {
				DOBREAK(breaklen, 1, fd);
				c = *cp++;
				PUTHEX(c, fd);
			}
			fputs(">\n", fd);
			breaklen = MAXLINE;
		} else {
		        Ascii85Init();
#if defined( EXP_ASCII85ENCODER )
			ascii85_l = Ascii85EncodeBlock( ascii85_p, 1, tf_buf, cc );

			if ( ascii85_l > 0 )
				fwrite( ascii85_p, ascii85_l, 1, fd );
#else
			for (cp = tf_buf; cc > 0; cc--)
				Ascii85Put(*cp++, fd);
			Ascii85Flush(fd);
#endif	/* EXP_ASCII85ENCODER */
		}
	}
	_TIFFfree((char *) tf_buf);

#if defined( EXP_ASCII85ENCODER )
	if ( ascii85_p )
		_TIFFfree( ascii85_p );
#endif
}
Ejemplo n.º 10
0
int fm_MITIFF_read(char *infile, unsigned char *image[], 
    fmio_mihead *ginfo) {
    
    char *where="MITIFF_read";
    TIFF *in;
    int i, status, size;
    short pmi;
    unsigned int fieldlen, currlen, nextlen, taglen;
    char *description, *o_description;
    char *currfield, *nextfield, *field, *pt;
    char *o_currfield, *o_nextfield, *o_field;
    char *fieldname[FMIO_FIELDS]={
	"Satellite:", 
	"Date and Time:", 
	"SatDir:", 
	"Channels:", 
	"In this file:", 
	"Xsize:", 
	"Ysize:", 
	"Map projection:", 
	"TrueLat:", 
	"GridRot:", 
	"Xunit:", 
	"Yunit:", 
	"NPX:", 
	"NPY:", 
	"Ax:", 
	"Ay:", 
	"Bx:", 
	"By:", 
	"Calibration"
    };

    /*
     * Open TIFF files and initialize IFD
     */
    
    in=TIFFOpen(infile, "rc");
    if (!in) {
	printf(" This is no TIFF file! \n");
	return(FM_IO_ERR);
    }

    /*
     * Test whether this is a color palette image or not. If so another
     * function should be used.
     */
    status = TIFFGetField(in, 262, &pmi);
    if (pmi == 3) {
	return(FM_IO_ERR);
    }

    description = (char *) malloc(1024*sizeof(char));
    if (!description) fmerrmsg(where,"Memory allocation failed");
    o_description = description;
    TIFFGetField(in, 270, &description);
    currfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char));
    if (!currfield) fmerrmsg(where,"Memory allocation failed"); 
    o_currfield = currfield;
    nextfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char));
    if (!nextfield) fmerrmsg(where,"Memory allocation failed"); 
    o_nextfield = nextfield;
    for (i=0; i<FMIO_FIELDS-1; i++) {
	pt = strstr(description, fieldname[i]);
	sprintf(currfield, "%s", pt);
	currlen = strlen(currfield);
	pt = strstr(description, fieldname[i+1]);
	sprintf(nextfield, "%s", pt);
	nextlen = strlen(nextfield);
	taglen = strlen(fieldname[i]);
	fieldlen = currlen-nextlen-taglen;
	field = (char *) calloc(fieldlen+1, sizeof(char));
	if (!field) fmerrmsg(where,"Memory allocation failed");
	o_field = field;
	currfield += taglen;
	strncpy(field, currfield, fieldlen);
	fillhead(field, fieldname[i], ginfo);
	free(o_field);
    }
    free(o_currfield);
    free(o_nextfield);
    free(o_description); 
    
    /*
     * Read image data into matrix.
     */
    TIFFGetField(in, 256, &ginfo->xsize);
    TIFFGetField(in, 257, &ginfo->ysize);
    size = ginfo->xsize*ginfo->ysize;
  
    /*
     * Memory allocated for image data in this function (*image) is freed 
     * in function main process.
     */
    if (ginfo->zsize > FMIO_MAXCHANNELS) {
	printf("\n\tNOT ENOUGH POINTERS AVAILABLE TO HOLD DATA!\n");
	return(FM_IO_ERR);
    }
    for (i=0; i<ginfo->zsize; i++) {
	image[i] = (unsigned char *) malloc((size+1)*sizeof(char));
	if (!image[i]) fmerrmsg(where,"Memory allocation failed");
	status = TIFFReadRawStrip(in, 0, image[i], size);
	if (status == -1) return(FM_IO_ERR);
	if (TIFFReadDirectory(in) == 0) break;
    }

    if (ginfo->zsize != (i+1)) {
	printf("\n\tERROR READING MULTIPLE SUBFILES!\n");
	return(FM_IO_ERR);
    }
    
    TIFFClose(in);
    return(FM_OK);
}
Ejemplo n.º 11
0
/*
 * PURPOSE:
 * To read DNMI/TIFF palette color files containing either classed satellite
 * imagery or radar imagery.
 *
 * RETURN VALUES:
 * 0 - Normal and correct ending
 * 2 - This is not a Palette-color image
 *
 * NOTE:
 * Requires access to libtiff.
 *
 * AUTHOR:
 * Øystein Godøy, DNMI/FOU, 21/07/1999
 * MODIFICATION:
 * Øystein Godøy, DNMI/FOU, 27/03/2001
 * Corrected some memory allocation and freeing problems connected to
 * strtok actually changing the string it operates on.
 */
int fm_MITIFF_read_imagepal(char *infile, unsigned char *image[], 
    fmio_mihead *ginfo, fmio_mihead_pal *palinfo) {
    
    char *where="MITIFF_read_imagepal";
    TIFF *in;
    int i, status, size;
    short pmi;
    unsigned int fieldlen, currlen, nextlen, taglen;
    uint16 *red, *green, *blue;
    char *description, *o_description;
    char *currfield, *nextfield, *field, *pt;
    char *o_currfield, *o_nextfield, *o_field;
    char *fieldname[FMIO_FIELDS]={
	"Satellite:", 
	"Date and Time:", 
	"SatDir:", 
	"Channels:", 
	"In this file:", 
	"Xsize:", 
	"Ysize:", 
	"Map projection:", 
	"TrueLat:", 
	"GridRot:", 
	"Xunit:", 
	"Yunit:", 
	"NPX:", 
	"NPY:", 
	"Ax:", 
	"Ay:", 
	"Bx:", 
	"By:", 
	"COLOR INFO:"
    };

    /*
     * Open TIFF files and initialize IFD
     */
    
    in=TIFFOpen(infile, "rc");
    if (!in) {
	printf(" This is no TIFF file! \n");
	return(FM_IO_ERR);
    }

    /*
     * Test whether this is a color palette image or not. If so another
     * function should be used.
     */
    status = TIFFGetField(in, 262, &pmi);
    if (pmi != 3) {
	return(FM_IO_ERR);
    }

    status = TIFFGetField(in, 320, &red, &green, &blue);
    if (status != 1) {
	return(FM_IO_ERR);
    }
    for (i=0; i<256; i++) {
	palinfo->cmap[0][i] = red[i];
	palinfo->cmap[1][i] = green[i];
	palinfo->cmap[2][i] = blue[i];
    }

    description = (char *) malloc(FMIO_TIFFHEAD*sizeof(char));
    if (!description) fmerrmsg(where,"Memory allocation failed"); 
    o_description = description;
    TIFFGetField(in, 270, &description);
    /*
     * Lead through the filed tags defined, except for the last one which will
     * create a segmentation fault if it is used. This is processed after the
     * loop.
     */
    currfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char));
    if (!currfield) fmerrmsg(where,"Memory allocation failed"); 
    o_currfield = currfield;
    nextfield = (char *) malloc(FMIO_TIFFHEAD*sizeof(char));
    if (!nextfield) fmerrmsg(where,"Memory allocation failed"); 
    o_nextfield = nextfield;
    for (i=0; i<FMIO_FIELDS-1; i++) {
	pt = strstr(description, fieldname[i]);
	sprintf(currfield, "%s", pt);
	currlen = strlen(currfield);
	pt = strstr(description, fieldname[i+1]);
	sprintf(nextfield, "%s", pt);
	nextlen = strlen(nextfield);
	taglen = strlen(fieldname[i]);
	fieldlen = currlen-nextlen-taglen;
	field = (char *) malloc((fieldlen+1)*sizeof(char));
	if (!field) fmerrmsg(where,"Memory allocation failed");
	o_field = field;
	currfield += taglen;
	strncpy(field, currfield, fieldlen);
	fillhead(field, fieldname[i], ginfo);
	free(o_field);
    }
    /*
     * The last part of the information header is treated as one single string
     * and is extracted as the remaining part and processed in a suitable way
     * later...
     */
    pt = strstr(description, fieldname[FMIO_FIELDS-1]);
    sprintf(currfield, "%s", pt);
    currlen = strlen(currfield);
    nextlen = strlen(description);
    taglen = strlen(fieldname[FMIO_FIELDS-1]);
    /*
    fieldlen = nextlen-currlen-taglen;
    field = (char *) malloc((fieldlen+1)*sizeof(char));
    */
    fieldlen = nextlen-currlen-taglen;
    field = (char *) malloc((currlen+1)*sizeof(char));
    if (!field) fmerrmsg(where,"Memory allocation failed");
    o_field = field;
    /*
     * Beware here, in order to help C keep track of which memory to free
     * later, currfield should be reduced by taglen imediately or better a new
     * work string should be used, but for now this solution is chosen...
     */
    currfield += taglen;
    /*
    strncpy(field, currfield, fieldlen);
    */
    strncpy(field, currfield, currlen);
    currfield -= taglen;
    /*
    printf(" %d-%d-%d-%d\n",currlen,nextlen,taglen,fieldlen);
    printf("%s\n\n%s\n",currfield, field);
    printf("%d - %d\n",strlen(currfield),strlen(field));
    */
    fillhead_imagepal(field, fieldname[FMIO_FIELDS-1], palinfo); 
    free(o_field);
    free(o_currfield);
    free(o_nextfield);
    free(o_description);
    /*
     * Read image data into matrix.
     */
    TIFFGetField(in, 256, &ginfo->xsize);
    TIFFGetField(in, 257, &ginfo->ysize);
    size = ginfo->xsize*ginfo->ysize;
  
    /*
     * Memory allocated for image data in this function (*image) is freed 
     * in function main process.
     */
    if (ginfo->zsize > FMIO_MAXCHANNELS) {
	printf("\n\tNOT ENOUGH POINTERS AVAILABLE TO HOLD DATA!\n");
	return(FM_IO_ERR);
    }
    for (i=0; i<ginfo->zsize; i++) {
	image[i] = (unsigned char *) malloc((size+1)*sizeof(char));
	if (!image[i]) fmerrmsg(where,"Memory allocation failed");
	status = TIFFReadRawStrip(in, 0, *image, size);
	if (status == -1) return(FM_IO_ERR);
	if (TIFFReadDirectory(in) == 0) break;
    }

    if (ginfo->zsize != (i+1)) {
	printf("\n\tERROR READING MULTIPLE SUBFILES!\n");
	return(FM_IO_ERR);
    }
    
    TIFFClose(in);
    return(FM_OK);
}