diff --git a/src/gc_gl.c b/src/gc_gl.c index f9540b8..0c22c72 100644 --- a/src/gc_gl.c +++ b/src/gc_gl.c @@ -81,8 +81,9 @@ static void conv_rgba_to_luminance_alpha(unsigned char *src, void *dst, const un static void scramble_2b(unsigned short *src, void *dst, const unsigned int width, const unsigned int height); static void scramble_4b(unsigned char *src, void *dst, const unsigned int width, const unsigned int height); -void scale_internal(int components, int widthin, int heightin, const unsigned char *datain, - int widthout, int heightout, unsigned char *dataout); +static void scale_internal(int components, int widthin, int heightin, + const unsigned char *datain, + int widthout, int heightout, unsigned char *dataout); static void draw_arrays_pos_normal_texc(float *ptr_pos, float *ptr_texc, float *ptr_normal, int count, bool loop); @@ -1456,7 +1457,8 @@ void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei widt unsigned char *dst_addr = currtex->data; dst_addr += offset; - convert_rgb_image_to_DXT1((unsigned char *)data, dst_addr, width, height, needswap); + _ogx_convert_rgb_image_to_DXT1((unsigned char *)data, dst_addr, + width, height, needswap); DCFlushRange(dst_addr, calc_memory(width, height, bytesperpixelinternal)); } @@ -2759,8 +2761,9 @@ static void swap_rgb565(unsigned short *pixels, int num_pixels) //// Image scaling for arbitrary size taken from Mesa 3D and adapted by davidgf //// -void scale_internal(int components, int widthin, int heightin, const unsigned char *datain, - int widthout, int heightout, unsigned char *dataout) +static void scale_internal(int components, int widthin, int heightin, + const unsigned char *datain, + int widthout, int heightout, unsigned char *dataout) { float x, lowx, highx, convx, halfconvx; float y, lowy, highy, convy, halfconvy; diff --git a/src/image_DXT.c b/src/image_DXT.c index 453987d..39dd256 100644 --- a/src/image_DXT.c +++ b/src/image_DXT.c @@ -51,83 +51,15 @@ in DXT1 format (color only, no alpha). Speed is valued over prettyness, at least for now. */ -void compress_DDS_color_block( +static void compress_DDS_color_block( int channels, const unsigned char *const uncompressed, unsigned char compressed[8] ); -/* - Takes a 4x4 block of pixels and compresses the alpha - component it into 8 bytes for use in DXT5 DDS files. - Speed is valued over prettyness, at least for now. -*/ -void compress_DDS_alpha_block( - const unsigned char *const uncompressed, - unsigned char compressed[8] ); - -/********* Actual Exposed Functions *********/ -int - save_image_as_DDS - ( - const char *filename, - int width, int height, int channels, - const unsigned char *const data - ) -{ - /* variables */ - FILE *fout; - unsigned char *DDS_data; - DDS_header header; - int DDS_size; - /* error check */ - if( (NULL == filename) || - (width < 1) || (height < 1) || - (channels < 1) || (channels > 4) || - (data == NULL ) ) - { - return 0; - } - /* Convert the image */ - if( (channels & 1) == 1 ) - { - /* no alpha, just use DXT1 */ - DDS_data = convert_image_to_DXT1( data, width, height, channels, &DDS_size ); - } else - { - /* has alpha, so use DXT5 */ - DDS_data = convert_image_to_DXT5( data, width, height, channels, &DDS_size ); - } - /* save it */ - memset( &header, 0, sizeof( DDS_header ) ); - header.dwMagic = ('D' << 0) | ('D' << 8) | ('S' << 16) | (' ' << 24); - header.dwSize = 124; - header.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_LINEARSIZE; - header.dwWidth = width; - header.dwHeight = height; - header.dwPitchOrLinearSize = DDS_size; - header.sPixelFormat.dwSize = 32; - header.sPixelFormat.dwFlags = DDPF_FOURCC; - if( (channels & 1) == 1 ) - { - header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('1' << 24); - } else - { - header.sPixelFormat.dwFourCC = ('D' << 0) | ('X' << 8) | ('T' << 16) | ('5' << 24); - } - header.sCaps.dwCaps1 = DDSCAPS_TEXTURE; - /* write it out */ - fout = fopen( filename, "wb"); - fwrite( &header, sizeof( DDS_header ), 1, fout ); - fwrite( DDS_data, 1, DDS_size, fout ); - fclose( fout ); - /* done */ - free( DDS_data ); - return 1; -} // Modified and simplified version. It also handles Nintendo GX scrambling // Forced to 3 channels (RGB) // Forced to square and power of two textures -void convert_rgb_image_to_DXT1 ( +void _ogx_convert_rgb_image_to_DXT1 ( const unsigned char *const uncompressed, unsigned char *compressed, int width, int height, int red_blue_swap ) { @@ -173,190 +105,14 @@ void convert_rgb_image_to_DXT1 ( } } -unsigned char* convert_image_to_DXT1( - const unsigned char *const uncompressed, - int width, int height, int channels, - int *out_size ) -{ - unsigned char *compressed; - int i, j, x, y; - unsigned char ublock[16*3]; - unsigned char cblock[8]; - int index = 0, chan_step = 1; - int block_count = 0; - /* error check */ - *out_size = 0; - if( (width < 1) || (height < 1) || - (NULL == uncompressed) || - (channels < 1) || (channels > 4) ) - { - return NULL; - } - /* for channels == 1 or 2, I do not step forward for R,G,B values */ - if( channels < 3 ) - { - chan_step = 0; - } - /* get the RAM for the compressed image - (8 bytes per 4x4 pixel block) */ - *out_size = ((width+3) >> 2) * ((height+3) >> 2) * 8; - compressed = (unsigned char*)malloc( *out_size ); - /* go through each block */ - for( j = 0; j < height; j += 4 ) - { - for( i = 0; i < width; i += 4 ) - { - /* copy this block into a new one */ - int idx = 0; - int mx = 4, my = 4; - if( j+4 >= height ) - { - my = height - j; - } - if( i+4 >= width ) - { - mx = width - i; - } - for( y = 0; y < my; ++y ) - { - for( x = 0; x < mx; ++x ) - { - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels]; - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step]; - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step]; - } - for( x = mx; x < 4; ++x ) - { - ublock[idx++] = ublock[0]; - ublock[idx++] = ublock[1]; - ublock[idx++] = ublock[2]; - } - } - for( y = my; y < 4; ++y ) - { - for( x = 0; x < 4; ++x ) - { - ublock[idx++] = ublock[0]; - ublock[idx++] = ublock[1]; - ublock[idx++] = ublock[2]; - } - } - /* compress the block */ - ++block_count; - compress_DDS_color_block( 3, ublock, cblock ); - /* copy the data from the block into the main block */ - for( x = 0; x < 8; ++x ) - { - compressed[index++] = cblock[x]; - } - } - } - return compressed; -} - -unsigned char* convert_image_to_DXT5( - const unsigned char *const uncompressed, - int width, int height, int channels, - int *out_size ) -{ - unsigned char *compressed; - int i, j, x, y; - unsigned char ublock[16*4]; - unsigned char cblock[8]; - int index = 0, chan_step = 1; - int block_count = 0, has_alpha; - /* error check */ - *out_size = 0; - if( (width < 1) || (height < 1) || - (NULL == uncompressed) || - (channels < 1) || ( channels > 4) ) - { - return NULL; - } - /* for channels == 1 or 2, I do not step forward for R,G,B vales */ - if( channels < 3 ) - { - chan_step = 0; - } - /* # channels = 1 or 3 have no alpha, 2 & 4 do have alpha */ - has_alpha = 1 - (channels & 1); - /* get the RAM for the compressed image - (16 bytes per 4x4 pixel block) */ - *out_size = ((width+3) >> 2) * ((height+3) >> 2) * 16; - compressed = (unsigned char*)malloc( *out_size ); - /* go through each block */ - for( j = 0; j < height; j += 4 ) - { - for( i = 0; i < width; i += 4 ) - { - /* local variables, and my block counter */ - int idx = 0; - int mx = 4, my = 4; - if( j+4 >= height ) - { - my = height - j; - } - if( i+4 >= width ) - { - mx = width - i; - } - for( y = 0; y < my; ++y ) - { - for( x = 0; x < mx; ++x ) - { - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels]; - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step]; - ublock[idx++] = uncompressed[(j+y)*width*channels+(i+x)*channels+chan_step+chan_step]; - ublock[idx++] = - has_alpha * uncompressed[(j+y)*width*channels+(i+x)*channels+channels-1] - + (1-has_alpha)*255; - } - for( x = mx; x < 4; ++x ) - { - ublock[idx++] = ublock[0]; - ublock[idx++] = ublock[1]; - ublock[idx++] = ublock[2]; - ublock[idx++] = ublock[3]; - } - } - for( y = my; y < 4; ++y ) - { - for( x = 0; x < 4; ++x ) - { - ublock[idx++] = ublock[0]; - ublock[idx++] = ublock[1]; - ublock[idx++] = ublock[2]; - ublock[idx++] = ublock[3]; - } - } - /* now compress the alpha block */ - compress_DDS_alpha_block( ublock, cblock ); - /* copy the data from the compressed alpha block into the main buffer */ - for( x = 0; x < 8; ++x ) - { - compressed[index++] = cblock[x]; - } - /* then compress the color block */ - ++block_count; - compress_DDS_color_block( 4, ublock, cblock ); - /* copy the data from the compressed color block into the main buffer */ - for( x = 0; x < 8; ++x ) - { - compressed[index++] = cblock[x]; - } - } - } - return compressed; -} - /********* Helper Functions *********/ -int convert_bit_range( int c, int from_bits, int to_bits ) +static int convert_bit_range( int c, int from_bits, int to_bits ) { int b = (1 << (from_bits - 1)) + c * ((1 << to_bits) - 1); return (b + (b >> from_bits)) >> from_bits; } -int rgb_to_565( int r, int g, int b ) +static int rgb_to_565( int r, int g, int b ) { return (convert_bit_range( r, 8, 5 ) << 11) | @@ -364,14 +120,14 @@ int rgb_to_565( int r, int g, int b ) (convert_bit_range( b, 8, 5 ) << 00); } -void rgb_888_from_565( unsigned int c, int *r, int *g, int *b ) +static void rgb_888_from_565( unsigned int c, int *r, int *g, int *b ) { *r = convert_bit_range( (c >> 11) & 31, 5, 8 ); *g = convert_bit_range( (c >> 05) & 63, 6, 8 ); *b = convert_bit_range( (c >> 00) & 31, 5, 8 ); } -void compute_color_line_STDEV( +static void compute_color_line_STDEV( const unsigned char *const uncompressed, int channels, float point[3], float direction[3] ) @@ -483,7 +239,7 @@ void compute_color_line_STDEV( #endif } -void LSE_master_colors_max_min( +static void LSE_master_colors_max_min( int *cmax, int *cmin, int channels, const unsigned char *const uncompressed ) @@ -572,7 +328,7 @@ void LSE_master_colors_max_min( } } -void +static void compress_DDS_color_block ( int channels, @@ -652,60 +408,3 @@ void } /* done compressing to DXT1 */ } - -void - compress_DDS_alpha_block - ( - const unsigned char *const uncompressed, - unsigned char compressed[8] - ) -{ - /* variables */ - int i; - int next_bit; - int a0, a1; - float scale_me; - /* stupid order */ - int swizzle8[] = { 1, 7, 6, 5, 4, 3, 2, 0 }; - /* get the alpha limits (a0 > a1) */ - a0 = a1 = uncompressed[3]; - for( i = 4+3; i < 16*4; i += 4 ) - { - if( uncompressed[i] > a0 ) - { - a0 = uncompressed[i]; - } else if( uncompressed[i] < a1 ) - { - a1 = uncompressed[i]; - } - } - /* store those limits, and zero the rest of the compressed dataset */ - compressed[0] = a0; - compressed[1] = a1; - /* zero out the compressed data */ - compressed[2] = 0; - compressed[3] = 0; - compressed[4] = 0; - compressed[5] = 0; - compressed[6] = 0; - compressed[7] = 0; - /* store the all of the alpha values */ - next_bit = 8*2; - scale_me = 7.9999f / (a0 - a1); - for( i = 3; i < 16*4; i += 4 ) - { - /* convert this alpha value to a 3 bit number */ - int svalue; - int value = (int)((uncompressed[i] - a1) * scale_me); - svalue = swizzle8[ value&7 ]; - /* OK, store this value, start with the 1st byte */ - compressed[next_bit >> 3] |= svalue << (next_bit & 7); - if( (next_bit & 7) > 5 ) - { - /* spans 2 bytes, fill in the start of the 2nd byte */ - compressed[1 + (next_bit >> 3)] |= svalue >> (8 - (next_bit & 7) ); - } - next_bit += 3; - } - /* done compressing to DXT1 */ -} diff --git a/src/image_DXT.h b/src/image_DXT.h index c3381e5..4c4544d 100644 --- a/src/image_DXT.h +++ b/src/image_DXT.h @@ -10,119 +10,9 @@ #ifndef HEADER_IMAGE_DXT #define HEADER_IMAGE_DXT -/** - Converts an image from an array of unsigned chars (RGB or RGBA) to - DXT1 or DXT5, then saves the converted image to disk. - \return 0 if failed, otherwise returns 1 -**/ -int -save_image_as_DDS -( - const char *filename, - int width, int height, int channels, - const unsigned char *const data -); - -/** - take an image and convert it to DXT1 (no alpha) -**/ -unsigned char* -convert_image_to_DXT1 -( - const unsigned char *const uncompressed, - int width, int height, int channels, - int *out_size -); - -void convert_rgb_image_to_DXT1 ( +void _ogx_convert_rgb_image_to_DXT1 ( const unsigned char *const uncompressed, unsigned char *compressed, int width, int height, int red_blue_swap ); -/** - take an image and convert it to DXT5 (with alpha) -**/ -unsigned char* -convert_image_to_DXT5 -( - const unsigned char *const uncompressed, - int width, int height, int channels, - int *out_size -); - -/** A bunch of DirectDraw Surface structures and flags **/ -typedef struct -{ - unsigned int dwMagic; - unsigned int dwSize; - unsigned int dwFlags; - unsigned int dwHeight; - unsigned int dwWidth; - unsigned int dwPitchOrLinearSize; - unsigned int dwDepth; - unsigned int dwMipMapCount; - unsigned int dwReserved1[ 11 ]; - - /* DDPIXELFORMAT */ - struct - { - unsigned int dwSize; - unsigned int dwFlags; - unsigned int dwFourCC; - unsigned int dwRGBBitCount; - unsigned int dwRBitMask; - unsigned int dwGBitMask; - unsigned int dwBBitMask; - unsigned int dwAlphaBitMask; - } - sPixelFormat; - - /* DDCAPS2 */ - struct - { - unsigned int dwCaps1; - unsigned int dwCaps2; - unsigned int dwDDSX; - unsigned int dwReserved; - } - sCaps; - unsigned int dwReserved2; -} -DDS_header ; - -/* the following constants were copied directly off the MSDN website */ - -/* The dwFlags member of the original DDSURFACEDESC2 structure - can be set to one or more of the following values. */ -#define DDSD_CAPS 0x00000001 -#define DDSD_HEIGHT 0x00000002 -#define DDSD_WIDTH 0x00000004 -#define DDSD_PITCH 0x00000008 -#define DDSD_PIXELFORMAT 0x00001000 -#define DDSD_MIPMAPCOUNT 0x00020000 -#define DDSD_LINEARSIZE 0x00080000 -#define DDSD_DEPTH 0x00800000 - -/* DirectDraw Pixel Format */ -#define DDPF_ALPHAPIXELS 0x00000001 -#define DDPF_FOURCC 0x00000004 -#define DDPF_RGB 0x00000040 - -/* The dwCaps1 member of the DDSCAPS2 structure can be - set to one or more of the following values. */ -#define DDSCAPS_COMPLEX 0x00000008 -#define DDSCAPS_TEXTURE 0x00001000 -#define DDSCAPS_MIPMAP 0x00400000 - -/* The dwCaps2 member of the DDSCAPS2 structure can be - set to one or more of the following values. */ -#define DDSCAPS2_CUBEMAP 0x00000200 -#define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400 -#define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800 -#define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000 -#define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000 -#define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000 -#define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000 -#define DDSCAPS2_VOLUME 0x00200000 - #endif /* HEADER_IMAGE_DXT */