diff src/common/Util.cpp @ 41:f94fa48624d0

upgraded Util.cpp to work with libpng 1.5
author Robert McIntyre <rlm@mit.edu>
date Tue, 06 Mar 2012 12:38:47 -0600
parents f9f4f1b99eed
children a6d060a64246
line wrap: on
line diff
     1.1 --- a/src/common/Util.cpp	Mon Mar 05 16:37:58 2012 -0600
     1.2 +++ b/src/common/Util.cpp	Tue Mar 06 12:38:47 2012 -0600
     1.3 @@ -284,144 +284,144 @@
     1.4  
     1.5  bool utilWritePNGFile(const char *fileName, int w, int h, u8 *pix)
     1.6  {
     1.7 -	u8 writeBuffer[256 * 3];
     1.8 +  u8 writeBuffer[256 * 3];
     1.9  
    1.10 -	FILE *fp = fopen(fileName, "wb");
    1.11 +  FILE *fp = fopen(fileName, "wb");
    1.12  
    1.13 -	if (!fp)
    1.14 -	{
    1.15 -		systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
    1.16 -		return false;
    1.17 -	}
    1.18 +  if (!fp)
    1.19 +    {
    1.20 +      systemMessage(MSG_ERROR_CREATING_FILE, N_("Error creating file %s"), fileName);
    1.21 +      return false;
    1.22 +    }
    1.23  
    1.24 -	png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
    1.25 -	                                              NULL,
    1.26 -	                                              NULL,
    1.27 -	                                              NULL);
    1.28 -	if (!png_ptr)
    1.29 -	{
    1.30 -		fclose(fp);
    1.31 -		return false;
    1.32 -	}
    1.33 +  png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
    1.34 +						NULL,
    1.35 +						NULL,
    1.36 +						NULL);
    1.37 +  if (!png_ptr)
    1.38 +    {
    1.39 +      fclose(fp);
    1.40 +      return false;
    1.41 +    }
    1.42  
    1.43 -	png_infop info_ptr = png_create_info_struct(png_ptr);
    1.44 +  png_infop info_ptr = png_create_info_struct(png_ptr);
    1.45  
    1.46 -	if (!info_ptr)
    1.47 -	{
    1.48 -		png_destroy_write_struct(&png_ptr, NULL);
    1.49 -		fclose(fp);
    1.50 -		return false;
    1.51 -	}
    1.52 +  if (!info_ptr)
    1.53 +    {
    1.54 +      png_destroy_write_struct(&png_ptr, NULL);
    1.55 +      fclose(fp);
    1.56 +      return false;
    1.57 +    }
    1.58  
    1.59 -	if (setjmp(png_ptr->jmpbuf))
    1.60 -	{
    1.61 -		png_destroy_write_struct(&png_ptr, NULL);
    1.62 -		fclose(fp);
    1.63 -		return false;
    1.64 -	}
    1.65 +  if (setjmp(png_jmpbuf(png_ptr)))
    1.66 +    {
    1.67 +      png_destroy_write_struct(&png_ptr, NULL);
    1.68 +      fclose(fp);
    1.69 +      return false;
    1.70 +    }
    1.71  
    1.72 -	png_init_io(png_ptr, fp);
    1.73 +  png_init_io(png_ptr, fp);
    1.74  
    1.75 -	png_set_IHDR(png_ptr,
    1.76 -	             info_ptr,
    1.77 -	             w,
    1.78 -	             h,
    1.79 -	             8,
    1.80 -	             PNG_COLOR_TYPE_RGB,
    1.81 -	             PNG_INTERLACE_NONE,
    1.82 -	             PNG_COMPRESSION_TYPE_DEFAULT,
    1.83 -	             PNG_FILTER_TYPE_DEFAULT);
    1.84 +  png_set_IHDR(png_ptr,
    1.85 +	       info_ptr,
    1.86 +	       w,
    1.87 +	       h,
    1.88 +	       8,
    1.89 +	       PNG_COLOR_TYPE_RGB,
    1.90 +	       PNG_INTERLACE_NONE,
    1.91 +	       PNG_COMPRESSION_TYPE_DEFAULT,
    1.92 +	       PNG_FILTER_TYPE_DEFAULT);
    1.93  
    1.94 -	png_write_info(png_ptr, info_ptr);
    1.95 +  png_write_info(png_ptr, info_ptr);
    1.96  
    1.97 -	u8 *b = writeBuffer;
    1.98 +  u8 *b = writeBuffer;
    1.99  
   1.100 -	int sizeX = w;
   1.101 -	int sizeY = h;
   1.102 +  int sizeX = w;
   1.103 +  int sizeY = h;
   1.104  
   1.105 -	switch (systemColorDepth)
   1.106 -	{
   1.107 -	case 16:
   1.108 -	{
   1.109 -		u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line
   1.110 -		for (int y = 0; y < sizeY; y++)
   1.111 -		{
   1.112 -			for (int x = 0; x < sizeX; x++)
   1.113 -			{
   1.114 -				u16 v = *p++;
   1.115 +  switch (systemColorDepth)
   1.116 +    {
   1.117 +    case 16:
   1.118 +      {
   1.119 +	u16 *p = (u16 *)(pix + (w + 2) * 2); // skip first black line
   1.120 +	for (int y = 0; y < sizeY; y++)
   1.121 +	  {
   1.122 +	    for (int x = 0; x < sizeX; x++)
   1.123 +	      {
   1.124 +		u16 v = *p++;
   1.125  
   1.126 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.127 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.128 -				*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.129 -			}
   1.130 -			p++; // skip black pixel for filters
   1.131 -			p++; // skip black pixel for filters
   1.132 -			png_write_row(png_ptr, writeBuffer);
   1.133 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.134 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.135 +		*b++ = ((v >> systemBlueShift) & 0x01f) << 3; // B
   1.136 +	      }
   1.137 +	    p++; // skip black pixel for filters
   1.138 +	    p++; // skip black pixel for filters
   1.139 +	    png_write_row(png_ptr, writeBuffer);
   1.140  
   1.141 -			b = writeBuffer;
   1.142 -		}
   1.143 -		break;
   1.144 -	}
   1.145 -	case 24:
   1.146 -	{
   1.147 -		u8 *pixU8 = (u8 *)pix;
   1.148 -		for (int y = 0; y < sizeY; y++)
   1.149 -		{
   1.150 -			for (int x = 0; x < sizeX; x++)
   1.151 -			{
   1.152 -				if (systemRedShift < systemBlueShift)
   1.153 -				{
   1.154 -					*b++ = *pixU8++; // R
   1.155 -					*b++ = *pixU8++; // G
   1.156 -					*b++ = *pixU8++; // B
   1.157 -				}
   1.158 -				else
   1.159 -				{
   1.160 -					int blue  = *pixU8++;
   1.161 -					int green = *pixU8++;
   1.162 -					int red	  = *pixU8++;
   1.163 +	    b = writeBuffer;
   1.164 +	  }
   1.165 +	break;
   1.166 +      }
   1.167 +    case 24:
   1.168 +      {
   1.169 +	u8 *pixU8 = (u8 *)pix;
   1.170 +	for (int y = 0; y < sizeY; y++)
   1.171 +	  {
   1.172 +	    for (int x = 0; x < sizeX; x++)
   1.173 +	      {
   1.174 +		if (systemRedShift < systemBlueShift)
   1.175 +		  {
   1.176 +		    *b++ = *pixU8++; // R
   1.177 +		    *b++ = *pixU8++; // G
   1.178 +		    *b++ = *pixU8++; // B
   1.179 +		  }
   1.180 +		else
   1.181 +		  {
   1.182 +		    int blue  = *pixU8++;
   1.183 +		    int green = *pixU8++;
   1.184 +		    int red	  = *pixU8++;
   1.185  
   1.186 -					*b++ = red;
   1.187 -					*b++ = green;
   1.188 -					*b++ = blue;
   1.189 -				}
   1.190 -			}
   1.191 -			png_write_row(png_ptr, writeBuffer);
   1.192 +		    *b++ = red;
   1.193 +		    *b++ = green;
   1.194 +		    *b++ = blue;
   1.195 +		  }
   1.196 +	      }
   1.197 +	    png_write_row(png_ptr, writeBuffer);
   1.198  
   1.199 -			b = writeBuffer;
   1.200 -		}
   1.201 -		break;
   1.202 -	}
   1.203 -	case 32:
   1.204 -	{
   1.205 -		u32 *pixU32 = (u32 *)(pix + 4 * (w + 1));
   1.206 -		for (int y = 0; y < sizeY; y++)
   1.207 -		{
   1.208 -			for (int x = 0; x < sizeX; x++)
   1.209 -			{
   1.210 -				u32 v = *pixU32++;
   1.211 +	    b = writeBuffer;
   1.212 +	  }
   1.213 +	break;
   1.214 +      }
   1.215 +    case 32:
   1.216 +      {
   1.217 +	u32 *pixU32 = (u32 *)(pix + 4 * (w + 1));
   1.218 +	for (int y = 0; y < sizeY; y++)
   1.219 +	  {
   1.220 +	    for (int x = 0; x < sizeX; x++)
   1.221 +	      {
   1.222 +		u32 v = *pixU32++;
   1.223  
   1.224 -				*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.225 -				*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.226 -				*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.227 -			}
   1.228 -			pixU32++;
   1.229 +		*b++ = ((v >> systemRedShift) & 0x001f) << 3; // R
   1.230 +		*b++ = ((v >> systemGreenShift) & 0x001f) << 3; // G
   1.231 +		*b++ = ((v >> systemBlueShift) & 0x001f) << 3; // B
   1.232 +	      }
   1.233 +	    pixU32++;
   1.234  
   1.235 -			png_write_row(png_ptr, writeBuffer);
   1.236 +	    png_write_row(png_ptr, writeBuffer);
   1.237  
   1.238 -			b = writeBuffer;
   1.239 -		}
   1.240 -		break;
   1.241 -	}
   1.242 -	}
   1.243 +	    b = writeBuffer;
   1.244 +	  }
   1.245 +	break;
   1.246 +      }
   1.247 +    }
   1.248  
   1.249 -	png_write_end(png_ptr, info_ptr);
   1.250 +  png_write_end(png_ptr, info_ptr);
   1.251  
   1.252 -	png_destroy_write_struct(&png_ptr, &info_ptr);
   1.253 +  png_destroy_write_struct(&png_ptr, &info_ptr);
   1.254  
   1.255 -	fclose(fp);
   1.256 +  fclose(fp);
   1.257  
   1.258 -	return true;
   1.259 +  return true;
   1.260  }
   1.261  
   1.262  static int utilReadInt2(FILE *f)