Ejemplo n.º 1
0
/// LATER: this is NOT tested.
static int ImageRead(YARPGenericImage& img, const char *filename)
{
	int width, height, color, num, size;

	FILE  *fp = ACE_OS::fopen(filename, "rb");

	if (!fp)    //die("cannot open file for reading");
	{
		warn("cannot open file for reading");
		return -1;
	}

	if (ReadHeader(fp, &height, &width, &color) < 0)
	{
		ACE_OS::fclose (fp);
		return -1;
	}

	if (!color)
		// img.GetID()==YARP_PIXEL_RGB || img.GetID() == YARP_PIXEL_MONO)
	{
		// img.SetID(color?YARP_PIXEL_RGB:YARP_PIXEL_MONO);
		img.SetID(YARP_PIXEL_MONO);
		img.Resize(width,height);
		///ACE_ASSERT(img.GetPadding() == 0);
		ACE_ASSERT(img.GetRawBuffer()!=NULL);

		const int w = img.GetWidth() * img.GetPixelSize();
		const int h = img.GetHeight();
		const int pad = img.GetPadding() + w;
		char *dst = img.GetRawBuffer ();
		size = w * h;

		num = 0;
		for (int i = 0; i < h; i++)
		{
			num += ACE_OS::fread((void *) dst, 1, (size_t) w, fp);
			dst += pad;
		}
	}
	else if (img.GetID()==YARP_PIXEL_RGB)
	{		
		img.SetID(YARP_PIXEL_RGB);
		img.Resize(width,height);
		///ACE_ASSERT(img.GetPadding() == 0);
		ACE_ASSERT(img.GetRawBuffer()!=NULL);

		const int w = img.GetWidth() * img.GetPixelSize();
		const int h = img.GetHeight();
		const int pad = img.GetPadding() + w;
		char *dst = img.GetRawBuffer ();
		size = w * h;

		num = 0;
		for (int i = 0; i < h; i++)
		{
			num += ACE_OS::fread((void *) dst, 1, (size_t) w, fp);
			dst += pad;
		}

	}
	else
	{
		// image is color, nothing was specified, assume BGR
		img.SetID(YARP_PIXEL_BGR);
		img.Resize(width,height);
		///ACE_ASSERT(img.GetPadding() == 0);
		ACE_ASSERT(img.GetRawBuffer()!=NULL);

		const int w = img.GetWidth() * img.GetPixelSize();
		const int h = img.GetHeight();
		const int pad = img.GetPadding() + w;
		size = w * h;

		YARPImageOf<YarpPixelRGB> img2;
		img2.Resize (width,height);
		char *dst = img2.GetRawBuffer ();

		num = 0;
		for (int i = 0; i < h; i++)
		{
			num += ACE_OS::fread((void *) dst, 1, (size_t) w, fp);
			dst += pad;
		}

		img.CastCopy(img2);
	}

	if (num != size) 
	{
		ACE_OS::printf ( "%d versus %d\n", num, size );
		//die("cannot read image data from file");
		warn("cannot read image data from file");
		ACE_OS::fclose (fp);
		return -1;
	}

	ACE_OS::fclose(fp);

	return 0;
}