Exemplo n.º 1
0
// if the image is in a directly supported format return the raw data otherwise decode & return the bitmap
bool MCImageImport(IO_handle p_stream, IO_handle p_mask_stream, MCPoint &r_hotspot, MCStringRef&r_name, MCImageCompressedBitmap *&r_compressed, MCImageBitmap *&r_bitmap)
{
	bool t_success;
	t_success = true;

	// IM-2014-07-31: [[ ImageLoader ]] Update to use MCImageLoader class
	MCImageLoaderFormat t_format;

	if (t_success)
		t_success = MCImageLoader::IdentifyFormat(p_stream, t_format);

	if (t_success)
	{
		uint32_t t_compression;
		if (MCImageLoaderFormatToCompression(t_format, t_compression))
		{
			t_success = MCImageCreateCompressedBitmap(t_compression, r_compressed);
			if (t_success)
			{
				uint32_t t_width, t_height;
				t_width = t_height = 0;
				
				if (t_success && t_compression == F_PICT)
					t_success = MCImageGetMetafileGeometry(p_stream, t_width, t_height);
				
				if (t_success)
					t_success = read_all(p_stream, r_compressed->data, r_compressed->size);

				r_compressed->width = t_width;
				r_compressed->height = t_height;
			}
		}
		else
		{
			MCImageLoader *t_loader;
			t_loader = nil;
			
			t_success = MCImageLoader::LoaderForStreamWithFormat(p_stream, t_format, t_loader);
			
			uint32_t t_xhot, t_yhot;
			
			MCAutoStringRef t_name;
			
			MCBitmapFrame *t_frames;
			t_frames = nil;
			
			uint32_t t_count;
            t_count = 0;
			
			if (t_success)
				t_success = t_loader->GetHotSpot(t_xhot, t_yhot);
			
			if (t_success)
				t_success = t_loader->GetName(&t_name);
			
			if (t_success)
				t_success = t_loader->TakeFrames(t_frames, t_count);
            
            if (t_success && p_mask_stream != nil && t_loader->GetFormat() == kMCImageFormatNetPBM)
				{
					MCImageBitmap *t_mask = nil;
					t_success = MCImageDecodeNetPBM(p_mask_stream, t_mask) &&
				MCImageBitmapApplyMask(t_frames[0].image, t_mask);
					MCImageFreeBitmap(t_mask);
				}

			if (t_success)
			{
				r_hotspot.x = t_xhot;
				r_hotspot.y = t_yhot;
                r_name = MCValueRetain(*t_name);
				r_bitmap = t_frames[0].image;
				t_frames[0].image = nil;
            }
			else
                r_name = MCValueRetain(kMCEmptyString);
			
			MCImageFreeFrames(t_frames, t_count);
			
			if (t_loader != nil)
				delete t_loader;
		}
	}

	return t_success;
}
Exemplo n.º 2
0
// if the image is in a directly supported format return the raw data otherwise decode & return the bitmap
bool MCImageImport(IO_handle p_stream, IO_handle p_mask_stream, MCPoint &r_hotspot, char *&r_name, MCImageCompressedBitmap *&r_compressed, MCImageBitmap *&r_bitmap)
{
	bool t_success = true;

	uindex_t t_width = 0, t_height = 0;

	uint8_t t_head[8];
	uindex_t t_size = 8;

	uint32_t t_compression = F_RLE;

	if (t_success)
		t_success = MCS_read(t_head, sizeof(uint8_t), t_size, p_stream) == IO_NORMAL &&
		t_size == 8 && MCS_seek_cur(p_stream, -8) == IO_NORMAL;

	if (t_success)
	{
		if (memcmp(t_head, "GIF87a", 6) == 0)
			t_compression = F_GIF;
		else if (memcmp(t_head, "GIF89a", 6) == 0)
			t_compression = F_GIF;
		else if (memcmp(t_head, "\211PNG", 4) == 0)
			t_compression = F_PNG;
		else if (memcmp(t_head, "\xff\xd8", 2) == 0)
			t_compression = F_JPEG;
		else if (MCImageGetMetafileGeometry(p_stream, t_width, t_height))
			t_compression = F_PICT;

		if (t_compression != F_RLE)
		{
			t_success = MCImageCreateCompressedBitmap(t_compression, r_compressed);
			if (t_success)
			{
				if (t_success)
					t_success = read_all(p_stream, r_compressed->data, r_compressed->size);

				r_compressed->width = t_width;
				r_compressed->height = t_height;
			}
		}
		else
		{
			MCImageBitmap *t_bitmap = nil;
			
			if (memcmp(t_head, "BM", 2) == 0)
				t_success = MCImageDecodeBMP(p_stream, r_hotspot, t_bitmap);
			else if (memcmp(t_head, "#define", 7) == 0)
				t_success = MCImageDecodeXBM(p_stream, r_hotspot, r_name, t_bitmap);
			else if (memcmp(t_head, "/* XPM", 6) == 0)
				t_success = MCImageDecodeXPM(p_stream, t_bitmap);
			else if (t_head[0] == 'P' && (t_head[1] >= '1' && t_head[1] <= '6'))
			{
				t_success = MCImageDecodeNetPBM(p_stream, t_bitmap);
				// may have a mask image
				if (t_success && p_mask_stream != nil)
				{
					MCImageBitmap *t_mask = nil;
					t_success = MCImageDecodeNetPBM(p_mask_stream, t_mask) &&
						MCImageBitmapApplyMask(t_bitmap, t_mask);
					MCImageFreeBitmap(t_mask);
				}
			}
			else // if all else fails, assume it's an XWD
				t_success = MCImageDecodeXWD(p_stream, r_name, t_bitmap);

			if (t_success)
				r_bitmap = t_bitmap;
			else
				MCImageFreeBitmap(t_bitmap);
		}
	}

	return t_success;
}