Beispiel #1
0
void testSavers2(ILenum type, const TCHAR* targetName, const TCHAR* targetExt)
{
	TCHAR targetFN[MAX_PATH];

	// Test ilSave
	_tcscpy(targetFN, targetName);
	_tcscat(targetFN, L".ilSave.");
	_tcscat(targetFN, targetExt);
	if (!ilSave(type, targetFN)) {
		printf("testSavers2: Failed to save " PathCharMod " using ilSave\n", targetFN);
		++errors;
	}

	// Test ilSaveF
	_tcscpy(targetFN, targetName);
	_tcscat(targetFN, L".ilSaveF.");
	_tcscat(targetFN, targetExt);
	FILE* file = _wfopen(targetFN, L"wb");
	if (!ilSaveF(type, file)) {
		printf("testSavers2: Failed to save " PathCharMod " using ilSaveF\n", targetFN);
		++errors;
	}
	fclose(file);

	// Test ilSaveL
	_tcscpy(targetFN, targetName);
	_tcscat(targetFN, L"ilSaveL.");
	_tcscat(targetFN, targetExt);
	ILuint lumpSize = ilDetermineSize(type);
	BYTE* lump = new BYTE[lumpSize];
	ILuint writtenToLump = ilSaveL(type, lump, lumpSize);
	if (writtenToLump > 0) {
		FILE* file = _wfopen(targetFN, L"wb");
		size_t writtenToFile = fwrite(lump, 1, lumpSize, file);
		if (writtenToLump != writtenToFile) {
			printf("testSavers2: Failed to write " PathCharMod " after ilSaveL\n", targetFN);
			++errors;
		}
		fclose(file);
	} else {
		printf("testSavers2: Failed to save " PathCharMod " using ilSaveL\n", targetFN);
		++errors;
	}
	delete lump;

	// Test ilSaveFuncs
	wcscpy(targetFN, targetName);
	wcscat(targetFN, L".ilSaveFuncs.");
	wcscat(targetFN, targetExt);
	writeFile = _wfopen(targetFN, L"wb");
	if (writeFile != NULL) {
		ilSetWrite(NULL, NULL, myPutc, mySeek, myTell, myWrite);
		if (!ilSaveFuncs(type))
			printf("testSavers2: Failed to save " PathCharMod " using ilSave\n", targetFN);
		fclose(writeFile);
	} else
		printf("testSavers2: Failed to open " PathCharMod " for writing\n", targetFN);
}
Beispiel #2
0
	void ImageData::encode(love::filesystem::File * f, ImageData::Format format) {
		Lock lock(devilMutex);
		Lock lock2(mutex);

		ILuint tempimage;
		ilGenImages(1, &tempimage);
		ilBindImage(tempimage);

		while(ilGetError() != IL_NO_ERROR);

		bool success = ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, this->data) == IL_TRUE;

		ILenum err = ilGetError();
		while(ilGetError() != IL_NO_ERROR);

		if (!success)
		{
			ilDeleteImages(1, &tempimage);

			if (err != IL_NO_ERROR)
			{
				switch (err)
				{
					case IL_ILLEGAL_OPERATION:
						throw love::Exception("Illegal operation");
					case IL_INVALID_PARAM:
						throw love::Exception("Invalid parameters");
					case IL_OUT_OF_MEMORY:
						throw love::Exception("Out of memory");
					default:
						throw love::Exception("Unknown error (%d)", (int) err);
				}
			}

			throw love::Exception("Could not create image for the encoding!");
		}

		ilRegisterOrigin(IL_ORIGIN_UPPER_LEFT);

		ILuint ilFormat;
		switch (format)
		{
			case ImageData::FORMAT_BMP:
				ilFormat = IL_BMP;
				break;
			case ImageData::FORMAT_TGA:
				ilFormat = IL_TGA;
				break;
			case ImageData::FORMAT_GIF:
				ilFormat = IL_GIF;
				break;
			case ImageData::FORMAT_JPG:
				ilFormat = IL_JPG;
				break;
			case ImageData::FORMAT_PNG:
			default: // PNG is the default format
				ilFormat = IL_PNG;
				break;
		}

		ILuint size = ilSaveL(ilFormat, NULL, 0);
		if (!size)
		{
			ilDeleteImages(1, &tempimage);
			throw love::Exception("Could not encode image!");
		}

		ILubyte * encoded_data;
		try
		{
			encoded_data = new ILubyte[size];
		}
		catch(std::bad_alloc)
		{
			ilDeleteImages(1, &tempimage);
			throw love::Exception("Out of memory");
		}

		ilSaveL(ilFormat, encoded_data, size);
		ilDeleteImages(1, &tempimage);

		f->open(love::filesystem::File::WRITE);
		f->write(encoded_data, size);
		f->close();

		delete[] encoded_data;
	}
void CDevILCodec::CodeToBuffer(CMemoryBuffer & out, const CImage &image,
							   ESaveFormats ext)
{
	ILuint imageid;
	CDevILFormats informat;
	informat.SetExFormat(image.GetPixelFormat());
	// Generate the main image name to use.
	ilGenImages(1, &imageid);

	// Bind this image name.
	ilBindImage(imageid);

	ilTexImage(image.GetWidth(), image.GetHeight(), image.GetDepth(), informat.GetInternalChannels(),
		informat.GetFormat(), IL_UNSIGNED_BYTE, image.GetBitsPtr());

	ILenum type = 0;
	switch(ext)
	{
	case SF_BMP:
		type = IL_BMP;
		break;
	case SF_ICO:
		type = IL_ICO;
		break;
	case SF_JPG:
		type = IL_JPG;
		break;
	case SF_PCX:
		type = IL_PCX;
		break;
	case SF_PIC:
		type = IL_PIC;
		break;
	case SF_PNG:
		type = IL_PNG;
		break;
	case SF_TGA:
		type = IL_TGA;
		break;
	case SF_TIF:
		type = IL_TIF;
		break;
	case SF_GIF:
		type = IL_GIF;
		break;
	case SF_DDS:
		type = IL_DDS;
		break;
	case SF_PIX:
		type = IL_PIX;
		break;
	case SF_HDR:
		type = IL_HDR;
		break;

	default:
		return;
	}

	out.AllocBuffer(image.GetSize()+0xff);
	ilSaveL(type, out.GetBegin(), out.GetBufferSize());

	ilDeleteImages(1, &imageid);

	ILenum Error = 0;
	if((Error = ilGetError()) != NULL)
	{
		nstring str("CDevILCodec::CodeToStream: ");
		str.append(iluErrorString(Error));
		throw NOVA_EXP(str.c_str(), BAD_OPERATION);
	}
}