clsPng.cpp 5.3 KB
#include "clsPng.h"
#include"clsCrSurf.h"
namespace DmapCore_30
{
	clsPng::clsPng(void)
	{
		b = 0;
		//b3=0;
		//p1=0;	 
		patta = 0;
	}

	clsPng::~clsPng(void)
	{
		//	if(p1){delete p1;p1=0;}
		if (patta){ delete patta; patta = 0; };

		if (b){ free(b); b = 0; }
		//	if(b3){free(b3);b3=0;}
	}

	int clsPng::ReadPng(const char * fileName, unsigned int colorThis_)
	{
		try
		{
			//	b = (unsigned char *)malloc(1024);
			unsigned int colorThis = colorThis_; ((char *)&colorThis)[3] = 0;
			cairo_surface_t* image = cairo_image_surface_create_from_png(fileName);
			clsCrSurf clsCS(image);
			int width_image = cairo_image_surface_get_width(image); int height_image = cairo_image_surface_get_height(image);

			if (width_image == 0 || height_image == 0)return 1;
			{
				unsigned char* b3 = cairo_image_surface_get_data(image);
				b = (unsigned char *)malloc(4 * width_image*height_image);
				memcpy(b, b3, 4 * width_image*height_image);
			}
			for (int j = 0; j < height_image; j++)
			{
				int *c = ((int *)(b + 4 * (width_image * j)));
				for (int i = 0; i < width_image; i++, c++)
				{
					if (((unsigned char *)c)[3] == 0)
					{
						c[0] = colorThis;
					}
				}
			}
			int beforCount = 0, endCount = 0;
			for (int j = 0; j < height_image; j++)
			{
				bool found = false;
				int *c = ((int *)(b + 4 * (width_image * j)));
				for (int i = 0; i < width_image; i++, c++)
				{
					if (((unsigned char *)c)[3] == 0)continue;
					found = true; break;
				}
				if (found)break;
				beforCount++;
			}
			for (int j = height_image - 1; j >= 0; j--)
			{
				bool found = false;
				int *c = ((int *)(b + 4 * (width_image * j)));
				for (int i = 0; i < width_image; i++, c++)
				{
					if (((unsigned char *)c)[3] == 0)continue;
					found = true; break;
				}
				if (found)break;
				endCount++;
			}
			if (beforCount + endCount >= height_image){ return 1; }
			if (beforCount>0)
			{
				int hTemp = height_image - (beforCount + endCount);
				memmove(b, b + beforCount*width_image * 4, hTemp*width_image * 4);
				height_image = hTemp;
			}

			agg::rendering_buffer rbuf_img_aa;

			rbuf_img_aa.attach(b, width_image, height_image, (width_image)* 4);
			pattern_src_brightness_to_alpha_rgba8 p1(rbuf_img_aa);
			patta = new agg::line_image_pattern<agg::pattern_filter_bilinear_rgba8>(fltr, p1);
		}
		catch (...)
		{
			return (1);
		}
		return 0;

		/*
	//	globeLock.Log1Message("1","aa");
	char sz[2000];
	int len=strlen(fileName);
	for(int i=0;i<=len;i++)
	{
	if(fileName[i]=='/')
	{
	sz[i]='\\';
	}
	else
	{
	sz[i]=fileName[i];
	}
	}
	clsPngRead read;
	ZeroFile zeroFile;
	zeroFile.fp=fopen(sz,"rb");
	FILE *fp = zeroFile.fp;
	if (!fp)
	{
	return (1);
	}
	//globeLock.Log1Message("2","aa");
	try
	{
	int k=read.readpng_init(fp,&width,&height);
	png_uint_16 rr=0,gg=0,bb=0;
	k=read.readpng_get_bgcolor(&rr,&gg,&bb);
	//png_uint_32 rowbytes;
	b=read.readpng_get_image(0,&channels,&rowbytes);
	if(channels==3&&b)
	{
	int rowbytes1=4*rowbytes/3;
	png_byte *b1=(png_byte *)malloc(width*height*4);
	for(unsigned long j=0;j<height;j++)
	{
	for(unsigned long i=0;i<width;i++)
	{
	(b1+rowbytes1*j+i*4)[0]=(b+rowbytes*j+i*3)[0];
	(b1+rowbytes1*j+i*4)[1]=(b+rowbytes*j+i*3)[1];
	(b1+rowbytes1*j+i*4)[2]=(b+rowbytes*j+i*3)[2];
	(b1+rowbytes1*j+i*4)[3]=255;
	}
	}
	free(b);
	b=b1;
	rowbytes=4*rowbytes/3;
	}

	read.readpng_cleanup(0);
	//globeLock.Log1Message("3","aa");

	unsigned long j;
	for(j=0;j<height;j++)
	{
	for(unsigned long i=0;i<width;i++)
	{
	png_byte rrr=(b+rowbytes*j+i*4)[0];
	//png_byte ggg=(icon+rowbytes*j+i*4)[1];
	png_byte ggg=(b+rowbytes*j+i*4)[1];
	png_byte bbb=(b+rowbytes*j+i*4)[2];
	//unsigned char  aa[100];
	//memcpy(aa,b,sizeof(aa));

	if(rrr==0&&ggg==0&&bbb==0)
	{
	rrr=1;
	}
	else if(rrr==255&&ggg==255&&bbb==255)
	{
	//rrr=254;
	}

	(b+rowbytes*j+i*4)[0]=bbb;
	//ggg(icon+rowbytes*j+i*4)[1];
	(b+rowbytes*j+i*4)[2]=rrr;

	if(read.color_type==6)
	{
	if((b+rowbytes*j+i*4)[3]==0)
	{
	(b+rowbytes*j+i*4)[0]=255;
	(b+rowbytes*j+i*4)[1]=255;
	(b+rowbytes*j+i*4)[2]=255;
	}
	}
	}
	}
	//globeLock.Log1Message("4","aa");
	png_byte *b3=(png_byte *)malloc(width*height*3);
	png_byte *t=b3;
	for(j=0;j<height;j++)
	{
	for(unsigned long i=0;i<width;i++)
	{
	png_byte rrr=(b+rowbytes*j+i*4)[0];
	png_byte ggg=(b+rowbytes*j+i*4)[1];
	png_byte bbb=(b+rowbytes*j+i*4)[2];

	if((b+rowbytes*j+i*4)[3]==0)
	{
	rrr=ggg=bbb=255;
	}

	t[0]=rrr;t++;
	t[0]=ggg;t++;
	t[0]=bbb;t++;
	}
	}
	agg::rendering_buffer rbuf_img_aa;
	rbuf_img_aa.attach(b3,width,height,width * 3);pattern_src_brightness_to_alpha_rgba8 p1(rbuf_img_aa);
	//;p1=new pattern_src_brightness_to_alpha_rgba8(rbuf_img_aa);
	patta=new agg::line_image_pattern<agg::pattern_filter_bilinear_rgba8>(fltr,p1);

	free(b);b=b3;
	//free(p1);p1=0;
	//globeLock.Log1Message("5","aa");
	return 0;
	}
	catch(...)
	{
	return (1);
	}
	*/
		//return 0;
	}



	/*
		{
		clsPngRead read;
		FILE *fp = fopen("D:\\beijing\\temp\\2.png", "r+b");
		if (!fp)
		{
		return (ERROR);
		}
		unsigned long width,height;
		int k=read.readpng_init(fp,&width,&height);
		png_uint_16 rr=0,gg=0,bb=0;
		k=read.readpng_get_bgcolor(&rr,&gg,&bb);
		int channels;png_uint_32 rowbytes;
		png_byte *b=read.readpng_get_image(0,&channels,&rowbytes);

		HDC hdc=::GetDC(0);
		int cc=RGB(255,255,255);
		for(int j=0;j<height;j++);
		{
		png_byte *brow=b+j*rowbytes;
		for(int i=0;i<width;i++,brow+=channels)
		{
		int c=RGB(brow[0],brow[1],brow[2]);
		if(c!=cc){
		//	::SetPixel(hdc,i,j,c);
		}
		}
		}
		width=width;
		}
		*/

}