| OLD | NEW |
| (Empty) |
| 1 // | |
| 2 // Book: OpenGL(R) ES 2.0 Programming Guide | |
| 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | |
| 4 // ISBN-10: 0321502795 | |
| 5 // ISBN-13: 9780321502797 | |
| 6 // Publisher: Addison-Wesley Professional | |
| 7 // URLs: http://safari.informit.com/9780321563835 | |
| 8 // http://www.opengles-book.com | |
| 9 // | |
| 10 | |
| 11 // esUtil_TGA.c | |
| 12 // | |
| 13 // This file contains the Win32 implementation of a TGA image loader | |
| 14 | |
| 15 #ifndef WIN32_LEAN_AND_MEAN | |
| 16 #define WIN32_LEAN_AND_MEAN | |
| 17 #endif // WIN32_LEAN_AND_MEAN | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include <stdio.h> | |
| 21 #include <stdlib.h> | |
| 22 | |
| 23 /// | |
| 24 // Macros | |
| 25 // | |
| 26 #define INVERTED_BIT (1 << 5) | |
| 27 | |
| 28 /// | |
| 29 // Types | |
| 30 // | |
| 31 #pragma pack(push,x1) // Byte alignment (8-bit) | |
| 32 #pragma pack(1) | |
| 33 | |
| 34 typedef struct | |
| 35 { | |
| 36 unsigned char IdSize, | |
| 37 MapType, | |
| 38 ImageType; | |
| 39 unsigned short PaletteStart, | |
| 40 PaletteSize; | |
| 41 unsigned char PaletteEntryDepth; | |
| 42 unsigned short X, | |
| 43 Y, | |
| 44 Width, | |
| 45 Height; | |
| 46 unsigned char ColorDepth, | |
| 47 Descriptor; | |
| 48 | |
| 49 } TGA_HEADER; | |
| 50 | |
| 51 #pragma pack(pop,x1) | |
| 52 | |
| 53 ////////////////////////////////////////////////////////////////////////////////
//// | |
| 54 // | |
| 55 // Private Functions | |
| 56 // | |
| 57 | |
| 58 ////////////////////////////////////////////////////////////////////////////////
//// | |
| 59 // | |
| 60 // Public Functions | |
| 61 // | |
| 62 // | |
| 63 | |
| 64 | |
| 65 /// | |
| 66 // WinTGALoad() | |
| 67 // | |
| 68 int WinTGALoad( const char *fileName, char **buffer, int *width, int *height ) | |
| 69 { | |
| 70 FILE *fp; | |
| 71 TGA_HEADER Header; | |
| 72 | |
| 73 if ( fopen_s ( &fp, fileName, "rb" ) != 0 ) | |
| 74 { | |
| 75 return FALSE; | |
| 76 } | |
| 77 | |
| 78 if ( fp == NULL ) | |
| 79 { | |
| 80 return FALSE; | |
| 81 } | |
| 82 | |
| 83 fread ( &Header, sizeof(TGA_HEADER), 1, fp ); | |
| 84 | |
| 85 *width = Header.Width; | |
| 86 *height = Header.Height; | |
| 87 | |
| 88 if ( Header.ColorDepth == 24 ) | |
| 89 { | |
| 90 RGBTRIPLE *Buffer24; | |
| 91 | |
| 92 Buffer24= (RGBTRIPLE*)malloc(sizeof(RGBTRIPLE) * (*width) * (*height)); | |
| 93 | |
| 94 if(Buffer24) | |
| 95 { | |
| 96 int i=0; | |
| 97 int x, | |
| 98 y; | |
| 99 | |
| 100 fread(Buffer24, sizeof(RGBTRIPLE), (*width) * (*height), fp); | |
| 101 | |
| 102 *buffer= (LPSTR) malloc(3 * (*width) * (*height)); | |
| 103 | |
| 104 for ( y = 0; y < *height; y++ ) | |
| 105 for( x = 0; x < *width; x++ ) | |
| 106 { | |
| 107 int Index= y * (*width) + x; | |
| 108 | |
| 109 if(!(Header.Descriptor & INVERTED_BIT)) | |
| 110 Index= ((*height) - 1 - y) * (*width) + x; | |
| 111 | |
| 112 (*buffer)[(i * 3)]= Buffer24[Index].rgbtRed; | |
| 113 (*buffer)[(i * 3) + 1]= Buffer24[Index].rgbtGreen; | |
| 114 (*buffer)[(i * 3) + 2]= Buffer24[Index].rgbtBlue; | |
| 115 | |
| 116 i++; | |
| 117 } | |
| 118 | |
| 119 fclose(fp); | |
| 120 free(Buffer24); | |
| 121 return(TRUE); | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 return(FALSE); | |
| 126 } | |
| OLD | NEW |