Beispiel #1
0
int main()
{
	tga_image tga;
	double dct_buf[8][8];
	int i, j, k, l;

	load_tga(&tga, "in.tga");

	k = 0;
	l = (tga.height / 8) * (tga.width / 8);
	for (j=0; j<tga.height/8; j++)
	for (i=0; i<tga.width/8; i++)
	{
		dct(&tga, dct_buf, i*8, j*8);
		quantize(dct_buf);
		idct(&tga, dct_buf, i*8, j*8);
		printf("processed %d/%d blocks.\r", ++k,l);
		fflush(stdout);
	}
	printf("\n");

	DONTFAIL( tga_write_mono("out.tga", tga.image_data,
		tga.width, tga.height) );

	tga_free_buffers(&tga);
	return EXIT_SUCCESS;
}
Beispiel #2
0
bool LoadTarga( CImage32* pImage, const char* pNameFile )
{
	if (!(pImage && pNameFile))
	{
		return false;
	}

	tga_image tgaImage;
	tga_result result = tga_read( &tgaImage, pNameFile );
	if (result != TGA_NOERR)
	{
		return false;
	}

	if (tga_is_colormapped( &tgaImage ))
	{
		tga_color_unmap( &tgaImage );
	}

	UINT width = tgaImage.width;
	UINT height = tgaImage.height;
	UINT bitsPerPixel = tgaImage.pixel_depth;

	pImage->Resize( width, height );

	for (UINT y = 0; y < height; y ++)
	{
		for (UINT x = 0; x < width; x ++)
		{
			BYTE* pPixel = tga_find_pixel( &tgaImage, x, y );
			TARGB color;
			if (bitsPerPixel == 8)
			{
				tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, NULL, NULL, NULL );
				color.G = color.R = color.B;
				color.A = 255;
			}
			else if (bitsPerPixel == 16 || bitsPerPixel == 24)
			{
				tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, &color.G, &color.R, NULL );
				color.A = 255;
			}
			else
			{ // bitsPerPixel == 32
				tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, &color.G, &color.R, &color.A );
			}
			pImage->PixelSet( x, y, color.Value );
		}
	}

	tga_free_buffers( &tgaImage );

	return true;
}
Beispiel #3
0
static RGBSpectrum *ReadImageTGA(const std::string &name, int *width,
                                 int *height) {
    tga_image img;
    tga_result result;
    if ((result = tga_read(&img, name.c_str())) != TGA_NOERR) {
        Error("Unable to read from TGA file \"%s\" (%s)", name.c_str(),
              tga_error(result));
        return nullptr;
    }

    if (tga_is_right_to_left(&img)) tga_flip_horiz(&img);
    if (!tga_is_top_to_bottom(&img)) tga_flip_vert(&img);
    if (tga_is_colormapped(&img)) tga_color_unmap(&img);

    *width = img.width;
    *height = img.height;

    // "Unpack" the pixels (origin in the lower left corner).
    // TGA pixels are in BGRA format.
    RGBSpectrum *ret = new RGBSpectrum[*width * *height];
    RGBSpectrum *dst = ret;
    for (int y = *height - 1; y >= 0; y--)
        for (int x = 0; x < *width; x++) {
            uint8_t *src = tga_find_pixel(&img, x, y);
            if (tga_is_mono(&img))
                *dst++ = RGBSpectrum(*src / 255.f);
            else {
                Float c[3];
                c[2] = src[0] / 255.f;
                c[1] = src[1] / 255.f;
                c[0] = src[2] / 255.f;
                *dst++ = RGBSpectrum::FromRGB(c);
            }
        }

    tga_free_buffers(&img);
    Info("Read TGA image %s (%d x %d)", name.c_str(), *width, *height);

    return ret;
}
Beispiel #4
0
bool ImageFormat_TGA::TestAsset(const Asset& asset)
	{
	// Check if the footer says that this is a TGA file
	if (asset.Open())
		{
		asset.Seek(-18,Asset::SEEK_FROM_END);
		char buffer[16];
		asset.Read(buffer,16);
		asset.Close();
		if (StrNCmp(buffer,"TRUEVISION-XFILE",16)==0)
			{
			return true;
			}
		}

	// If the footer doesn't match, this might still be a tga file (version < 2)
	// so try and load it as a tga and see if it works...
	int size=asset.GetSize();
	char* buffer=new char[size];
	asset.Open();
	asset.Read(buffer,size);
    asset.Close();

	tga_image image;
	tga_read_from_Buffer(&image,buffer);

	delete[] buffer;

	// Check to see if the header data makes sense...
	if (image.width<32767 && image.height<32767 && image.image_data && (image.pixel_depth==8 || image.pixel_depth==24 || image.pixel_depth==16 || image.pixel_depth==32))
		{
		// Yeah, sure, this looks like proper data, so give thumbs up and hope for the best
		return true;
		}

	tga_free_buffers(&image);

	// Nope, not likely to be a TGA
	return false;
	}
Beispiel #5
0
ImageFormat_TGA::~ImageFormat_TGA()
	{
	tga_free_buffers(static_cast<tga_image*>(image_));
	delete static_cast<tga_image*>(image_);
	}
bool loadDepth(const char* pNameFile, ImageBase** ppImage)
{
	if (!pNameFile || !ppImage || *ppImage) return false;

	tga_image tgaImage;
	tga_result result = tga_read(&tgaImage, pNameFile);
	if (result != TGA_NOERR) return false;

	if (tga_is_colormapped(&tgaImage)) {
		tga_color_unmap(&tgaImage);
	}

	unsigned int width = tgaImage.width;
	unsigned int height = tgaImage.height;
	unsigned int bitsPerPixel = tgaImage.pixel_depth;

	if (bitsPerPixel != 32) return false;

	// depth の最大値・最小値を取得
	unsigned long minValueDepth = ULONG_MAX;
	unsigned long maxValueDepth = 0;
	for (unsigned int y = 0; y < tgaImage.height; y ++) {
		for (unsigned int x = 0; x < tgaImage.width; x ++) {
			unsigned char* pPixel = tga_find_pixel(&tgaImage, x, y);
			unsigned char valueR = 0, valueG = 0, valueB = 0, valueA = 0;
			tga_unpack_pixel(pPixel, tgaImage.pixel_depth,
							 &valueB, &valueG, &valueR, &valueA);

			unsigned long valueDepth = ((unsigned int)(valueA) << 24) | ((unsigned int)(valueR) << 16) |
			((unsigned int)(valueG) << 8) | (unsigned int)(valueB);
			if (valueDepth < minValueDepth) minValueDepth = valueDepth;
			if (maxValueDepth < valueDepth) maxValueDepth = valueDepth;
		}
	}
	// 最大値・最小値をもとに正規化
	unsigned char maxValueElement = ElementUtil::getMaxValue<ImageGray8::TypeElement>();
	unsigned char minValueElement = ElementUtil::getMinValue<ImageGray8::TypeElement>();
	ImageGray8 *pImage = ImageFactory::createImage<ImageGray8>(tgaImage.width, tgaImage.height);
	for (unsigned int y = 0; y < tgaImage.height; y ++) {
		for (unsigned int x = 0; x < tgaImage.width; x ++) {
			unsigned char* pPixel = tga_find_pixel(&tgaImage, x, y);
			unsigned char valueR = 0, valueG = 0, valueB = 0, valueA = 0;
			tga_unpack_pixel(pPixel, tgaImage.pixel_depth,
							 &valueB, &valueG, &valueR, &valueA);

			unsigned long valueDepth = ((unsigned int)(valueA) << 24) | ((unsigned int)(valueR) << 16) |
			((unsigned int)(valueG) << 8) | (unsigned int)(valueB);

			unsigned char valueNormalized = cropValue<unsigned char>((unsigned char)(float(maxValueElement) * float(valueDepth - minValueDepth) / float(maxValueDepth - minValueDepth)),
								minValueElement, maxValueElement);
			unsigned char value = maxValueElement - valueNormalized;

			pImage->setPixel(x, y, ImageGray8::TypePixel(value));
		}
	}

	tga_free_buffers(&tgaImage);

	*ppImage = static_cast<ImageBase*>(pImage);

	return pImage ? true : false;
}