Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bmp_to_png.cpp File Reference
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include "../endianness.h"
#include <assert.h>

Go to the source code of this file.

Classes

struct  BITMAPINFOHEADER
 
struct  BITMAPFILEHEADER
 
struct  RGBQUAD
 

Typedefs

typedef unsigned int DWORD
 
typedef int LONG
 
typedef unsigned short WORD
 
typedef unsigned char BYTE
 

Functions

static unsigned char * LoadTex (char *FileName, int &sizeX, int &sizeY)
 holds all the textures in a huge hash table More...
 
void png_write (const char *myfile, unsigned char *data, unsigned int width, unsigned int height, bool alpha, char bpp)
 
int main (int argc, char **argv)
 

Variables

const int ltwid = 256
 
const int lthei = 256
 
const int SIZEOF_BITMAPFILEHEADER = sizeof (WORD)+sizeof (DWORD)+sizeof (WORD)+sizeof (WORD)+sizeof (DWORD)
 Defined for gcc which pads the size of structs. More...
 
const int SIZEOF_BITMAPINFOHEADER
 Defined for gcc which pads the size of structs. More...
 
const int SIZEOF_RGBQUAD = sizeof (BYTE)*4
 defined for gcc which pads size of structs (not entirely necessary) More...
 

Typedef Documentation

typedef unsigned char BYTE

Definition at line 12 of file bmp_to_png.cpp.

typedef unsigned int DWORD

Definition at line 9 of file bmp_to_png.cpp.

typedef int LONG

Definition at line 10 of file bmp_to_png.cpp.

typedef unsigned short WORD

Definition at line 11 of file bmp_to_png.cpp.

Function Documentation

static unsigned char* LoadTex ( char *  FileName,
int sizeX,
int sizeY 
)
static

holds all the textures in a huge hash table

Definition at line 72 of file bmp_to_png.cpp.

References BITMAPINFOHEADER::biBitCount, BITMAPINFOHEADER::biHeight, BITMAPINFOHEADER::biWidth, i, j, k, le16_to_cpu, le32_to_cpu, SIZEOF_BITMAPFILEHEADER, VSFileSystem::vs_close(), and VSFileSystem::vs_open().

Referenced by main().

73 {
74  unsigned char ctemp;
75  unsigned char *data = NULL;
76  FILE *fp = NULL;
77  fp = VSFileSystem::vs_open( FileName, "rb" );
78  if (!fp)
79  return false;
80  VSFileSystem::Fseek( fp, SIZEOF_BITMAPFILEHEADER, SEEK_SET );
81  long temp;
82  BITMAPINFOHEADER info;
83  VSFileSystem::Read( &info, SIZEOF_BITMAPINFOHEADER, 1, fp );
84  sizeX = le32_to_cpu( info.biWidth );
85  sizeY = le32_to_cpu( info.biHeight );
86  if (le16_to_cpu( info.biBitCount ) == 24) {
87  data = new unsigned char[3*sizeY*sizeX];
88  if (!data)
89  return false;
90  for (int i = sizeY-1; i >= 0; i--) {
91  int itimes3width = 3*i*sizeX; //speed speed speed (well if I really wanted speed Pos'd have wrote this function)
92  for (int j = 0; j < sizeX; j++) {
93  //for (int k=2; k>=0;k--)
94  //{
95  VSFileSystem::Read( data+3*j+itimes3width, sizeof (unsigned char)*3, 1, fp );
96  unsigned char tmp = data[3*j+itimes3width];
97  data[3*j+itimes3width] = data[3*j+itimes3width+2];
98  data[3*j+itimes3width+2] = tmp;
99  //}
100  }
101  }
102  } else if (le16_to_cpu( info.biBitCount ) == 8) {
103  data = new unsigned char[sizeY*sizeX*3];
104  unsigned char palette[256*3+1];
105  unsigned char *paltemp = palette;
106  for (int palcount = 0; palcount < 256; palcount++) {
107  VSFileSystem::Read( paltemp, sizeof (RGBQUAD), 1, fp );
108  //ctemp = paltemp[0];//don't reverse
109  //paltemp[0] = paltemp[2];
110  //paltemp[2] = ctemp;
111  paltemp += 3;
112  }
113  if (!data)
114  return false;
115  int k = 0;
116  for (int i = sizeY-1; i >= 0; i--)
117  for (int j = 0; j < sizeX; j++) {
118  VSFileSystem::Read( &ctemp, sizeof (unsigned char), 1, fp );
119  data[3*(i*sizeX+j)+2] = palette[( (short) ctemp )*3];
120  data[3*(i*sizeX+j)+1] = palette[( (short) ctemp )*3+1];
121  data[3*(i*sizeX+j)] = palette[( (short) ctemp )*3+2];
122  }
123  }
124  VSFileSystem::vs_close( fp );
125  return data;
126 }
int main ( int  argc,
char **  argv 
)

Definition at line 184 of file bmp_to_png.cpp.

References i, LoadTex(), and png_write().

185 {
186  FILE *fp1, fp2;
187  int out;
188  unsigned char *dat1 = NULL;
189  int wid1 = 0;
190  int hei1 = 0;
191  unsigned char *dat2 = NULL;
192  int wid2 = 0;
193  int hei2 = 0;
194  if (argc > 3) {
195  dat1 = LoadTex( argv[1], wid1, hei1 );
196  dat2 = LoadTex( argv[2], wid2, hei2 );
197  out = 3;
198  } else {
199  dat1 = LoadTex( argv[1], wid1, hei1 );
200  out = 2;
201  }
202  unsigned char *data = dat1;
203  if (!dat1)
204  return 0;
205  if (argc > 3 && dat2) {
206  assert( wid1 == wid2 );
207  assert( hei1 == hei2 );
208  data = (unsigned char*) malloc( sizeof (unsigned char)*wid1*hei1*4 );
209  for (int i = 0; i < wid1*hei1; i++) {
210  data[i*4] = dat1[i*3];
211  data[i*4+1] = dat1[i*3+1];
212  data[i*4+2] = dat1[i*3+2];
213  data[i*4+3] = dat2[i*3];
214  }
215  }
216  png_write( argv[out], data, wid1, hei1, dat2 != NULL, 8 );
217  if (argc > 3 && dat2)
218  free( dat1 );
219  if (dat2)
220  free( dat2 );
221  return 0;
222 }
void png_write ( const char *  myfile,
unsigned char *  data,
unsigned int  width,
unsigned int  height,
bool  alpha,
char  bpp 
)

Definition at line 128 of file bmp_to_png.cpp.

References height, i, VSFileSystem::vs_close(), VSFileSystem::vs_open(), and width.

Referenced by main().

129 {
130  FILE *fp = VSFileSystem::vs_open( myfile, "wb" );
131  png_structp png_ptr = png_create_write_struct
132  ( PNG_LIBPNG_VER_STRING, (png_voidp) NULL, NULL, NULL );
133  if (!png_ptr)
134  return;
135  png_infop info_ptr = png_create_info_struct( png_ptr );
136  if (!info_ptr) {
137  png_destroy_write_struct( &png_ptr, (png_infopp) NULL );
138  return;
139  }
140  if ( setjmp( png_ptr->jmpbuf ) ) {
141  png_destroy_write_struct( &png_ptr, &info_ptr );
143  return;
144  }
145  png_init_io( png_ptr, fp );
146  png_set_filter( png_ptr, 0, PNG_FILTER_NONE );
147  png_set_compression_level( png_ptr, Z_BEST_COMPRESSION );
148 
149  /* set other zlib parameters */
150  png_set_compression_mem_level( png_ptr, 8 );
151  png_set_compression_strategy( png_ptr, Z_DEFAULT_STRATEGY );
152  png_set_compression_window_bits( png_ptr, 15 );
153  png_set_compression_method( png_ptr, 8 );
154 
155  png_set_IHDR( png_ptr,
156  info_ptr,
157  width,
158  height,
159  bpp,
160  alpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
161  PNG_INTERLACE_NONE,
162  PNG_COMPRESSION_TYPE_DEFAULT,
163  PNG_FILTER_TYPE_DEFAULT );
164 
165  png_write_info( png_ptr, info_ptr );
166 # if __BYTE_ORDER != __BIG_ENDIAN
167  if (bpp == 16)
168  png_set_swap( png_ptr );
169 #endif
170  int stride = (bpp/8)*(alpha ? 4 : 3);
171  png_byte **row_pointers = new png_byte*[height];
172  for (unsigned int i = 0; i < height; i++)
173  row_pointers[i] = (png_byte*) &data[stride*i*width];
174  png_write_image( png_ptr, row_pointers );
175  png_write_end( png_ptr, info_ptr );
176  png_write_flush( png_ptr );
177  png_destroy_write_struct( &png_ptr, &info_ptr );
178 
180  free( data );
181  delete[] row_pointers;
182 }

Variable Documentation

const int lthei = 256

Definition at line 7 of file bmp_to_png.cpp.

const int ltwid = 256

Definition at line 6 of file bmp_to_png.cpp.

const int SIZEOF_BITMAPFILEHEADER = sizeof (WORD)+sizeof (DWORD)+sizeof (WORD)+sizeof (WORD)+sizeof (DWORD)

Defined for gcc which pads the size of structs.

Definition at line 64 of file bmp_to_png.cpp.

Referenced by LoadTex().

const int SIZEOF_BITMAPINFOHEADER
Initial value:
= sizeof (DWORD)+sizeof (LONG)+sizeof (LONG)+2*sizeof (WORD)+2*sizeof (DWORD)+2
*sizeof (LONG)+2*sizeof (DWORD)

Defined for gcc which pads the size of structs.

Definition at line 66 of file bmp_to_png.cpp.

const int SIZEOF_RGBQUAD = sizeof (BYTE)*4

defined for gcc which pads size of structs (not entirely necessary)

Definition at line 69 of file bmp_to_png.cpp.