Example #1
0
void TifReader::open(FILE *file)
{
	int fd = fileno(file);
#if 0
	m_tiff = TIFFFdOpenNoCloseProc(fd, "", "rb");
#else
	m_tiff = TIFFFdOpen(dup(fd), "", "rb");
#endif
	if (!m_tiff) {
		std::string str("Tiff file closed");
		throw(str);
	}

	uint32 w = 0, h = 0, rps = 0;
	uint16 bps = 0, spp = 0;
	uint32 tileWidth = 0, tileLength = 0;

	//TIFFSetDirectory(m_tiff,1);
	//TIFFGetField(m_tiff, TIFFTAG_PAGENUMBER, &pn);
	//int pn = TIFFNumberOfDirectories(m_tiff);
	//TIFFSetDirectory(m_tiff,1);
	TIFFGetField(m_tiff, TIFFTAG_IMAGEWIDTH, &w);
	TIFFGetField(m_tiff, TIFFTAG_IMAGELENGTH, &h);
	TIFFGetField(m_tiff, TIFFTAG_BITSPERSAMPLE, &bps);
	TIFFGetField(m_tiff, TIFFTAG_SAMPLESPERPIXEL, &spp);
	TIFFGetField(m_tiff, TIFFTAG_ROWSPERSTRIP, &rps);
	//int stripCount = TIFFNumberOfStrips(m_tiff);
	//int tileCount = TIFFNumberOfTiles(m_tiff);
	TIFFGetField(m_tiff, TIFFTAG_TILEWIDTH, &tileWidth);
	TIFFGetField(m_tiff, TIFFTAG_TILELENGTH, &tileLength);
	Tiio::TifWriterProperties *prop = new Tiio::TifWriterProperties();
	m_info.m_properties = prop;
	uint16 orient = Tiio::TOP2BOTTOM;
	if (TIFFGetField(m_tiff, TIFFTAG_ORIENTATION, &orient)) {
		switch (orient) {
		case ORIENTATION_TOPLEFT: /* row 0 top, col 0 lhs */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_TOPLEFT);
			m_rowOrder = Tiio::TOP2BOTTOM;
			break;
		case ORIENTATION_TOPRIGHT: /* row 0 top, col 0 rhs */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_TOPRIGHT);
			m_rowOrder = Tiio::TOP2BOTTOM;
			break;
		case ORIENTATION_LEFTTOP: /* row 0 lhs, col 0 top */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_LEFTTOP);
			m_rowOrder = Tiio::TOP2BOTTOM;
			break;
		case ORIENTATION_RIGHTTOP: /* row 0 rhs, col 0 top */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_RIGHTTOP);
			m_rowOrder = Tiio::TOP2BOTTOM;
			break;

		case ORIENTATION_BOTRIGHT: /* row 0 bottom, col 0 rhs */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_BOTRIGHT);
			m_rowOrder = Tiio::BOTTOM2TOP;
			break;
		case ORIENTATION_BOTLEFT: /* row 0 bottom, col 0 lhs */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_BOTLEFT);
			m_rowOrder = Tiio::BOTTOM2TOP;
			break;
		case ORIENTATION_RIGHTBOT: /* row 0 rhs, col 0 bottom */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_RIGHTBOT);
			m_rowOrder = Tiio::BOTTOM2TOP;
			break;
		case ORIENTATION_LEFTBOT: /* row 0 lhs, col 0 bottom */
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_LEFTBOT);
			m_rowOrder = Tiio::BOTTOM2TOP;
			break;
		default:
			prop->m_orientation.setValue(TNZ_INFO_ORIENT_NONE);
			m_rowOrder = Tiio::TOP2BOTTOM;
			break;
		}
	}

	USHORT compression;
	TIFFGetField(m_tiff, TIFFTAG_COMPRESSION, &compression);
	switch (compression) {
	case COMPRESSION_LZW:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_LZW);
		break;
	case COMPRESSION_PACKBITS:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_PACKBITS);
		break;
	case COMPRESSION_THUNDERSCAN:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_THUNDERSCAN);
		break;
	case COMPRESSION_CCITTFAX3:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTFAX3);
		break;
	case COMPRESSION_CCITTFAX4:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTFAX4);
		break;
	case COMPRESSION_CCITTRLE:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_CCITTRLE);
		break;
	case COMPRESSION_JPEG:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_JPEG);
		break;
	case COMPRESSION_OJPEG:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_OJPEG);
		break;
	case COMPRESSION_NONE:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_NONE);
		break;
	case COMPRESSION_SGILOG:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_SGILOG);
		break;
	case COMPRESSION_SGILOG24:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_SGILOG24);
		break;
	case COMPRESSION_ADOBE_DEFLATE:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_ADOBE_DEFLATE);
		break;
	case COMPRESSION_DEFLATE:
		prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_DEFLATE);
		break;
	/*default :
      prop->m_compressionType.setValue(TNZ_INFO_COMPRESS_UNKNOWN);
      break;*/
	default:
		assert(0);
	}

	float xdpi = 0, ydpi = 0;
	TIFFGetField(m_tiff, TIFFTAG_XRESOLUTION, &xdpi);
	TIFFGetField(m_tiff, TIFFTAG_YRESOLUTION, &ydpi);

	bool swapxy = false; // orient == ORIENTATION_RIGHTTOP;

	if (swapxy) {
		tswap(w, h);
		tswap(xdpi, ydpi);
	}

	m_xdpi = xdpi;
	m_ydpi = ydpi;
	m_info.m_lx = w;
	m_info.m_ly = h;
	m_info.m_dpix = xdpi;
	m_info.m_dpiy = ydpi;

	m_info.m_samplePerPixel = spp;

	if (bps == 64 && spp == 3)
		bps = 16; //immagine con bpp = 192

	uint16 photometric; //codice di controllo
	TIFFGetField(m_tiff, TIFFTAG_PHOTOMETRIC, &photometric);
	if (photometric == 3 && (bps == 2 || bps == 4)) //immagini con PHOTOMATRIC_PALETTE
		bps = 8;

	if (photometric == 1 && (bps == 12 || bps == 24))
		bps = 16;

	if (bps == 6)
		bps = 4; //immagini con bps = 6

	if (bps == 10 || bps == 12 || bps == 14) //immagini con bps = 10 , 12 , 14 , 24 , 32
		bps = 8;
	if (bps == 24 || bps == 32)
		bps = 16;

	m_info.m_bitsPerSample = bps;

	if (bps == 8)
		switch (spp) {
		case 1: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L" 8(GREYTONES)");
			break;
		case 3: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"24(RGB)");
			break;
		case 4: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		}
	else if (bps == 16)
		switch (spp) {
		case 1: /* row 0 top, col 0 lhs */
			//prop->m_bitsPerPixel.setValue(L"16(GREYTONES)");
			break;
		case 3: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"48(RGB)");
			break;
		case 4: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"64(RGBM)");
			break;
		}

	else if (bps == 2)
		switch (spp) {
		case 1: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		case 3: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		case 4: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		}

	else if (bps == 1)
		switch (spp) {
		case 1: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L" 1(BW)");
			break;
		case 3: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		case 4: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		}

	else if (bps == 4)
		switch (spp) {
		case 1: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L" 8(GREYTONES)");
			break;
		case 3: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		case 4: /* row 0 top, col 0 lhs */
			prop->m_bitsPerPixel.setValue(L"32(RGBM)");
			break;
		}

	else if (bps == 64 && spp == 3)
		prop->m_bitsPerPixel.setValue(L"64(RGBM)");

	else
		assert(false);

	if (TIFFIsTiled(m_tiff)) {
		m_rowsPerStrip = tileLength;
		int tilesPerRow = (w + tileWidth - 1) / tileWidth;
		//m_rowLength = tileWidth * tilesPerRow;
		m_rowLength = m_info.m_lx;
		int pixelSize = bps == 16 ? 8 : 4;
		int stripSize = m_rowsPerStrip * m_rowLength * pixelSize;

		m_tmpRas = TRasterGR8P(stripSize, 1);
		m_tmpRas->lock();

		m_stripBuffer = m_tmpRas->getRawData();
	} else {
		m_rowsPerStrip = rps;
		//if(m_rowsPerStrip<=0) m_rowsPerStrip = 1;			//potrei mettere qualsiasi valore
		//purchè sia lo stesso in tif_getimage.c linea 2512
		//if(m_rowsPerStrip==-1) assert(0);

		if (m_rowsPerStrip <= 0)
			m_rowsPerStrip = m_info.m_ly;

		int stripSize = m_rowsPerStrip * w * 4; // + 4096;  TIFFStripSize(m_tiff);

		if (bps == 16)
			stripSize *= 2;

		m_tmpRas = TRasterGR8P(stripSize, 1);
		m_tmpRas->lock();

		m_stripBuffer = m_tmpRas->getRawData();

		m_rowLength = m_info.m_lx; //w;
	}

	/* 
  int TIFFTileRowSize(m_tiff);

  m_rowsPerStrip = 0;
  if(TIFFGetField(m_tiff, TIFFTAG_ROWSPERSTRIP, &rps) )
    {
     int stripSize = TIFFStripSize(m_tiff);
     if(stripSize>0)
       {
       }
    }
    */
	if (m_isTzi) {
		USHORT risCount = 0;
		USHORT *risArray = 0;

		if (TIFFGetField(m_tiff, TIFFTAG_TOONZWINDOW, &risCount, &risArray)) {
			if (m_info.m_lx == risArray[2] && m_info.m_ly == risArray[3]) //se sono diverse, la lettura tif crasha....
			{
				//m_info.m_lx = risArray[2];
				//m_info.m_ly = risArray[3];
				m_info.m_x0 = risArray[0];
				m_info.m_y0 = risArray[1];
			}
			//      USHORT extraMask = risArray[4];
			//bool isEduFile = risArray[TOONZWINDOW_COUNT - 1] & 1;
		} else {
			m_info.m_x0 = 0;
			m_info.m_y0 = 0;
		}
		if (swapxy) {
			tswap(m_info.m_x0, m_info.m_y0);
			tswap(m_info.m_lx, m_info.m_ly);
		}
		m_info.m_x1 = m_info.m_x0 + w;
		m_info.m_y1 = m_info.m_y0 + h;
	} else {
		m_info.m_x0 = m_info.m_y0 = 0;
		m_info.m_x1 = m_info.m_x0 + m_info.m_lx - 1;
		m_info.m_y1 = m_info.m_y0 + m_info.m_ly - 1;
	}
}
void FreeDistortBaseFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri)
{
	if (!m_input.isConnected())
		return;

	//Upon deactivation, this fx does nothing.
	if (m_deactivate->getValue()) {
		m_input->compute(tile, frame, ri);
		return;
	}

	//Get the source quad
	TPointD p00_b = m_p00_b->getValue(frame);
	TPointD p10_b = m_p10_b->getValue(frame);
	TPointD p01_b = m_p01_b->getValue(frame);
	TPointD p11_b = m_p11_b->getValue(frame);

	//Get destination quad
	TPointD p00_a = m_p00_a->getValue(frame);
	TPointD p10_a = m_p10_a->getValue(frame);
	TPointD p01_a = m_p01_a->getValue(frame);
	TPointD p11_a = m_p11_a->getValue(frame);

	if (m_isCastShadow) {
		//Shadows are mirrored
		tswap(p00_a, p01_a);
		tswap(p10_a, p11_a);
	}

	//Get requested tile's geometry
	TRasterP tileRas(tile.getRaster());
	TRectD tileRect(convert(tileRas->getBounds()) + tile.m_pos);

	//Call transform to get the minimal rectOnInput
	TRectD inRect;
	TRenderSettings riNew;
	TRectD inBBox;

	safeTransform(frame, 0, tileRect, ri, inRect, riNew, inBBox);

	//Intersect with the bbox
	inRect *= inBBox;

	if (myIsEmpty(inRect))
		return;

	double scale = ri.m_affine.a11;

	double downBlur = m_downBlur->getValue(frame) * scale;
	double upBlur = m_upBlur->getValue(frame) * scale;
	int brad = tceil(tmax(downBlur, upBlur));

	inRect = inRect.enlarge(brad);

	TDimension inRectSize(tceil(inRect.getLx()), tceil(inRect.getLy()));

	TTile inTile;
	m_input->allocateAndCompute(inTile, inRect.getP00(), inRectSize, tileRas, frame, riNew);

	TPointD inTilePosRi = inTile.m_pos;

	//Update quads by the scale factors
	p00_b = riNew.m_affine * p00_b;
	p10_b = riNew.m_affine * p10_b;
	p01_b = riNew.m_affine * p01_b;
	p11_b = riNew.m_affine * p11_b;

	p00_a = ri.m_affine * p00_a;
	p10_a = ri.m_affine * p10_a;
	p01_a = ri.m_affine * p01_a;
	p11_a = ri.m_affine * p11_a;

	PerspectiveDistorter perpDistorter(
		p00_b - inTile.m_pos, p10_b - inTile.m_pos, p01_b - inTile.m_pos, p11_b - inTile.m_pos,
		p00_a, p10_a, p01_a, p11_a);

	BilinearDistorter bilDistorter(
		p00_b - inTile.m_pos, p10_b - inTile.m_pos, p01_b - inTile.m_pos, p11_b - inTile.m_pos,
		p00_a, p10_a, p01_a, p11_a);

	TQuadDistorter *distorter;
	if (m_distortType->getValue() == PERSPECTIVE)
		distorter = &perpDistorter;
	else if (m_distortType->getValue() == BILINEAR)
		distorter = &bilDistorter;
	else
		assert(0);

	if (m_isCastShadow) {
		TRaster32P ras32 = inTile.getRaster();
		TRaster64P ras64 = inTile.getRaster();

		if (ras32) {
			if (m_fade->getValue(frame) > 0)
				doFade(ras32, m_color->getValue(frame), m_fade->getValue(frame) / 100.0);
			if (brad > 0)
				doBlur(ras32, upBlur, downBlur,
					   m_upTransp->getValue(frame) / 100.0, m_downTransp->getValue(frame) / 100.0,
					   inBBox.y0 - inTile.m_pos.y, inBBox.y1 - inTile.m_pos.y);
			else if (m_upTransp->getValue(frame) > 0 || m_downTransp->getValue(frame) > 0)
				doTransparency(ras32, m_upTransp->getValue(frame) / 100.0, m_downTransp->getValue(frame) / 100.0,
							   inBBox.y0 - inTile.m_pos.y, inBBox.y1 - inTile.m_pos.y);
		} else if (ras64) {
			if (m_fade->getValue(frame) > 0)
				doFade(ras64, toPixel64(m_color->getValue(frame)), m_fade->getValue(frame) / 100.0);
			if (brad > 0)
				doBlur(ras64, upBlur, downBlur,
					   m_upTransp->getValue(frame) / 100.0, m_downTransp->getValue(frame) / 100.0,
					   inBBox.y0 - inTile.m_pos.y, inBBox.y1 - inTile.m_pos.y);
			else if (m_upTransp->getValue(frame) > 0 || m_downTransp->getValue(frame) > 0)
				doTransparency(ras64, m_upTransp->getValue(frame) / 100.0, m_downTransp->getValue(frame) / 100.0,
							   inBBox.y0 - inTile.m_pos.y, inBBox.y1 - inTile.m_pos.y);
		} else
			assert(false);
	}

	distort(tileRas, inTile.getRaster(), *distorter, convert(tile.m_pos), TRop::Bilinear);
}