struct bufferAndSize pngQuant(MagickWand *output){

      unsigned long wid = MagickGetImageWidth(output);
      unsigned long hei = MagickGetImageHeight(output);
      unsigned char *bmp_buffer;
      bmp_buffer = (unsigned char*) malloc(wid*hei*4);
      MagickGetImagePixels(output,0,0,wid,hei,"RGBA",CharPixel,bmp_buffer);

      liq_attr *attr = liq_attr_create();
      liq_image *qimage = liq_image_create_rgba(attr, bmp_buffer, wid, hei, 0);
      liq_set_max_colors(attr, 255);
      liq_set_speed(attr, 10);
      liq_result *res = liq_quantize_image(attr, qimage);

      int png_buffer_size = wid*hei;
      unsigned char *png_buffer = malloc(png_buffer_size);

      liq_write_remapped_image(res, qimage, png_buffer, png_buffer_size);
      const liq_palette *pal = liq_get_palette(res);

      LodePNGState state;

      lodepng_state_init(&state);
      /*optionally customize the state*/
      state.info_png.color.bitdepth = 8;
      state.info_png.color.colortype = LCT_PALETTE;
      state.info_raw.colortype = LCT_PALETTE;
      state.info_raw.bitdepth = 8;
      state.encoder.auto_convert = 0;
      state.encoder.add_id = 0;
      state.encoder.zlibsettings.nicematch = 258;
      int i = 0;
      for (i=0; i < pal->count; ++i)
      {
        lodepng_palette_add(&state.info_png.color, pal->entries[i].r , pal->entries[i].g, pal->entries[i].b, pal->entries[i].a);
        lodepng_palette_add(&state.info_raw, pal->entries[i].r , pal->entries[i].g, pal->entries[i].b, pal->entries[i].a);
      }
      unsigned char *data;
      size_t size = 0;
      lodepng_encode(&data, &size, png_buffer, wid, hei, &state);
      // writtendata = io_write(size, data);
      lodepng_state_cleanup(&state);
      free(bmp_buffer);
      free(png_buffer);
      liq_attr_destroy(attr);
      liq_image_destroy(qimage);
      liq_result_destroy(res);

      lodepng_state_cleanup(&state);
      struct bufferAndSize bs;
      bs.data = data;
      bs.size = size;
      return bs;
}
SDL_Surface *TCOD_sys_read_png(const char *filename) {
	unsigned error;
	unsigned char* image;
	unsigned width, height, y, bpp;
	unsigned char* png;
	size_t pngsize;
	LodePNGState state;
	SDL_Surface *bitmap;
	unsigned char *source;
	unsigned int rowsize;

	lodepng_state_init(&state);
	/*optionally customize the state*/
	if (!TCOD_sys_read_file(filename,&png,&pngsize)) return NULL;

	lodepng_inspect(&width,&height,&state, png, pngsize);
	bpp=lodepng_get_bpp(&state.info_png.color);

	if ( bpp == 24 ) {
		/* don't convert to 32 bits because libtcod's 24bits renderer is faster */
		state.info_raw.colortype=LCT_RGB;
	} else if (  bpp != 24 && bpp != 32 ) { 
		/* paletted png. convert to 24 bits */
		state.info_raw.colortype=LCT_RGB;
		state.info_raw.bitdepth=8;
		bpp=24;
	}
	error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
	free(png);
	if(error) {
		printf("error %u: %s\n", error, lodepng_error_text(error));
		lodepng_state_cleanup(&state);
		return NULL;
	}
		
	/* create the SDL surface */
	bitmap=TCOD_sys_get_surface(width,height,bpp==32);
	source=image;
	rowsize=width*bpp/8;
	for (y=0; y<  height; y++ ) {
		Uint8 *row_pointer=(Uint8 *)(bitmap->pixels) + y * bitmap->pitch;
		memcpy(row_pointer,source,rowsize);
		source+=rowsize;
	}

	lodepng_state_cleanup(&state);
	free(image);	
	return bitmap;
}
Beispiel #3
0
int main(int argc, char **argv) {
    unsigned error;
    unsigned char* image;
    unsigned width, height;
    unsigned char* png;
    size_t pngsize;
    LodePNGState state;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s input-8bit.png output-32bit.png\n\nVersion 0.2, © 2014 Kornel Lesiński <*****@*****.**>\nhttps://github.com/pornel/undither\n\n", argv[0]);
        return 1;
    }

    lodepng_state_init(&state);
    state.decoder.color_convert = 0;
    state.info_raw.colortype = LCT_PALETTE;
    state.info_raw.bitdepth = 8;
    error = lodepng_load_file(&png, &pngsize, argv[1]);
    if (!error) {
        error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
    }
    free(png);
    if (error) {
        fprintf(stderr, "error when loading '%s': %s\n", argv[1], lodepng_error_text(error));
        return error;
    }

    if (state.info_raw.bitdepth != 8) {
        fprintf(stderr, "Only 256-color images are supported\n");
        return 1;
    }

    const rgba *pal = (rgba *)state.info_raw.palette;
    if (!pal || state.info_raw.colortype != LCT_PALETTE) {
        fprintf(stderr, "No pal?\n");
        return 1;
    }
    rgba *out = malloc(width*height*4);

    undither(image, pal, width, height, out);

    lodepng_state_cleanup(&state);
    free(image);

    error = lodepng_encode32_file(argv[2], (unsigned char*)out, width, height);
    if (error) {
        fprintf(stderr, "error when saving '%s': %s\n", argv[2], lodepng_error_text(error));
        return error;
    }

    return 0;
}
Beispiel #4
0
/*
Example 3
Save a PNG file to disk using a State, normally needed for more advanced usage.
The image argument has width * height RGBA pixels or width * height * 4 bytes
*/
void encodeWithState(const char* filename, const unsigned char* image, unsigned width, unsigned height)
{
  unsigned error;
  unsigned char* png;
  size_t pngsize;
  LodePNGState state;

  lodepng_state_init(&state);
  /*optionally customize the state*/

  error = lodepng_encode(&png, &pngsize, image, width, height, &state);
  if(!error) lodepng_save_file(png, pngsize, filename);

  /*if there's an error, display it*/
  if(error) printf("error %u: %s\n", error, lodepng_error_text(error));

  lodepng_state_cleanup(&state);
  free(png);
}
/*
Example 3
Load PNG file from disk using a State, normally needed for more advanced usage.
*/
void decodeWithState(const char *filename)
{
    unsigned error;
    unsigned char *image;
    unsigned width, height;
    unsigned char *png;
    size_t pngsize;
    LodePNGState state;

    lodepng_state_init(&state);
    /*optionally customize the state*/

    lodepng_load_file(&png, &pngsize, filename);
    error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
    if (error) printf("error %u: %s\n", error, lodepng_error_text(error));

    free(png);

    /*use image here*/
    /*state contains extra information about the PNG such as text chunks, ...*/

    lodepng_state_cleanup(&state);
    free(image);
}
Beispiel #6
0
int main(void)
{
    unsigned error;
    unsigned char* image;
    unsigned width, height;
    unsigned char* png;
    size_t pngsize;
    LodePNGState state;
    char filename[] = "handmaze.png";  /* NAME OF INPUT IMAGE */

    lodepng_state_init(&state);
    /*optionally customize the state*/
    state.info_raw.colortype = LCT_GREY;

    lodepng_load_file(&png, &pngsize, filename);
    error = lodepng_decode(&image, &width, &height, &state, png, pngsize);
    if(error) printf("error %u: %s\n", error, lodepng_error_text(error));

    free(png);

    /*use image here*/
    //printf("Width=%d Height=%d\n", width, height);
    
    //FILE * fp;
    //fp = fopen("imgout.txt", "w");
    //fp = fopen("imgout.ram", "wb");
    //int i,x,y;
    //char * ramimg = (char*) calloc(2*width*height, sizeof(char));
    //for(i = 0; i < 2*width*height; i++){
        //fprintf(fp, "%d (%d,%d)=%d\n", i, i%width, i/width, image[i]);
    //    if(i%2 == 0)
    //        ramimg[i] = image[i/2];
    //    else
    //        ramimg[i] = 0;
    //}
    //fwrite(ramimg, sizeof(unsigned char), 2*width*height, fp);
    //free(ramimg);
    //fclose(fp);
    //for(y = 0; y < height; y++) {
    //    for(x = 0; x < width; x++) {
    //        fprintf(fp, "(%d,%d)=%d\n", x, y, image[coords(x,y,width,height)]);
    //    }
    //}
    
    unsigned char * gaussimg = (unsigned char*) calloc(width*height, sizeof(char));
    unsigned char * outimg = (unsigned char*) calloc(width*height, sizeof(char));
    gaussian_blur(width, height, image, gaussimg);
    sobel_filtering(width, height, gaussimg, outimg);
    
    int right_coord, startx, starty, index1=6, index2=5;
    int block_size =blocksize(width, height, image, &startx, &starty, 7, &right_coord, index1, index2);
    //printf("blocksize = %d\n", block_size);
    
    int solver;
    unsigned char path[144];
    int *size=(int *) malloc(sizeof(int));
    *size = 0;

    solver = dfs(width,height,image, startx, starty, startx, starty, block_size, path, size);
    
    //printf("size = %d\n", *size);
    int i1;
    for(i1 = *size; i1 >= 0; i1--)
    {
        printf("%c\n", path[i1]);
    }
    
    //int x, y;
    //for(x = 0; x < width; x++)
    //{
    //    for(y = 0; y < height; y++)
    //    {
    //        outimg[coords(x,y,width,height)] = image[coords(x,y,width,height)];
    //    }
    //}
    
    unsigned char* outpng;
    error = lodepng_encode(&outpng, &pngsize, outimg, width, height, &state);
    if(!error) lodepng_save_file(outpng, pngsize, "sobel.png");  /* NAME OF OUTPUT IMAGE */
    if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
    
    
    
    /* CLEANUP */
    lodepng_state_cleanup(&state);
    free(image);
    free(outimg);
    
    return 0;
}
Beispiel #7
0
m3d_Image *m3d_PngLoad(const char *filename) {
    unsigned char *image = NULL;
    unsigned int width = 0, height = 0;
    unsigned int error = 0;

    unsigned char* png;
    size_t pngsize;
    LodePNGState state;
    lodepng_state_init(&state);

    lodepng_load_file(&png, &pngsize, filename);

    error = lodepng_decode(&image, &width, &height, &state, png, pngsize);

    if(error) {
        printf("loading of image \"%s\" failed (%s)\n",
                filename, lodepng_error_text(error));
        free(png);
        return NULL;
    }

    m3d_ImageFormat image_format = M3D_IMAGE_RGBA;
    switch (state.info_png.color.colortype) {
        case LCT_GREY:
            image_format = M3D_IMAGE_GREYSCALE;
            break;

        case LCT_RGB:
            image_format = M3D_IMAGE_RGB;
            break;

        case LCT_PALETTE:
            image_format = M3D_IMAGE_INDEXED;
            break;

        case LCT_GREY_ALPHA:
            image_format = M3D_IMAGE_GREYSCALE_ALPHA;
            break;

        case LCT_RGBA:
            image_format = M3D_IMAGE_RGBA;
            break;

        default:
            printf("loading of .png image \"%s\" failed (unknow image type)\n",
                    filename);
            free(png);
            lodepng_state_cleanup(&state);
            free(image);
            return NULL;
            break;
    }

    m3d_Image *mimage = m3d_ImageInit(image_format, width, height);
    if (!mimage) {
        printf("loading of image \"%s\" failed (alloc. error)\n",
                    filename);
    }

    m3d_ImageCopyData(mimage, M3D_IMAGE_RGBA, image);

    if (image_format == M3D_IMAGE_INDEXED) {
        mimage->palette = state.info_png.color.palette;
        mimage->palette_size = state.info_png.color.palettesize;
    }

    free(image);
    free(png);
    lodepng_state_cleanup(&state);

    return mimage;
}