int
res_create_alpha_surface(const char *name, gr_surface *pSurface)
{
	int result = 0;
	unsigned int y;
	unsigned char *p_row;
	gr_surface surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height;
	png_byte channels;
	FILE *fp = NULL;

	*pSurface = NULL;

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	if (channels != 1) {
		result = -7;
		goto exit;
	}

	if (!(surface = malloc_surface(width * height))) {
		result = -8;
		goto exit;
	}

	surface->width = width;
	surface->height = height;
	surface->row_bytes = width;
	surface->pixel_bytes = 1;

	for (y = 0; y < height; y++) {
		p_row = surface->data + y * surface->row_bytes;
		png_read_row(png_ptr, p_row, NULL);
	}

	*pSurface = surface;
exit:
	close_png(&png_ptr, &info_ptr, fp);
	if (result < 0 && surface != NULL)
		free(surface);

	return result;
}
int
res_create_display_surface(const char *name, gr_surface *pSurface)
{
	int result = 0;
	unsigned int y;
	unsigned char *p_row;
	gr_surface surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height;
	png_byte channels;
	FILE *fp = NULL;

	*pSurface = NULL;

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	if (!(surface = init_display_surface(width, height))) {
		result = -8;
		goto exit;
	}

	/* TODO: check for error */
	p_row = malloc(width * 4);
	for (y = 0; y < height; y++) {
		png_read_row(png_ptr, p_row, NULL);
		transform_rgb_to_draw(p_row,
				      surface->data + y * surface->row_bytes,
				      channels, width);
	}

	free(p_row);

	*pSurface = surface;

exit:
	close_png(&png_ptr, &info_ptr, fp);
	if (result < 0 && surface != NULL)
		free(surface);

	return result;
}
Пример #3
0
/*
 * Parameters: <hundred_dpi> <basename_> <titlestring> <bottom_html_code>
 * <pageoffset> [first page number]
 */
int
main(int argc, char **argv)
{
        int a,z;
 
        if (argc<7){
                fprintf(stderr,"Usage: pbm2png <hundred_dpi> <basename_> <titlestring> <bottom_html_code> <pageoffset> <ifname> [starting_filenumber]\n");
        return 0;
        }
        dpi=atof(argv[1])/10;
        ppm=dpi*1000/25.4;
        basename_=argv[2];
        titlestring=argv[3];
	bottom=argv[4];
        pageoffset=atol(argv[5]);
        filename=argv[6];
	again0:
        ifd=open(filename,O_RDONLY);
	if (ifd<0){
		if (errno==EAGAIN||errno==EINTR||errno==EWOULDBLOCK) goto
			again0;
		else {
			perror("");
			exit(1);
		}
	}
        fprintf(stderr,"filename %s, %d\n",filename,ifd);
        if (argc>=8){
                filenumber=atol(argv[7]);
        }
        gentables();
        again:
        fprintf(stderr,"\nFile %i\n",filenumber);
        if (read_header()){
                for (a=0;a<filenumber;a++)
                        make_page(a);
		make_index();
                return 0;
        }
        lw=(x+15)>>4;
        lb=(x+7)>>3;
        ox=x/17;
        oy=y/15;
        fprintf(stderr,"Input: %i*%i pixels, %f*%f dpi, %.1fMB.\n",x,y,dpi*17,dpi*15,(float)lb*y/1048576);
        fprintf(stderr,"Ouput: %i*%i pixels, %f*%f dpi, %.1fKB raw data.\n",ox,oy,dpi,dpi,(float)ox*oy/1024);
        l1=(unsigned char*)malloc(lw*2);
        l2=(unsigned long*)malloc(lw*sizeof(unsigned long));
        l4=(unsigned long long*)malloc(lw*sizeof(unsigned long long));
        sprintf(string,"%s%d.png",basename_,filenumber);
        filenumber++;
        of=fopen(string,"w");
        open_png();
        for (z=oy;z;z--){
                if (!(z&15)){
                        fprintf(stderr,".");
                        fflush(stderr);
                } 
                load_to_4();
                export_from_4();
        }
        close_png();
        fprintf(stderr,"\nWritten %lu bytes of data, ratio %.1f%%\n",ftell(of),(float)ftell(of)*100/ox/oy);
        fclose(of);
        for (a=y%15;a;a--)
                load();
        free(l1);
        free(l2);
        free(l4);
        goto again;
}
int
res_create_localized_alpha_surface(const char *name, const char *locale,
				   gr_surface *pSurface)
{
	int result = 0;
	unsigned char *row;
	gr_surface surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height, y;
	png_byte channels;
	FILE *fp = NULL;

	*pSurface = NULL;

	if (!locale) {
		surface = malloc_surface(0);
		surface->width = 0;
		surface->height = 0;
		surface->row_bytes = 0;
		surface->pixel_bytes = 1;
		goto exit;
	}

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	if (channels != 1) {
		result = -7;
		goto exit;
	}

	/* TODO: check for error */
	row = malloc(width);
	for (y = 0; y < height; y++) {
		int h, w;
		char *loc;

		png_read_row(png_ptr, row, NULL);
		w = (row[1] << 8) | row[0];
		h = (row[3] << 8) | row[2];
		loc = (char *)row + 5;

		if (y + 1 + h >= height || matches_locale(loc, locale)) {
			int i;

			printf("  %20s: %s (%d x %d @ %ld)\n", name, loc, w,
			       h, (long)y);

			if (!(surface = malloc_surface(w * h))) {
				result = -8;
				goto exit;
			}

			surface->width = w;
			surface->height = h;
			surface->row_bytes = w;
			surface->pixel_bytes = 1;

			for (i = 0; i < h; i++, y++) {
				png_read_row(png_ptr, row, NULL);
				memcpy(surface->data + i * w, row, w);
			}

			*pSurface = (gr_surface)surface;
			break;
		} else {
			int i;

			for (i = 0; i < h; i++, y++)
				png_read_row(png_ptr, row, NULL);
		}
	}

exit:
	close_png(&png_ptr, &info_ptr, fp);
	if (result < 0 && surface)
		free(surface);

	return result;
}
int
res_create_multi_display_surface(const char *name, int *frames,
				 gr_surface **pSurface)
{
	int i, result = 0, num_text;
	unsigned int y;
	unsigned char *p_row;
	gr_surface *surface = NULL;
	png_structp png_ptr = NULL;
	png_infop info_ptr = NULL;
	png_uint_32 width, height;
	png_byte channels = 0;
	png_textp text;
	FILE *fp = NULL;

	*pSurface = NULL;
	*frames = -1;

	result = open_png(name, &png_ptr, &info_ptr, &fp, &width, &height,
			  &channels);
	if (result < 0)
		return result;

	*frames = 1;
	if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
		for (i = 0; i < num_text; i++)
			if (text[i].key && !strcmp(text[i].key, "Frames") &&
			    text[i].text) {
				*frames = atoi(text[i].text);
				break;
			}

		printf("  found frames = %d\n", *frames);
	}

	if (height % *frames != 0) {
		printf("bad height (%ld) for frame count (%d)\n",
		       (long)height, *frames);
		result = -9;
		goto exit;
	}

	if (!(surface = malloc(*frames * sizeof(gr_surface)))) {
		result = -8;
		goto exit;
	}

	for (i = 0; i < *frames; i++) {
		surface[i] = init_display_surface(width, height / *frames);
		if (!surface[i]) {
			result = -8;
			goto exit;
		}
	}

	/* TODO: Check for error */
	p_row = malloc(width * 4);
	for (y = 0; y < height; y++) {
		int frame = y % *frames;
		unsigned char *out_row;

		png_read_row(png_ptr, p_row, NULL);
		out_row = surface[frame]->data + (y / *frames) *
			  surface[frame]->row_bytes;
		transform_rgb_to_draw(p_row, out_row, channels, width);
	}

	free(p_row);

	*pSurface = (gr_surface *)surface;

exit:
	close_png(&png_ptr, &info_ptr, fp);

	if (result < 0)
		if (surface) {
			for (i = 0; i < *frames; i++)
				if (surface[i])
					free(surface[i]);

			free(surface);
		}

	return result;
}