コード例 #1
0
ファイル: image.c プロジェクト: clbr/netradiant
image_t *ImageLoad( const char *filename )
{
	int			i;
	image_t		*image;
	char		name[ 1024 ];
	int			size;
	byte		*buffer = NULL;
	qboolean	alphaHack = qfalse;

	
	/* init */
	ImageInit();
	
	/* dummy check */
	if( filename == NULL || filename[ 0 ] == '\0' )
		return NULL;
	
	/* strip file extension off name */
	strcpy( name, filename );
	StripExtension( name );
	
	/* try to find existing image */
	image = ImageFind( name );
	if( image != NULL )
	{
		image->refCount++;
		return image;
	}
	
	/* none found, so find first non-null image */
	image = NULL;
	for( i = 0; i < MAX_IMAGES; i++ )
	{
		if( images[ i ].name == NULL )
		{
			image = &images[ i ];
			break;
		}
	}
	
	/* too many images? */
	if( image == NULL )
		Error( "MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES );
	
	/* set it up */
	image->name = safe_malloc( strlen( name ) + 1 );
	strcpy( image->name, name );
	
	/* attempt to load tga */
	StripExtension( name );
	strcat( name, ".tga" );
	size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
	if( size > 0 )
		LoadTGABuffer( buffer, buffer + size, &image->pixels, &image->width, &image->height );
	else
	{
		/* attempt to load png */
		StripExtension( name );
		strcat( name, ".png" );
		size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
		if( size > 0 )
			LoadPNGBuffer( buffer, size, &image->pixels, &image->width, &image->height );
		else
		{
			/* attempt to load jpg */
			StripExtension( name );
			strcat( name, ".jpg" );
			size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
			if( size > 0 )
			{
				if( LoadJPGBuff( buffer, size, &image->pixels, &image->width, &image->height ) == -1 && image->pixels != NULL )
					Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
				alphaHack = qtrue;
			}
			else
			{
				/* attempt to load dds */
				StripExtension( name );
				strcat( name, ".dds" );
				size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
				if( size > 0 )
				{
					LoadDDSBuffer( buffer, size, &image->pixels, &image->width, &image->height );
					
					/* debug code */
					#if 1
					{
						ddsPF_t	pf;
						DDSGetInfo( (ddsBuffer_t*) buffer, NULL, NULL, &pf );
						Sys_Printf( "pf = %d\n", pf );
						if( image->width > 0 )
						{
							StripExtension( name );
							strcat( name, "_converted.tga" );
							WriteTGA( "C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width, image->height );
						}
					}
					#endif
				}
			}
		}
	}
	
	/* free file buffer */
	free( buffer );
	
	/* make sure everything's kosher */
	if( size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL )
	{
		//%	Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
		//%		size, image->width, image->height, image->pixels, name );
		free( image->name );
		image->name = NULL;
		return NULL;
	}
	
	/* set filename */
	image->filename = safe_malloc( strlen( name ) + 1 );
	strcpy( image->filename, name );
	
	/* set count */
	image->refCount = 1;
	numImages++;

	if(alphaHack)
	{
		StripExtension( name );
		strcat( name, "_alpha.jpg" );
		size = vfsLoadFile( (const char*) name, (void**) &buffer, 0 );
		if( size > 0 )
		{
			unsigned char *pixels;
			int width, height;
			if( LoadJPGBuff( buffer, size, &pixels, &width, &height ) == -1 && pixels != NULL )
				Sys_Printf( "WARNING: LoadJPGBuff: %s\n", (unsigned char*) image->pixels );
			if(pixels && width == image->width && height == image->height)
			{
				int i;
				for(i = 0; i < width*height; ++i)
					image->pixels[4*i+3] = pixels[4*i+2]; // copy alpha from blue channel
			}
			free(pixels);
			free(buffer);
		}
	}
	
	/* return the image */
	return image;
}
コード例 #2
0
image_t        *ImageLoad(const char *filename)
{
    int             i;
    image_t        *image;
    char            name[1024];
    int             size;
    byte           *buffer = NULL;


    /* init */
    ImageInit();

    /* dummy check */
    if(filename == NULL || filename[0] == '\0')
        return NULL;

    /* strip file extension off name */
    strcpy(name, filename);
    StripExtension(name);

    /* try to find existing image */
    image = ImageFind(name);
    if(image != NULL)
    {
        image->refCount++;
        return image;
    }

    /* none found, so find first non-null image */
    image = NULL;
    for(i = 0; i < MAX_IMAGES; i++)
    {
        if(images[i].name == NULL)
        {
            image = &images[i];
            break;
        }
    }

    /* too many images? */
    if(image == NULL)
        Error("MAX_IMAGES (%d) exceeded, there are too many image files referenced by the map.", MAX_IMAGES);

    /* set it up */
    image->name = safe_malloc(strlen(name) + 1);
    strcpy(image->name, name);

#if 1
    /* attempt to load tga */
    StripExtension(name);
    strcat(name, ".tga");
    size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
    if(size > 0)
        LoadTGABuffer(buffer, &image->pixels, &image->width, &image->height);
    else
    {
#if 1
        /* attempt to load png */
        StripExtension(name);
        strcat(name, ".png");
        size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
        if(size > 0)
            LoadPNGBuffer(buffer, &image->pixels, &image->width, &image->height);
        else
        {
#if 1
            /* attempt to load jpg */
            StripExtension(name);
            strcat(name, ".jpg");
            size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
            if(size > 0)
            {
                LoadJPGBuffer((const char *)name, buffer, size, &image->pixels, &image->width, &image->height);
                if(image->pixels == NULL)
                    Sys_Printf("WARNING: LoadJPGBuffer: '%s'\n", image->name);
            }
            else
            {
#if 1
                /* attempt to load dds */
                StripExtension(name);
                strcat(name, ".dds");
                size = vfsLoadFile((const char *)name, (void **)&buffer, 0);
                if(size > 0)
                {
                    LoadDDSBuffer(buffer, size, &image->pixels, &image->width, &image->height);

                    /* debug code */
#if 0
                    {
                        ddsPF_t         pf;

                        DDSGetInfo((ddsBuffer_t *) buffer, NULL, NULL, &pf);
                        Sys_Printf("pf = %d\n", pf);
                        if(image->width > 0)
                        {
                            StripExtension(name);
                            strcat(name, "_converted.tga");
                            WriteTGA("C:\\games\\quake3\\baseq3\\textures\\rad\\dds_converted.tga", image->pixels, image->width,
                                     image->height);
                        }
                    }
#endif							// dds debug
                }
#endif							// ignore dds
            }
#endif							// ignore jpg dds
        }
#endif							// ignore png jpg dds
    }
#endif							// ignore all images

    /* free file buffer */
    free(buffer);

    /* make sure everything's kosher */
    if(size <= 0 || image->width <= 0 || image->height <= 0 || image->pixels == NULL)
    {
        //% Sys_Printf( "size = %d  width = %d  height = %d  pixels = 0x%08x (%s)\n",
        //%     size, image->width, image->height, image->pixels, name );
        free(image->name);
        image->name = NULL;
        return NULL;
    }

    /* set filename */
    image->filename = safe_malloc(strlen(name) + 1);
    strcpy(image->filename, name);

    /* set count */
    image->refCount = 1;
    numImages++;

    /* return the image */
    return image;
}