Esempio n. 1
0
bool bitmap_resize(struct bitmap *img, HermesHandle hermes_h,
		HermesFormat *fmt, int nw, int nh)
{
	unsigned int state = 0;
	short bpp = bitmap_get_bpp( img );
	int stride = bitmap_get_rowstride( img );
	int err;

	if( img->resized != NULL ) {
		if( img->resized->width != nw || img->resized->height != nh ) {
			bitmap_destroy( img->resized );
			img->resized = NULL;
		} else {
			/* the bitmap is already resized */
			return(true);
		}
	}

	/* allocate the mem for resized bitmap */
	if (img->opaque == true) {
		state |= BITMAP_OPAQUE;
	}
	img->resized = bitmap_create_ex( nw, nh, bpp, nw*bpp, state, NULL );
	if( img->resized == NULL ) {
			printf("W: %d, H: %d, bpp: %d\n", nw, nh, bpp);
			assert(img->resized);
			return(false);
	}

	/* allocate an converter, only for resizing */
	err = Hermes_ConverterRequest( hermes_h,
			fmt,
			fmt
	);
	if( err == 0 ) {
		return(false);
	}

	err = Hermes_ConverterCopy( hermes_h,
		img->pixdata,
		0,			/* x src coord of top left in pixel coords */
		0,			/* y src coord of top left in pixel coords */
		bitmap_get_width( img ), bitmap_get_height( img ),
		stride, 	/* stride as bytes */
		img->resized->pixdata,
		0,			/* x dst coord of top left in pixel coords */
		0,			/* y dst coord of top left in pixel coords */
		nw, nh,
		bitmap_get_rowstride(img->resized) /* stride as bytes */
	);
	if( err == 0 ) {
		bitmap_destroy( img->resized );
		img->resized = NULL;
		return(false);
	}

	return(true);
}
Esempio n. 2
0
/**
 * Build a bitmap struct from the bitmap file
 *
 * @param   file  Bitmap file
 *
 * @return        Bitmap Struct
 */
struct bitmap bitmap_build_struct( FILE * file )
{
    struct bitmap bitmap;
    unsigned int buffer = 0;
    struct pixel pixel_buffer[99999];
    int pixel = 0;

    bitmap.width  = bitmap_get_width( file );
    bitmap.height = bitmap_get_height( file );
    bitmap.length = bitmap_get_length( file );
    bitmap.depth  = bitmap_bit_depth( file );

    fseek( file, bitmap_get_data_offset( file ), SEEK_SET );

    while ( fread( &buffer, bitmap.depth / 8, 1, file ) )
    {
        pixel_buffer[ pixel ].r = buffer >> 16 & 0xFF;
        pixel_buffer[ pixel ].g = buffer >> 8  & 0xFF;
        pixel_buffer[ pixel ].b = buffer       & 0xFF;
        pixel++;
    }

    /* flip y axis because the bmp format is usually reversed (but not always!) */
    pixel = 0;

    for ( int y = bitmap.height - 1; y > -1; y-- )
    {
        for ( int x = 0; x < bitmap.width; x++ )
        {
            bitmap.pixels[pixel++] = pixel_buffer[ y * bitmap.width + x ];
        }
    }
    /* ------------------------------------------------------------------------ */

    return bitmap;
}
Esempio n. 3
0
HPDF_Image pdf_extract_image(struct bitmap *bitmap)
{
	HPDF_Image image = NULL;
	hlcache_handle *content = NULL;

	/* TODO - get content from bitmap pointer */

	if (content) {
		const char *source_data;
		unsigned long source_size;

		/*Not sure if I don't have to check if downloading has been
		finished.
		Other way - lock pdf plotting while fetching a website
		*/
		source_data = content_get_source_data(content, &source_size);

		switch(content_get_type(content)){
		/*Handle "embeddable" types of images*/
		case CONTENT_JPEG:
 			image = HPDF_LoadJpegImageFromMem(pdf_doc,
 					(const HPDF_BYTE *) source_data,
 					source_size);
 			break;

		/*Disabled until HARU PNG support will be more stable.

		case CONTENT_PNG:
			image = HPDF_LoadPngImageFromMem(pdf_doc,
					(const HPDF_BYTE *)content->source_data,
					content->total_size);
			break;*/
		default:
			break;
		}
	}

	if (!image) {
		HPDF_Image smask;
		unsigned char *img_buffer, *rgb_buffer, *alpha_buffer;
		int img_width, img_height, img_rowstride;
		int i, j;

		/*Handle pixmaps*/
		img_buffer = bitmap_get_buffer(bitmap);
		img_width = bitmap_get_width(bitmap);
		img_height = bitmap_get_height(bitmap);
		img_rowstride = bitmap_get_rowstride(bitmap);

		rgb_buffer = (unsigned char *)malloc(3 * img_width * img_height);
		alpha_buffer = (unsigned char *)malloc(img_width * img_height);
		if (rgb_buffer == NULL || alpha_buffer == NULL) {
			NSLOG(netsurf, INFO,
			      "Not enough memory to create RGB buffer");
			free(rgb_buffer);
			free(alpha_buffer);
			return NULL;
		}

		for (i = 0; i < img_height; i++)
			for (j = 0; j < img_width; j++) {
				rgb_buffer[((i * img_width) + j) * 3] =
				  img_buffer[(i * img_rowstride) + (j * 4)];

				rgb_buffer[(((i * img_width) + j) * 3) + 1] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 1];

				rgb_buffer[(((i * img_width) + j) * 3) + 2] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 2];

				alpha_buffer[(i * img_width)+j] =
				  img_buffer[(i * img_rowstride) + (j * 4) + 3];
			}

		smask = HPDF_LoadRawImageFromMem(pdf_doc, alpha_buffer,
				img_width, img_height,
     				HPDF_CS_DEVICE_GRAY, 8);

		image = HPDF_LoadRawImageFromMem(pdf_doc, rgb_buffer,
				img_width, img_height,
     				HPDF_CS_DEVICE_RGB, 8);

		if (HPDF_Image_AddSMask(image, smask) != HPDF_OK)
			image = NULL;

		free(rgb_buffer);
		free(alpha_buffer);
	}

	return image;
}