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
tex_transform.cpp
Go to the documentation of this file.
1 #include "vsimage.h"
2 
3 #include <png.h>
4 
5 unsigned char * texTransform( int &bpp, int &color_type, unsigned long &width, unsigned long &height, unsigned char **rp )
6 {
7  unsigned char *data;
8  unsigned int row_size;
9  assert( bpp == 8 );
10  if ( (color_type&PNG_HAS_PALETTE) || ( !(color_type&PNG_HAS_COLOR) ) )
11  row_size = width*(bpp/8)*sizeof (unsigned char)*( (color_type&PNG_HAS_ALPHA) ? 2 : 1 );
12  else
13  row_size = width*(bpp/8)*sizeof (unsigned char)*( (color_type&PNG_HAS_ALPHA) ? 4 : 3 );
14  data = (unsigned char*) malloc( row_size*height );
15  for (unsigned int i = 0; i < height; i++)
16  memcpy( data+i*row_size, rp[i], row_size );
17  return data;
18 }
19 
20 unsigned char * heightmapTransform( int &bpp,
21  int &color_type,
22  unsigned long &width,
23  unsigned long &height,
24  unsigned char **row_pointers )
25 {
26  unsigned short *dat = (unsigned short*) malloc( sizeof (unsigned short)*width*height );
27  if ( (bpp == 8 && color_type
28  == PNG_COLOR_TYPE_RGB_ALPHA) || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) {
29  if (bpp == 8 && color_type == PNG_COLOR_TYPE_GRAY) {
30  for (unsigned int i = 0; i < height; i++) {
31  unsigned long iwid = i*width;
32  for (unsigned int j = 0; j < width; j++)
33  dat[iwid+j] = row_pointers[i][j];
34  }
35  } else {
36  if ( (bpp == 16 && color_type == PNG_COLOR_TYPE_GRAY) || (bpp == 8 && color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ) {
37  for (unsigned int i = 0; i < height; i++)
38  memcpy( &dat[i*width], row_pointers[i], sizeof (unsigned short)*width );
39  } else {
40  //type is RGBA32 or GrayA32
41  for (unsigned int i = 0; i < height; i++) {
42  unsigned long iwid = i*width;
43  for (unsigned int j = 0; j < width; j++)
44  dat[iwid+j] = ( ( (unsigned short*) row_pointers[i] )[j*2] );
45  }
46  }
47  }
48  } else {
49  if (color_type == PNG_COLOR_TYPE_RGB) {
50  unsigned int coloffset = (bpp == 8) ? 3 : 6;
51  for (unsigned int i = 0; i < height; i++) {
52  unsigned long iwid = i*width;
53  for (unsigned int j = 0; j < width; j++)
54  dat[iwid+j] = *( (unsigned short*) ( &(row_pointers[i][j*coloffset]) ) );
55  }
56  } else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
58  for (unsigned int i = 0; i < height; i++) {
59  unsigned long iwid = i*width;
60  for (unsigned int j = 0; j < width; j++)
61  dat[iwid+j] = ( ( (unsigned short*) row_pointers[i] )[j*4] );
62  }
63  }
64  }
65  bpp = 16;
66  color_type = PNG_COLOR_TYPE_GRAY;
67  return (unsigned char*) dat;
68 }
69 
70 unsigned char * terrainTransform( int &bpp,
71  int &color_type,
72  unsigned long &width,
73  unsigned long &height,
74  unsigned char **row_pointers )
75 {
76  unsigned char *dat = (unsigned char*) malloc( sizeof (unsigned char)*width*height );
77  if ( (bpp == 8 && color_type
78  == PNG_COLOR_TYPE_RGB_ALPHA) || color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA ) {
79  if (bpp == 8 && color_type == PNG_COLOR_TYPE_GRAY) {
80  for (unsigned int i = 0; i < height; i++)
81  memcpy( &dat[i*width], row_pointers[i], sizeof (unsigned char)*width );
82  } else {
83  if ( (bpp == 16 && color_type == PNG_COLOR_TYPE_GRAY) || (bpp == 8 && color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ) {
84  for (unsigned int i = 0; i < height; i++) {
85  unsigned long iwid = i*width;
86  for (unsigned int j = 0; j < width; j++)
87  dat[iwid+j] = (row_pointers[i])[j*2];
88  }
89  } else {
90  //type is RGBA32 or GrayA32
91  for (unsigned int i = 0; i < height; i++) {
92  unsigned long iwid = i*width;
93  for (unsigned int j = 0; j < width; j++)
94  dat[iwid+j] = ( (row_pointers[i])[j*4] );
95  }
96  }
97  }
98  } else {
99  if (color_type == PNG_COLOR_TYPE_RGB) {
100  unsigned int coloffset = (bpp == 8) ? 3 : 6;
101  for (unsigned int i = 0; i < height; i++) {
102  unsigned long iwid = i*width;
103  for (unsigned int j = 0; j < width; j++)
104  dat[iwid+j] = *( (unsigned char*) ( &(row_pointers[i][j*coloffset]) ) );
105  }
106  } else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
108  for (unsigned int i = 0; i < height; i++) {
109  unsigned long iwid = i*width;
110  for (unsigned int j = 0; j < width; j++)
111  dat[iwid+j] = ( ( (unsigned short*) row_pointers[i] )[j*4] )/256;
112  }
113  }
114  }
115  bpp = 8;
116  color_type = PNG_COLOR_TYPE_GRAY;
117 
118  return (unsigned char*) dat;
119 }
120