コード例 #1
0
ファイル: ilu_rotate.c プロジェクト: kphillisjr/DevIL
ILboolean ILAPIENTRY iluRotate(ILfloat Angle)
{
	ILimage	*Temp, *Temp1, *CurImage = NULL;
	ILenum	PalType = 0;

	iluCurImage = ilGetCurImage();
	if (iluCurImage == NULL) {
		ilSetError(ILU_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (iluCurImage->Format == IL_COLOUR_INDEX) {
		PalType = iluCurImage->Pal.PalType;
		CurImage = iluCurImage;
		iluCurImage = iConvertImage(iluCurImage, ilGetPalBaseType(CurImage->Pal.PalType), IL_UNSIGNED_BYTE);
	}

	Temp = iluRotate_(iluCurImage, Angle);
	if (Temp != NULL) {
		if (PalType != 0) {
			ilCloseImage(iluCurImage);
			Temp1 = iConvertImage(Temp, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
			ilCloseImage(Temp);
			Temp = Temp1;
			ilSetCurImage(CurImage);
		}
		ilTexImage(Temp->Width, Temp->Height, Temp->Depth, Temp->Bpp, Temp->Format, Temp->Type, Temp->Data);
		if (PalType != 0) {
			iluCurImage = ilGetCurImage();
			iluCurImage->Pal.PalSize = Temp->Pal.PalSize;
			iluCurImage->Pal.PalType = Temp->Pal.PalType;
			iluCurImage->Pal.Palette = (ILubyte*)ialloc(Temp->Pal.PalSize);
			if (iluCurImage->Pal.Palette == NULL) {
				ilCloseImage(Temp);
				return IL_FALSE;
			}
			memcpy(iluCurImage->Pal.Palette, Temp->Pal.Palette, Temp->Pal.PalSize);
		}

		iluCurImage->Origin = Temp->Origin;
		ilCloseImage(Temp);
		return IL_TRUE;
	}
	return IL_FALSE;
}
コード例 #2
0
ファイル: ilu_scale.c プロジェクト: lucienslliu/Dproject
ILAPI ILimage* ILAPIENTRY iluScale_(ILimage *Image, ILuint Width, ILuint Height, ILuint Depth)
{
	ILimage	*Scaled, *CurImage, *ToScale;
	ILenum	Format, PalType;

	CurImage = ilGetCurImage();
	Format = Image->Format;
	if (Format == IL_COLOUR_INDEX) {
		ilSetCurImage(Image);
		PalType = Image->Pal.PalType;
		ToScale = iConvertImage(iluCurImage, ilGetPalBaseType(Image->Pal.PalType), iluCurImage->Type);
	}
	else {
		ToScale = Image;
	}

	// So we don't replicate this 3 times (one in each iluScalexD_() function.
	Scaled = (ILimage*)icalloc(1, sizeof(ILimage));
	if (ilCopyImageAttr(Scaled, ToScale) == IL_FALSE) {
		ilCloseImage(Scaled);
		if (ToScale != Image)
			ilCloseImage(ToScale);
		ilSetCurImage(CurImage);
		return NULL;
	}
	if (ilResizeImage(Scaled, Width, Height, Depth, ToScale->Bpp, ToScale->Bpc) == IL_FALSE) {
		ilCloseImage(Scaled);
		if (ToScale != Image)
			ilCloseImage(ToScale);
		ilSetCurImage(CurImage);
		return NULL;
	}
	
	if (Height <= 1 && Image->Height <= 1) {
		iluScale1D_(ToScale, Scaled, Width);
	}
	if (Depth <= 1 && Image->Depth <= 1) {
		iluScale2D_(ToScale, Scaled, Width, Height);
	}
	else {
		iluScale3D_(ToScale, Scaled, Width, Height, Depth);
	}

	if (Format == IL_COLOUR_INDEX) {
		//ilSetCurImage(Scaled);
		//ilConvertImage(IL_COLOUR_INDEX);
		ilSetCurImage(CurImage);
		ilCloseImage(ToScale);
	}

	return Scaled;
}
コード例 #3
0
ILimage *MakeD3D8Compliant(IDirect3DDevice8 *Device, D3DFORMAT *DestFormat)
{
	ILimage	*Converted, *Scaled, *CurImage;

	*DestFormat = D3DFMT_A8R8G8B8;

	// Images must be in BGRA format.
	if (ilutCurImage->Format != IL_BGRA) {
		Converted = iConvertImage(ilutCurImage, IL_BGRA, IL_UNSIGNED_BYTE);
		if (Converted == NULL)
			return NULL;
	}
	else {
		Converted = ilutCurImage;
	}

	// Images must have their origin in the upper left.
	if (Converted->Origin != IL_ORIGIN_UPPER_LEFT) {
		CurImage = ilutCurImage;
		ilSetCurImage(Converted);
		iluFlipImage();
		ilSetCurImage(CurImage);
	}

	// Images must have powers-of-2 dimensions.
	if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
		ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
		ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
			Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
						ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
			if (Converted != ilutCurImage) {
				ilCloseImage(Converted);
			}
			if (Scaled == NULL) {
				return NULL;
			}
			Converted = Scaled;
	}

	return Converted;
}
コード例 #4
0
ファイル: il_jpeg.c プロジェクト: ProfessorKaos64/tdm
ILboolean ilSaveFromJpegStruct(ILvoid *_JpegInfo)

{

#ifndef IL_NO_JPG

#ifndef IL_USE_IJL

	void (*errorHandler)(j_common_ptr);

	JSAMPROW	row_pointer[1];

	ILimage		*TempImage;

	ILubyte		*TempData;

	j_compress_ptr JpegInfo = (j_compress_ptr)_JpegInfo;



	if (iCurImage == NULL) {

		ilSetError(IL_ILLEGAL_OPERATION);

		return IL_FALSE;

	}



	//added on 2003-08-31 as explained in sf bug 596793

	jpgErrorOccured = IL_FALSE;



	errorHandler = JpegInfo->err->error_exit;

	JpegInfo->err->error_exit = ExitErrorHandle;





	if ((iCurImage->Format != IL_RGB && iCurImage->Format != IL_LUMINANCE) || iCurImage->Bpc != 1) {

		TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);

		if (TempImage == NULL) {

			return IL_FALSE;

		}

	}

	else {

		TempImage = iCurImage;

	}



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT) {

		TempData = iGetFlipped(TempImage);

		if (TempData == NULL) {

			if (TempImage != iCurImage)

				ilCloseImage(TempImage);

			return IL_FALSE;

		}

	}

	else {

		TempData = TempImage->Data;

	}



	JpegInfo->image_width = TempImage->Width;  // image width and height, in pixels

	JpegInfo->image_height = TempImage->Height;

	JpegInfo->input_components = TempImage->Bpp;  // # of color components per pixel



	jpeg_start_compress(JpegInfo, IL_TRUE);



	//row_stride = image_width * 3;	// JSAMPLEs per row in image_buffer



	while (JpegInfo->next_scanline < JpegInfo->image_height) {

		// jpeg_write_scanlines expects an array of pointers to scanlines.

		// Here the array is only one element long, but you could pass

		// more than one scanline at a time if that's more convenient.

		row_pointer[0] = &TempData[JpegInfo->next_scanline * TempImage->Bps];

		(void) jpeg_write_scanlines(JpegInfo, row_pointer, 1);

	}



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT)

		ifree(TempData);

	if (TempImage != iCurImage)

		ilCloseImage(TempImage);



	return (!jpgErrorOccured);

#endif

#endif

	return IL_FALSE;

}
コード例 #5
0
ファイル: il_jpeg.c プロジェクト: ProfessorKaos64/tdm
ILboolean iSaveJpegInternal(ILstring FileName, ILvoid *Lump, ILuint Size)

{

	JPEG_CORE_PROPERTIES	Image;

	ILuint	Quality;

	ILimage	*TempImage;

	ILubyte	*TempData;



	imemclear(&Image, sizeof(JPEG_CORE_PROPERTIES));



	if (iCurImage == NULL) {

		ilSetError(IL_ILLEGAL_OPERATION);

		return IL_FALSE;

	}

	if (FileName == NULL && Lump == NULL) {

		ilSetError(IL_INVALID_PARAM);

		return IL_FALSE;

	}



	if (iGetHint(IL_COMPRESSION_HINT) == IL_USE_COMPRESSION)

		Quality = 85;  // Not sure how low we should dare go...

	else

		Quality = 99;



	if (ijlInit(&Image) != IJL_OK) {

		ilSetError(IL_LIB_JPEG_ERROR);

		return IL_FALSE;

	}



	if ((iCurImage->Format != IL_RGB && iCurImage->Format != IL_RGBA && iCurImage->Format != IL_LUMINANCE)

		|| iCurImage->Bpc != 1) {

		if (iCurImage->Format == IL_BGRA)

			Temp = iConvertImage(iCurImage, IL_RGBA, IL_UNSIGNED_BYTE);

		else

			Temp = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);

		if (Temp == NULL) {

			return IL_FALSE;

		}

	}

	else {

		Temp = iCurImage;

	}



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT) {

		TempData = iGetFlipped(TempImage);

		if (TempData == NULL) {

			if (TempImage != iCurImage)

				ilCloseImage(TempImage);

			return IL_FALSE;

		}

	}

	else {

		TempData = TempImage->Data;

	}



	// Setup DIB

	Image.DIBWidth		= TempImage->Width;

	Image.DIBHeight		= TempImage->Height;

	Image.DIBChannels	= TempImage->Bpp;

	Image.DIBBytes		= TempData;

	Image.DIBPadBytes	= 0;



	// Setup JPEG

	Image.JPGWidth		= TempImage->Width;

	Image.JPGHeight		= TempImage->Height;

	Image.JPGChannels	= TempImage->Bpp;



	switch (Temp->Bpp)

	{

		case 1:

			Image.DIBColor			= IJL_G;

			Image.JPGColor			= IJL_G;

			Image.JPGSubsampling	= IJL_NONE;

			break;

		case 3:

			Image.DIBColor			= IJL_RGB;

			Image.JPGColor			= IJL_YCBCR;

			Image.JPGSubsampling	= IJL_411;

			break;

		case 4:

			Image.DIBColor			= IJL_RGBA_FPX;

			Image.JPGColor			= IJL_YCBCRA_FPX;

			Image.JPGSubsampling	= IJL_4114;

			break;

	}



	if (FileName != NULL) {

		Image.JPGFile = FileName;

		if (ijlWrite(&Image, IJL_JFILE_WRITEWHOLEIMAGE) != IJL_OK) {

			if (TempImage != iCurImage)

				ilCloseImage(TempImage);

			ilSetError(IL_LIB_JPEG_ERROR);

			return IL_FALSE;

		}

	}

	else {

		Image.JPGBytes = Lump;

		Image.JPGSizeBytes = Size;

		if (ijlWrite(&Image, IJL_JBUFF_WRITEWHOLEIMAGE) != IJL_OK) {

			if (TempImage != iCurImage)

				ilCloseImage(TempImage);

			ilSetError(IL_LIB_JPEG_ERROR);

			return IL_FALSE;

		}

	}



	ijlFree(&Image);



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT)

		ifree(TempData);

	if (Temp != iCurImage)

		ilCloseImage(Temp);



	return IL_TRUE;

}
コード例 #6
0
ファイル: il_header.cpp プロジェクト: KFreon/KFreon_s-ResIL
//! Generates a C-style header file for the current image.
ILboolean ilSaveCHeader(ILimage* image, char *InternalName)
{
	FILE		*HeadFile = NULL;
	ILuint		i = 0, j;
	ILimage		*TempImage;
	const char	*Name;

	if (image == NULL) {
		il2SetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	Name = iGetString(IL_CHEAD_HEADER_STRING);
	if (Name == NULL)
		Name = InternalName;

	if (image->Bpc > 1) {
		TempImage = iConvertImage(image, image->Format, IL_UNSIGNED_BYTE);
		if (TempImage == NULL)
           return IL_FALSE;
	} else {
		TempImage = image;
	}

	FILE* file = (FILE*) image->io.handle;

	fprintf(HeadFile, "//#include <il/il.h>\n");
	fprintf(HeadFile, "// C Image Header:\n\n\n");
	fprintf(HeadFile, "// IMAGE_BPP is in bytes per pixel, *not* bits\n");
	fprintf(HeadFile, "#define IMAGE_BPP %d\n",image->Bpp);
	fprintf(HeadFile, "#define IMAGE_WIDTH   %d\n", image->Width);
	fprintf(HeadFile, "#define IMAGE_HEIGHT  %d\n", image->Height);	
	fprintf(HeadFile, "#define IMAGE_DEPTH   %d\n\n\n", image->Depth);
	fprintf(HeadFile, "#define IMAGE_TYPE    0x%X\n", image->Type);
	fprintf(HeadFile, "#define IMAGE_FORMAT  0x%X\n\n\n", image->Format);
	fprintf(HeadFile, "ILubyte %s[] = {\n", Name);
        

	for (; i < TempImage->SizeOfData; i += MAX_LINE_WIDTH) {
		fprintf(HeadFile, "\t");
		for (j = 0; j < MAX_LINE_WIDTH; j++) {
			if (i + j >= TempImage->SizeOfData - 1) {
				fprintf(HeadFile, "%4d", TempImage->Data[i+j]);
				break;
			}
			else
				fprintf(HeadFile, "%4d,", TempImage->Data[i+j]);
		}
		fprintf(HeadFile, "\n");
	}
	if (TempImage != image)
		ilCloseImage(TempImage);

	fprintf(HeadFile, "};\n");


	if (image->Pal.hasPalette()) {
		fprintf(HeadFile, "\n\n");
		fprintf(HeadFile, "#define IMAGE_PALSIZE %u\n\n", image->Pal.getPalSize());
		fprintf(HeadFile, "#define IMAGE_PALTYPE 0x%X\n\n", image->Pal.getPalType());
        fprintf(HeadFile, "ILubyte %sPal[] = {\n", Name);
		for (i = 0; i < image->Pal.getPalSize(); i += MAX_LINE_WIDTH) {
			fprintf(HeadFile, "\t");
			for (j = 0; j < MAX_LINE_WIDTH; j++) {
				if (i + j >= image->Pal.getPalSize() - 1) {
					fprintf(HeadFile, " %4d", image->Pal.getPalette()[i+j]);
					break;
				}
				else
					fprintf(HeadFile, " %4d,", image->Pal.getPalette()[i+j]);
			}
			fprintf(HeadFile, "\n");
		}

		fprintf(HeadFile, "};\n");
	}
	fclose(HeadFile);
	return IL_TRUE;
}
コード例 #7
0
//! Generates a C-style header file for the current image.
ILboolean ilSaveCHeader(ILconst_string FileName, char *InternalName)
{
	FILE		*HeadFile;
	ILuint		i = 0, j;
	ILimage		*TempImage;
	const char	*Name;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	Name = iGetString(IL_CHEAD_HEADER_STRING);
	if (Name == NULL)
		Name = InternalName;

	if (FileName == NULL || Name == NULL ||
		ilStrLen(FileName) < 1 || ilCharStrLen(Name) < 1) {
		ilSetError(IL_INVALID_VALUE);
		return IL_FALSE;
	}

	if (!iCheckExtension(FileName, IL_TEXT("h"))) {
		ilSetError(IL_INVALID_EXTENSION);
		return IL_FALSE;
	}

	if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
		if (iFileExists(FileName)) {
			ilSetError(IL_FILE_ALREADY_EXISTS);
            return IL_FALSE;
		}
	}

	if (iCurImage->Bpc > 1) {
		TempImage = iConvertImage(iCurImage, iCurImage->Format, IL_UNSIGNED_BYTE);
		if (TempImage == NULL)
           return IL_FALSE;
	} else {
		TempImage = iCurImage;
	}

#ifndef _UNICODE
	HeadFile = fopen(FileName, "wb");
#else
    HeadFile = _wfopen(FileName, L"rb");
#endif//_UNICODE

	if (HeadFile == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
        return IL_FALSE;
	}

	fprintf(HeadFile, "//#include <il/il.h>\n");
	fprintf(HeadFile, "// C Image Header:\n\n\n");
	fprintf(HeadFile, "// IMAGE_BPP is in bytes per pixel, *not* bits\n");
    fprintf(HeadFile, "#define IMAGE_BPP %d\n",iCurImage->Bpp);
	fprintf(HeadFile, "#define IMAGE_WIDTH   %d\n", iCurImage->Width);
	fprintf(HeadFile, "#define IMAGE_HEIGHT  %d\n", iCurImage->Height);	
	fprintf(HeadFile, "#define IMAGE_DEPTH   %d\n\n\n", iCurImage->Depth);
	fprintf(HeadFile, "#define IMAGE_TYPE    0x%X\n", iCurImage->Type);
	fprintf(HeadFile, "#define IMAGE_FORMAT  0x%X\n\n\n", iCurImage->Format);
    fprintf(HeadFile, "ILubyte %s[] = {\n", Name);
        

	for (; i < TempImage->SizeOfData; i += MAX_LINE_WIDTH) {
		fprintf(HeadFile, "\t");
		for (j = 0; j < MAX_LINE_WIDTH; j++) {
			if (i + j >= TempImage->SizeOfData - 1) {
				fprintf(HeadFile, "%4d", TempImage->Data[i+j]);
				break;
			}
			else
				fprintf(HeadFile, "%4d,", TempImage->Data[i+j]);
		}
		fprintf(HeadFile, "\n");
	}
	if (TempImage != iCurImage)
		ilCloseImage(TempImage);

	fprintf(HeadFile, "};\n");


	if (iCurImage->Pal.Palette && iCurImage->Pal.PalSize && iCurImage->Pal.PalType != IL_PAL_NONE) {
		fprintf(HeadFile, "\n\n");
		fprintf(HeadFile, "#define IMAGE_PALSIZE %u\n\n", iCurImage->Pal.PalSize);
		fprintf(HeadFile, "#define IMAGE_PALTYPE 0x%X\n\n", iCurImage->Pal.PalType);
        fprintf(HeadFile, "ILubyte %sPal[] = {\n", Name);
		for (i = 0; i < iCurImage->Pal.PalSize; i += MAX_LINE_WIDTH) {
			fprintf(HeadFile, "\t");
			for (j = 0; j < MAX_LINE_WIDTH; j++) {
				if (i + j >= iCurImage->Pal.PalSize - 1) {
					fprintf(HeadFile, " %4d", iCurImage->Pal.Palette[i+j]);
					break;
				}
				else
					fprintf(HeadFile, " %4d,", iCurImage->Pal.Palette[i+j]);
			}
			fprintf(HeadFile, "\n");
		}

		fprintf(HeadFile, "};\n");
	}
	fclose(HeadFile);
	return IL_TRUE;
}
コード例 #8
0
ファイル: il_header.c プロジェクト: kolrabi/kail
//! Generates a C-style header file for the current image.
static ILboolean 
iSaveCHEADInternal(ILimage* image)
{
	const char *InternalName = "IL_IMAGE";
	// FILE		*HeadFile;
	ILuint		i = 0, j;
	ILimage		*TempImage;
	const char	*Name;
	char        tmp[512];
	SIO *       io;

	if (image == NULL) {
		iSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	io = &image->io;

	Name = iGetString(image, IL_META_DOCUMENT_NAME);
	if (Name == NULL)
		Name = InternalName;

	if (image->Bpc > 1) {
		TempImage = iConvertImage(image, image->Format, IL_UNSIGNED_BYTE);
		if (TempImage == NULL)
           return IL_FALSE;
	} else {
		TempImage = image;
	}

	SIOputs(io, "//#include <il/il.h>\n");
	SIOputs(io, "// C Image Header:\n\n\n");
	SIOputs(io, "// IMAGE_BPP is in bytes per pixel, *not* bits\n");

	snprintf(tmp, sizeof(tmp), "#define IMAGE_BPP     %d\n",image->Bpp);
	SIOputs(io, tmp);

	snprintf(tmp, sizeof(tmp), "#define IMAGE_WIDTH   %d\n", image->Width);
	SIOputs(io, tmp);
	
	snprintf(tmp, sizeof(tmp), "#define IMAGE_HEIGHT  %d\n", image->Height);	
	SIOputs(io, tmp);
	
	snprintf(tmp, sizeof(tmp), "#define IMAGE_DEPTH   %d\n\n\n", image->Depth);
	SIOputs(io, tmp);
	
	snprintf(tmp, sizeof(tmp), "#define IMAGE_TYPE    0x%X\n", image->Type);
	SIOputs(io, tmp);
	
	snprintf(tmp, sizeof(tmp), "#define IMAGE_FORMAT  0x%X\n\n\n", image->Format);
	SIOputs(io, tmp);

	snprintf(tmp, sizeof(tmp), "ILubyte %s[] = {\n", Name);
	SIOputs(io, tmp);
        
	for (; i < TempImage->SizeOfData; i += MAX_LINE_WIDTH) {
		SIOputs(io, "\t");
		for (j = 0; j < MAX_LINE_WIDTH; j++) {
			if (i + j >= TempImage->SizeOfData - 1) {
				snprintf(tmp, sizeof(tmp), " %4d", TempImage->Data[i+j]);
				SIOputs(io, tmp);
				break;
			}	else {
				snprintf(tmp, sizeof(tmp), " %4d,", TempImage->Data[i+j]);
				SIOputs(io, tmp);
			}
		}
		SIOputs(io, "\n");
	}

	if (TempImage != image)
		iCloseImage(TempImage);

	SIOputs(io, "};\n");

	if (image->Pal.Palette && image->Pal.PalSize && image->Pal.PalType != IL_PAL_NONE) {
		SIOputs(io, "\n\n");

		snprintf(tmp, sizeof(tmp), "#define IMAGE_PALSIZE %u\n\n", image->Pal.PalSize);
		SIOputs(io, tmp);

		snprintf(tmp, sizeof(tmp), "#define IMAGE_PALTYPE 0x%X\n\n", image->Pal.PalType);
		SIOputs(io, tmp);

		snprintf(tmp, sizeof(tmp), "ILubyte %sPal[] = {\n", Name);
		SIOputs(io, tmp);

		for (i = 0; i < image->Pal.PalSize; i += MAX_LINE_WIDTH) {
			SIOputs(io, "\t");
			for (j = 0; j < MAX_LINE_WIDTH; j++) {
				if (i + j >= image->Pal.PalSize - 1) {
					snprintf(tmp, sizeof(tmp), " %4d", image->Pal.Palette[i+j]);
					SIOputs(io, tmp);
					break;
				} else {
					snprintf(tmp, sizeof(tmp), " %4d,", image->Pal.Palette[i+j]);
					SIOputs(io, tmp);
				}
			}
			SIOputs(io, "\n");
		}

		SIOputs(io, "};\n");
	}

	return IL_TRUE;
}
コード例 #9
0
// Internal function used to save the Tiff.
ILboolean iSaveTiffInternal(char *Filename)
{
	ILenum	Format;
	ILenum	Compression;
	ILuint	ixLine;
	TIFF	*File;
	char	Description[512];
	ILimage	*TempImage;

	if(iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}


	if (iGetHint(IL_COMPRESSION_HINT) == IL_USE_COMPRESSION)
		Compression = COMPRESSION_PACKBITS;
	else
		Compression = COMPRESSION_NONE;

	if (iCurImage->Format == IL_COLOUR_INDEX) {
		if (ilGetBppPal(iCurImage->Pal.PalType) == 4)  // Preserve the alpha.
			TempImage = iConvertImage(iCurImage, IL_RGBA, IL_UNSIGNED_BYTE);
		else
			TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);

		if (TempImage == NULL) {
			return IL_FALSE;
		}
	}
	else {
		TempImage = iCurImage;
	}
	
	File = TIFFOpen(Filename, "w");
	//File = iTIFFOpen("w");
	if (File == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return IL_FALSE;
	}

	sprintf(Description, "Tiff generated by %s", ilGetString(IL_VERSION_NUM));

	TIFFSetField(File, TIFFTAG_IMAGEWIDTH, TempImage->Width);
	TIFFSetField(File, TIFFTAG_IMAGELENGTH, TempImage->Height);
	TIFFSetField(File, TIFFTAG_COMPRESSION, Compression);
	TIFFSetField(File, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
	TIFFSetField(File, TIFFTAG_BITSPERSAMPLE, TempImage->Bpc << 3);
	TIFFSetField(File, TIFFTAG_SAMPLESPERPIXEL, TempImage->Bpp);
	TIFFSetField(File, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
	TIFFSetField(File, TIFFTAG_ROWSPERSTRIP, 1);
	TIFFSetField(File, TIFFTAG_SOFTWARE, ilGetString(IL_VERSION_NUM));
	/*TIFFSetField(File, TIFFTAG_DOCUMENTNAME,
						iGetString(IL_TIF_DOCUMENTNAME_STRING) ?
						iGetString(IL_TIF_DOCUMENTNAME_STRING) : FileName);*/
	if (iGetString(IL_TIF_DOCUMENTNAME_STRING))
		TIFFSetField(File, TIFFTAG_DOCUMENTNAME, iGetString(IL_TIF_DOCUMENTNAME_STRING));
	if (iGetString(IL_TIF_AUTHNAME_STRING))
		TIFFSetField(File, TIFFTAG_ARTIST, iGetString(IL_TIF_AUTHNAME_STRING));
	if (iGetString(IL_TIF_HOSTCOMPUTER_STRING))
		TIFFSetField(File, TIFFTAG_HOSTCOMPUTER,
					iGetString(IL_TIF_HOSTCOMPUTER_STRING));
	if (iGetString(IL_TIF_DESCRIPTION_STRING))
		TIFFSetField(File, TIFFTAG_IMAGEDESCRIPTION,
					iGetString(IL_TIF_DESCRIPTION_STRING));
	TIFFSetField(File, TIFFTAG_DATETIME, iMakeString());


        // 24/4/2003
        // Orientation flag is not always supported ( Photoshop, ...), orient the image data 
        // and set it always to normal view
        TIFFSetField(File, TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT );
        if( TempImage->Origin != IL_ORIGIN_UPPER_LEFT ) {
            ILubyte *Data = iGetFlipped(TempImage);
            ifree( (void*)TempImage->Data );
            TempImage->Data = Data;
        }
        
        /*
	TIFFSetField(File, TIFFTAG_ORIENTATION,
		TempImage->Origin == IL_ORIGIN_UPPER_LEFT ? ORIENTATION_TOPLEFT : ORIENTATION_BOTLEFT);
        */
        
	Format = TempImage->Format;
	if (Format == IL_BGR || Format == IL_BGRA)
		ilSwapColours();

	for (ixLine = 0; ixLine < TempImage->Height; ++ixLine) {
		if (TIFFWriteScanline(File, TempImage->Data + ixLine * TempImage->Bps, ixLine, 0) < 0) {
			TIFFClose(File);
			ilSetError(IL_LIB_TIFF_ERROR);
			return IL_FALSE;
		}
	}

	if (Format == IL_BGR || Format == IL_BGRA)
		ilSwapColours();

	if (TempImage != iCurImage)
		ilCloseImage(TempImage);

	TIFFClose(File);

	return IL_TRUE;
}
コード例 #10
0
ファイル: il_hdr.c プロジェクト: kphillisjr/DevIL
// Internal function used to save the Hdr.
ILboolean iSaveHdrInternal()
{
	ILimage *TempImage;
	rgbe_header_info stHeader;
	unsigned char rgbe[4];
	ILubyte		*buffer;
	ILfloat		*data;
	ILuint		i;
	ILboolean	bRet;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	stHeader.exposure = 0;
	stHeader.gamma = 0;
	stHeader.programtype[0] = 0;
	stHeader.valid = 0;

	if (iCurImage->Format != IL_UNSIGNED_BYTE) {
		TempImage = iConvertImage(iCurImage, IL_RGB, IL_FLOAT);
		if (TempImage == NULL)
			return IL_FALSE;
	}
	else
		TempImage = iCurImage;

	if (!RGBE_WriteHeader(TempImage->Width, TempImage->Height, &stHeader))
		return IL_FALSE;

	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT)
		iFlipBuffer(TempImage->Data,TempImage->Depth,TempImage->Bps,TempImage->Height);
	data = (ILfloat*)TempImage->Data;


	if ((TempImage->Width < 8)||(TempImage->Width > 0x7fff)) {
		/* run length encoding is not allowed so write flat*/
		bRet = RGBE_WritePixels(data,TempImage->Width*TempImage->Height);
		if (iCurImage != TempImage)
			ilCloseImage(TempImage);
		return bRet;
	}
	buffer = (ILubyte*)ialloc(sizeof(ILubyte)*4*TempImage->Width);
	if (buffer == NULL) {
		/* no buffer space so write flat */
		bRet = RGBE_WritePixels(data,TempImage->Width*TempImage->Height);
		if (iCurImage != TempImage)
			ilCloseImage(TempImage);
		return bRet;
	}

	while(TempImage->Height-- > 0) {
		rgbe[0] = 2;
		rgbe[1] = 2;
		rgbe[2] = TempImage->Width >> 8;
		rgbe[3] = TempImage->Width & 0xFF;
		if (iwrite(rgbe, sizeof(rgbe), 1) < 1) {
			free(buffer);
			if (iCurImage != TempImage)
				ilCloseImage(TempImage);
			return IL_FALSE;
		}

		for(i=0;i<TempImage->Width;i++) {
			float2rgbe(rgbe,data[RGBE_DATA_RED],data[RGBE_DATA_GREEN],data[RGBE_DATA_BLUE]);
			buffer[i] = rgbe[0];
			buffer[i+TempImage->Width] = rgbe[1];
			buffer[i+2*TempImage->Width] = rgbe[2];
			buffer[i+3*TempImage->Width] = rgbe[3];
			data += RGBE_DATA_SIZE;
		}
		/* write out each of the four channels separately run length encoded */
		/* first red, then green, then blue, then exponent */
		for(i=0;i<4;i++) {
			if (RGBE_WriteBytes_RLE(&buffer[i*TempImage->Width],TempImage->Width) != IL_TRUE) {
				ifree(buffer);
				if (iCurImage != TempImage)
					ilCloseImage(TempImage);
				return IL_FALSE;
			}
		}
	}
	ifree(buffer);

	if (iCurImage != TempImage)
		ilCloseImage(TempImage);
	return IL_TRUE;
}
コード例 #11
0
ファイル: il_manip.cpp プロジェクト: KFreon/KFreon_s-ResIL
ILAPI ILubyte* ILAPIENTRY il2GetAlpha(ILimage* image, ILenum Type)
{
	ILimage		*TempImage;
	ILubyte		*Alpha;
	ILushort	*AlphaShort;
	ILuint		*AlphaInt;
	ILdouble	*AlphaDbl;
	ILuint		i, j, Bpc, Size, AlphaOff;

	if (image == NULL) {
		il2SetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	Bpc = ilGetBpcType(Type);
	if (Bpc == 0) {
		il2SetError(IL_INVALID_PARAM);
		return NULL;
	}

	if (image->Type == Type) {
		TempImage = image;
	} else {
		TempImage = iConvertImage(image, image->Format, Type);
		if (TempImage == NULL)
			return NULL;
	}

	Size = image->Width * image->Height * image->Depth * TempImage->Bpp;
	Alpha = (ILubyte*)ialloc(Size / TempImage->Bpp * Bpc);
	if (Alpha == NULL) {
		if (TempImage != image)
			ilCloseImage(TempImage);
		return NULL;
	}

	switch (TempImage->Format)
	{
		case IL_RGB:
		case IL_BGR:
		case IL_LUMINANCE:
		case IL_COLOUR_INDEX:  // @TODO: Make IL_COLOUR_INDEX separate.
			memset(Alpha, 0xFF, Size / TempImage->Bpp * Bpc);
			if (TempImage != image)
				ilCloseImage(TempImage);
			return Alpha;
	}

	// If our format is alpha, just return a copy.
	if (TempImage->Format == IL_ALPHA) {
		memcpy(Alpha, TempImage->Data, TempImage->SizeOfData);
		return Alpha;
	}
		
	if (TempImage->Format == IL_LUMINANCE_ALPHA)
		AlphaOff = 2;
	else
		AlphaOff = 4;

	switch (TempImage->Type)
	{
		case IL_BYTE:
		case IL_UNSIGNED_BYTE:
			for (i = AlphaOff-1, j = 0; i < Size; i += AlphaOff, j++)
				Alpha[j] = TempImage->Data[i];
			break;

		case IL_SHORT:
		case IL_UNSIGNED_SHORT:
			AlphaShort = (ILushort*)Alpha;
			for (i = AlphaOff-1, j = 0; i < Size; i += AlphaOff, j++)
				AlphaShort[j] = ((ILushort*)TempImage->Data)[i];
			break;

		case IL_INT:
		case IL_UNSIGNED_INT:
		case IL_FLOAT:  // Can throw float in here, because it's the same size.
			AlphaInt = (ILuint*)Alpha;
			for (i = AlphaOff-1, j = 0; i < Size; i += AlphaOff, j++)
				AlphaInt[j] = ((ILuint*)TempImage->Data)[i];
			break;

		case IL_DOUBLE:
			AlphaDbl = (ILdouble*)Alpha;
			for (i = AlphaOff-1, j = 0; i < Size; i += AlphaOff, j++)
				AlphaDbl[j] = ((ILdouble*)TempImage->Data)[i];
			break;
	}

	if (TempImage != image)
		ilCloseImage(TempImage);

	return Alpha;
}
コード例 #12
0
ファイル: il_sgi.c プロジェクト: kphillisjr/DevIL
// Internal function used to save the Sgi.
ILboolean iSaveSgiInternal()
{
	ILuint		i, c;
	ILboolean	Compress;
	ILimage		*Temp = iCurImage;
	ILubyte		*TempData;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (iCurImage->Format != IL_LUMINANCE
	    //while the sgi spec doesn't directly forbid rgb files with 2
	    //channels, they are quite uncommon and most apps don't support
	    //them. so convert lum_a images to rgba before writing.
	    //&& iCurImage->Format != IL_LUMINANCE_ALPHA
	    && iCurImage->Format != IL_RGB
	    && iCurImage->Format != IL_RGBA) {
		if (iCurImage->Format == IL_BGRA || iCurImage->Format == IL_LUMINANCE_ALPHA)
			Temp = iConvertImage(iCurImage, IL_RGBA, DetermineSgiType(iCurImage->Type));
		else
			Temp = iConvertImage(iCurImage, IL_RGB, DetermineSgiType(iCurImage->Type));
	}
	else if (iCurImage->Type > IL_UNSIGNED_SHORT) {
		Temp = iConvertImage(iCurImage, iCurImage->Format, DetermineSgiType(iCurImage->Type));
	}
	
	//compression of images with 2 bytes per channel doesn't work yet
	Compress = iGetInt(IL_SGI_RLE) && Temp->Bpc == 1;

	if (Temp == NULL)
		return IL_FALSE;

	SaveBigUShort(SGI_MAGICNUM);  // 'Magic' number
	if (Compress)
		iputc(1);
	else
		iputc(0);

	if (Temp->Type == IL_UNSIGNED_BYTE)
		iputc(1);
	else if (Temp->Type == IL_UNSIGNED_SHORT)
		iputc(2);
	// Need to error here if not one of the two...

	if (Temp->Format == IL_LUMINANCE || Temp->Format == IL_COLOUR_INDEX)
		SaveBigUShort(2);
	else
		SaveBigUShort(3);

	SaveBigUShort((ILushort)Temp->Width);
	SaveBigUShort((ILushort)Temp->Height);
	SaveBigUShort((ILushort)Temp->Bpp);

	switch (Temp->Type)
	{
		case IL_BYTE:
			SaveBigInt(SCHAR_MIN);	// Minimum pixel value
			SaveBigInt(SCHAR_MAX);	// Maximum pixel value
			break;
		case IL_UNSIGNED_BYTE:
			SaveBigInt(0);			// Minimum pixel value
			SaveBigInt(UCHAR_MAX);	// Maximum pixel value
			break;
		case IL_SHORT:
			SaveBigInt(SHRT_MIN);	// Minimum pixel value
			SaveBigInt(SHRT_MAX);	// Maximum pixel value
			break;
		case IL_UNSIGNED_SHORT:
			SaveBigInt(0);			// Minimum pixel value
			SaveBigInt(USHRT_MAX);	// Maximum pixel value
			break;
	}

	SaveBigInt(0);  // Dummy value

	if (FName) {
		c = ilCharStrLen(FName);
		c = c < 79 ? 79 : c;
		iwrite(FName, 1, c);
		c = 80 - c;
		for (i = 0; i < c; i++) {
			iputc(0);
		}
	}
	else {
		for (i = 0; i < 80; i++) {
			iputc(0);
		}
	}

	SaveBigUInt(0);  // Colormap

	// Padding
	for (i = 0; i < 101; i++) {
		SaveLittleInt(0);
	}


	if (iCurImage->Origin == IL_ORIGIN_UPPER_LEFT) {
		TempData = iGetFlipped(Temp);
		if (TempData == NULL) {
			if (Temp!= iCurImage)
				ilCloseImage(Temp);
			return IL_FALSE;
		}
	}
	else {
		TempData = Temp->Data;
	}


	if (!Compress) {
		for (c = 0; c < Temp->Bpp; c++) {
			for (i = c; i < Temp->SizeOfData; i += Temp->Bpp) {
				iputc(TempData[i]);  // Have to save each colour plane separately.
			}
		}
	}
	else {
		iSaveRleSgi(TempData, Temp->Width, Temp->Height, Temp->Bpp, Temp->Bps);
	}


	if (TempData != Temp->Data)
		ifree(TempData);
	if (Temp != iCurImage)
		ilCloseImage(Temp);

	return IL_TRUE;
}
コード例 #13
0
ファイル: ilut_directx10.cpp プロジェクト: DentonW/DevIL
ILimage *MakeD3D10Compliant(ID3D10Device *Device, DXGI_FORMAT *DestFormat)
{
	ILimage	*Converted, *Scaled, *CurImage;
	ILuint nConversionType, ilutFormat;
	ILboolean bForceIntegerFormat = ilutGetBoolean(ILUT_FORCE_INTEGER_FORMAT);

	Device;
	ilutFormat = ilutCurImage->Format;
	nConversionType = ilutCurImage->Type;

	if (!ilutCurImage)
		return NULL;

	switch (ilutCurImage->Type)
	{
	case IL_UNSIGNED_BYTE:
	case IL_BYTE:
	case IL_SHORT:
	case IL_UNSIGNED_SHORT:
	case IL_INT:
	case IL_UNSIGNED_INT:
		*DestFormat     = DXGI_FORMAT_R8G8B8A8_UINT;
		nConversionType = IL_UNSIGNED_BYTE;
		ilutFormat      = IL_RGBA;
		break;
	case IL_FLOAT:
	case IL_DOUBLE:
	case IL_HALF:
		if (bForceIntegerFormat || (!FormatsDX10supported[6]))
		{
			*DestFormat     = DXGI_FORMAT_R8G8B8A8_UINT;
			nConversionType = IL_UNSIGNED_BYTE;
			ilutFormat      = IL_RGBA;
		}
		else
		{
			*DestFormat     = DXGI_FORMAT_R32G32B32A32_FLOAT;
			nConversionType = IL_HALF;
			ilutFormat      = IL_RGBA;
		}
		break;
	}

	// Images must be in BGRA format.
	if (((ilutCurImage->Format != ilutFormat))
		|| (ilutCurImage->Type != nConversionType)) 
	{
		Converted = iConvertImage(ilutCurImage, ilutFormat, nConversionType);
		if (Converted == NULL)
			return NULL;
	}
	else 
	{
		Converted = ilutCurImage;
	}

	// Images must have their origin in the upper left.
	if (Converted->Origin != IL_ORIGIN_UPPER_LEFT) 
	{
		CurImage = ilutCurImage;
		ilSetCurImage(Converted);
		iluFlipImage();
		ilSetCurImage(CurImage);
	}

	// Images must have powers-of-2 dimensions.
	if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
		ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
		ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
			Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
						ilNextPower2(ilutCurImage->Height), 1);  //@TODO: 1 should be ilNextPower2(ilutCurImage->Depth)
			if (Converted != ilutCurImage) {
				ilCloseImage(Converted);
			}
			if (Scaled == NULL) {
				return NULL;
			}
			Converted = Scaled;
	}

	return Converted;
}
コード例 #14
0
ILimage *MakeD3D9Compliant(IDirect3DDevice9 *Device, D3DFORMAT *DestFormat)
{
	ILuint	color;
	ILimage	*Converted, *Scaled, *CurImage;

	*DestFormat = D3DFMT_A8R8G8B8;

	// Images must be in BGRA format.
	if (ilutCurImage->Format != IL_BGRA) {
		Converted = iConvertImage(ilutCurImage, IL_BGRA, IL_UNSIGNED_BYTE);
		if (Converted == NULL)
			return NULL;
	}
	else {
		Converted = ilutCurImage;
	}

	// perform alpha key on images if requested
	color=ilutGetInteger(ILUT_D3D_ALPHA_KEY_COLOR);
	if(color>=0)
	{
	ILubyte *data;
	ILubyte *maxdata;
	ILuint t;

		data=(Converted->Data);
		maxdata=(Converted->Data+Converted->SizeOfData);
		while(data<maxdata)
		{
			t= (data[2]<<16) + (data[1]<<8) + (data[0]) ;

			if((t&0x00ffffff)==(color&0x00ffffff))
			{
				data[0]=0;
				data[1]=0;
				data[2]=0;
				data[3]=0;
			}
			data+=4;
		}
	}

	// Images must have their origin in the upper left.
	if (Converted->Origin != IL_ORIGIN_UPPER_LEFT) {
		CurImage = ilutCurImage;
		ilSetCurImage(Converted);
		iluFlipImage();
		ilSetCurImage(CurImage);
	}

	// Images must have powers-of-2 dimensions.
	if (ilNextPower2(ilutCurImage->Width) != ilutCurImage->Width ||
		ilNextPower2(ilutCurImage->Height) != ilutCurImage->Height ||
		ilNextPower2(ilutCurImage->Depth) != ilutCurImage->Depth) {
			Scaled = iluScale_(Converted, ilNextPower2(ilutCurImage->Width),
						ilNextPower2(ilutCurImage->Height), ilNextPower2(ilutCurImage->Depth));
			if (Converted != ilutCurImage) {
				ilCloseImage(Converted);
			}
			if (Scaled == NULL) {
				return NULL;
			}
			Converted = Scaled;
	}




	return Converted;
}
コード例 #15
0
ファイル: il_jpeg.c プロジェクト: ProfessorKaos64/tdm
ILboolean iSaveJpegInternal()

{

	struct		jpeg_compress_struct JpegInfo;

	struct		jpeg_error_mgr Error;

	JSAMPROW	row_pointer[1];

	ILimage		*TempImage;

	ILubyte		*TempData;

	ILenum		Type = 0;



	if (iCurImage == NULL) {

		ilSetError(IL_ILLEGAL_OPERATION);

		return IL_FALSE;

	}



	/*if (iGetHint(IL_COMPRESSION_HINT) == IL_USE_COMPRESSION)

		Quality = 85;  // Not sure how low we should dare go...

	else

		Quality = 99;*/


	// angua: RGB images also need to be converted to RGBA
	if ((iCurImage->Format != IL_RGBA && iCurImage->Format != IL_LUMINANCE) || iCurImage->Bpc != 1) {
        // taaaki: since RGB_PIXELSIZE was changed to 4, the conversion to 
        //         RGB is short one colour component, causing screenshots to crash
		TempImage = iConvertImage(iCurImage, IL_RGBA, IL_UNSIGNED_BYTE);

		if (TempImage == NULL) {

			return IL_FALSE;

		}

	}

	else {

		TempImage = iCurImage;

	}



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT) {

		TempData = iGetFlipped(TempImage);

		if (TempData == NULL) {

			if (TempImage != iCurImage)

				ilCloseImage(TempImage);

			return IL_FALSE;

		}

	}

	else {

		TempData = TempImage->Data;

	}





	JpegInfo.err = jpeg_std_error(&Error);

	// Now we can initialize the JPEG compression object.

	jpeg_create_compress(&JpegInfo);



	//jpeg_stdio_dest(&JpegInfo, JpegFile);

	devil_jpeg_write_init(&JpegInfo);



	JpegInfo.image_width = TempImage->Width;  // image width and height, in pixels

	JpegInfo.image_height = TempImage->Height;

	JpegInfo.input_components = TempImage->Bpp;  // # of color components per pixel



	// John Villar's addition

	if (TempImage->Bpp == 1)

		JpegInfo.in_color_space = JCS_GRAYSCALE;

	else

		JpegInfo.in_color_space = JCS_RGB;



	jpeg_set_defaults(&JpegInfo);



/*#ifndef IL_USE_JPEGLIB_UNMODIFIED

	Type = iGetInt(IL_JPG_SAVE_FORMAT);

	if (Type == IL_EXIF) {

		JpegInfo.write_JFIF_header = FALSE;

		JpegInfo.write_EXIF_header = TRUE;

	}

	else if (Type == IL_JFIF) {

		JpegInfo.write_JFIF_header = TRUE;

		JpegInfo.write_EXIF_header = FALSE;

	} EXIF not present int libjpeg...

#else*/

	Type = Type;

	JpegInfo.write_JFIF_header = TRUE;

//#endif//IL_USE_JPEGLIB_UNMODIFIED



	jpeg_set_quality(&JpegInfo, iGetInt(IL_JPG_QUALITY), IL_TRUE);



	jpeg_start_compress(&JpegInfo, IL_TRUE);



	//row_stride = image_width * 3;	// JSAMPLEs per row in image_buffer



	while (JpegInfo.next_scanline < JpegInfo.image_height) {

		// jpeg_write_scanlines expects an array of pointers to scanlines.

		// Here the array is only one element long, but you could pass

		// more than one scanline at a time if that's more convenient.

		row_pointer[0] = &TempData[JpegInfo.next_scanline * TempImage->Bps];

		(void) jpeg_write_scanlines(&JpegInfo, row_pointer, 1);

	}



	// Step 6: Finish compression

	jpeg_finish_compress(&JpegInfo);



	// Step 7: release JPEG compression object



	// This is an important step since it will release a good deal of memory.

	jpeg_destroy_compress(&JpegInfo);



	if (TempImage->Origin == IL_ORIGIN_LOWER_LEFT)

		ifree(TempData);

	if (TempImage != iCurImage)

		ilCloseImage(TempImage);



	return IL_TRUE;

}
コード例 #16
0
ファイル: il_pnm.c プロジェクト: MichaelH13/sdkpub
// Internal function used to save the Pnm.
ILboolean iSavePnmInternal()
{
	ILuint		Bpp, MaxVal = UCHAR_MAX, i = 0, j, k;
	ILenum		Type = 0;
	ILuint		LinePos = 0;  // Cannot exceed 70 for pnm's!
	ILboolean	Binary;
	ILimage		*TempImage;
	ILubyte		*TempData;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (iCheckExtension(FName, IL_TEXT("pbm")))
		Type = IL_PBM_ASCII;
	else if (iCheckExtension(FName, IL_TEXT("pgm")))
		Type = IL_PGM_ASCII;
	else if (iCheckExtension(FName, IL_TEXT("ppm")))
		Type = IL_PPM_ASCII;
	else
		Type = IL_PPM_ASCII;

	/*if (!Type) {
		ilSetError(IL_INVALID_EXTENSION);
		return IL_FALSE;
	}*/

	if (iGetHint(IL_COMPRESSION_HINT) == IL_USE_COMPRESSION) {
		Type += 3;
		Binary = IL_TRUE;
	}
	else {
		Binary = IL_FALSE;
	}

	if (iCurImage->Type == IL_UNSIGNED_BYTE) {
		MaxVal = UCHAR_MAX;
	}
	else if (iCurImage->Type == IL_UNSIGNED_SHORT) {
		MaxVal = USHRT_MAX;
	}
	else {
		ilSetError(IL_FORMAT_NOT_SUPPORTED);
		return IL_FALSE;
	}
	if (MaxVal > UCHAR_MAX && Type >= IL_PBM_BINARY) {  // binary cannot be higher than 255
		ilSetError(IL_FORMAT_NOT_SUPPORTED);
		return IL_FALSE;
	}

	switch (Type)
	{
		case IL_PBM_ASCII:
			Bpp = 1;
			ilprintf("P1\n");
			TempImage = iConvertImage(iCurImage, IL_LUMINANCE, IL_UNSIGNED_BYTE);
			break;
		//case IL_PBM_BINARY:  // Don't want to mess with saving bits just yet...
			//Bpp = 1;
			//ilprintf("P4\n");
			//break;
		case IL_PBM_BINARY:
			ilSetError(IL_FORMAT_NOT_SUPPORTED);
			return IL_FALSE;
		case IL_PGM_ASCII:
			Bpp = 1;
			ilprintf("P2\n");
			TempImage = iConvertImage(iCurImage, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
			break;
		case IL_PGM_BINARY:
			Bpp = 1;
			ilprintf("P5\n");
			TempImage = iConvertImage(iCurImage, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE);
			break;
		case IL_PPM_ASCII:
			Bpp = 3;
			ilprintf("P3\n");
			TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);
			break;
		case IL_PPM_BINARY:
			Bpp = 3;
			ilprintf("P6\n");
			TempImage = iConvertImage(iCurImage, IL_RGB, IL_UNSIGNED_BYTE);
			break;
		default:
			ilSetError(IL_INTERNAL_ERROR);
			return IL_FALSE;
	}

	if (TempImage == NULL)
		return IL_FALSE;

	if (Bpp != TempImage->Bpp) {
		ilSetError(IL_INVALID_CONVERSION);
		return IL_FALSE;
	}

	if (TempImage->Origin != IL_ORIGIN_UPPER_LEFT) {
		TempData = iGetFlipped(TempImage);
		if (TempData == NULL) {
			ilCloseImage(TempImage);
			return IL_FALSE;
		}
	}
	else {
		TempData = TempImage->Data;
	}

	ilprintf("%d %d\n", TempImage->Width, TempImage->Height);
	if (Type != IL_PBM_BINARY && Type != IL_PBM_ASCII)  // not needed for .pbm's (only 0 and 1)
		ilprintf("%d\n", MaxVal);

	while (i < TempImage->SizeOfPlane) {
		for (j = 0; j < Bpp; j++) {
			if (Binary) {
				if (Type == IL_PBM_BINARY) {
					iputc((ILubyte)(TempData[i] > 127 ? 1 : 0));
				}
				else {
					iputc(TempData[i]);
				}
			}
			else {
				if (TempImage->Type == IL_UNSIGNED_BYTE)
					k = TempData[i];
				else  // IL_UNSIGNED_SHORT
					k = *((ILushort*)TempData + i);
				if (Type == IL_PBM_ASCII) {
					LinePos += ilprintf("%d ", TempData[i] > 127 ? 1 : 0);
				}
				else {
					LinePos += ilprintf("%d ", TempData[i]);
				}
			}

			if (TempImage->Type == IL_UNSIGNED_SHORT)
				i++;
			i++;
		}

		if (LinePos > 65) {  // Just a good number =]
			ilprintf("\n");
			LinePos = 0;
		}
	}

	if (TempImage->Origin != IL_ORIGIN_UPPER_LEFT)
		ifree(TempData);
	ilCloseImage(TempImage);

	return IL_TRUE;
}
コード例 #17
0
// Internal function used to save the Sgi.
ILboolean iSaveSgiInternal()
{
	ILuint		i, c;
	ILboolean	Compress;
	ILimage		*Temp = iCurImage;
	ILubyte		*TempData;

	Compress = iGetInt(IL_SGI_RLE);

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (iCurImage->Format != IL_RGB && iCurImage->Format != IL_RGBA) {
		if (iCurImage->Format == IL_BGRA)
			Temp = iConvertImage(iCurImage, IL_RGBA, DetermineSgiType(iCurImage->Type));
		else
			Temp = iConvertImage(iCurImage, IL_RGB, DetermineSgiType(iCurImage->Type));
	}
	else if (iCurImage->Type > IL_UNSIGNED_SHORT) {
		Temp = iConvertImage(iCurImage, iCurImage->Format, DetermineSgiType(iCurImage->Type));
	}

	if (Temp == NULL)
		return IL_FALSE;

	SaveBigUShort(SGI_MAGICNUM);  // 'Magic' number
	if (Compress)
		iputc(1);
	else
		iputc(0);

	if (Temp->Type == IL_UNSIGNED_BYTE)
		iputc(1);
	else if (Temp->Type == IL_UNSIGNED_SHORT)
		iputc(2);
	// Need to error here if not one of the two...

	if (Temp->Format == IL_LUMINANCE || Temp->Format == IL_COLOUR_INDEX)
		SaveBigUShort(2);
	else
		SaveBigUShort(3);

	SaveBigUShort((ILushort)Temp->Width);
	SaveBigUShort((ILushort)Temp->Height);
	SaveBigUShort((ILushort)Temp->Bpp);

	switch (Temp->Type)
	{
		case IL_BYTE:
			SaveBigInt(SCHAR_MIN);	// Minimum pixel value
			SaveBigInt(SCHAR_MAX);	// Maximum pixel value
			break;
		case IL_UNSIGNED_BYTE:
			SaveBigInt(0);			// Minimum pixel value
			SaveBigInt(UCHAR_MAX);	// Maximum pixel value
			break;
		case IL_SHORT:
			SaveBigInt(SHRT_MIN);	// Minimum pixel value
			SaveBigInt(SHRT_MAX);	// Maximum pixel value
			break;
		case IL_UNSIGNED_SHORT:
			SaveBigInt(0);			// Minimum pixel value
			SaveBigInt(USHRT_MAX);	// Maximum pixel value
			break;
	}

	SaveBigInt(0);  // Dummy value

	if (FName) {
		c = strlen(FName);
		c = c < 79 ? 79 : c;
		iwrite(FName, 1, c);
		c = 80 - c;
		for (i = 0; i < c; i++) {
			iputc(0);
		}
	}
	else {
		for (i = 0; i < 80; i++) {
			iputc(0);
		}
	}

	SaveBigUInt(0);  // Colormap

	// Padding
	for (i = 0; i < 101; i++) {
		SaveLittleInt(0);
	}


	if (iCurImage->Origin == IL_ORIGIN_UPPER_LEFT) {
		TempData = iGetFlipped(Temp);
		if (TempData == NULL) {
			if (Temp!= iCurImage)
				ilCloseImage(Temp);
			return IL_FALSE;
		}
	}
	else {
		TempData = Temp->Data;
	}


	if (!Compress) {
		for (c = 0; c < Temp->Bpp; c++) {
			for (i = c; i < Temp->SizeOfData; i += Temp->Bpp) {
				iputc(TempData[i]);  // Have to save each colour plane separately.
			}
		}
	}
	else {
		iSaveRleSgi(TempData);
	}


	if (TempData != Temp->Data)
		ifree(TempData);
	if (Temp != iCurImage)
		ilCloseImage(Temp);

	return IL_TRUE;
}