예제 #1
0
bool
exr_mptr::get_frame(synfig::Surface &out_surface, const synfig::RendDesc &/*renddesc*/, Time, synfig::ProgressCallback *cb)
{
    try
    {

	Imf::RgbaInputFile in(identifier.filename.c_str());

    int w = in.dataWindow().max.x - in.dataWindow().min.x + 1;
    int h = in.dataWindow().max.y - in.dataWindow().min.y + 1;
    //int dx = in.dataWindow().min.x;
    //int dy = in.dataWindow().min.y;

	etl::surface<Imf::Rgba> in_surface;
	in_surface.set_wh(w,h);
	in.setFrameBuffer (reinterpret_cast<Imf::Rgba *>(in_surface[0]), 1, w);

	in.readPixels (in.dataWindow().min.y, in.dataWindow().max.y);

	int x;
	int y;
	out_surface.set_wh(w,h);
	for(y=0;y<out_surface.get_h();y++)
		for(x=0;x<out_surface.get_w();x++)
		{
			Color &color(out_surface[y][x]);
			Imf::Rgba &rgba(in_surface[y][x]);
			color.set_r(rgba.r);
			color.set_g(rgba.g);
			color.set_b(rgba.b);
			color.set_a(rgba.a);
		}
	}
	catch (const std::exception& e)
    {
		if(cb)cb->error(e.what());
		else synfig::error(e.what());
		return false;
    }

	return true;
}
예제 #2
0
bool
bmp_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time /*time*/, synfig::ProgressCallback *cb)
{
	FILE *file=fopen(filename.c_str(),"rb");
	if(!file)
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),filename.c_str()));
		return false;
	}

	synfig::BITMAPFILEHEADER fileheader;
	synfig::BITMAPINFOHEADER infoheader;
	char b_char=fgetc(file);
	char m_char=fgetc(file);

	if(b_char!='B' || m_char!='M')
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),filename.c_str()));
		return false;
	}

	if(fread(&fileheader.bfSize, 1, sizeof(synfig::BITMAPFILEHEADER)-4, file)!=sizeof(synfig::BITMAPFILEHEADER)-4)
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPFILEHEADER from %s"),filename.c_str()));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	if(fread(&infoheader, 1, sizeof(synfig::BITMAPINFOHEADER), file)!=sizeof(synfig::BITMAPINFOHEADER))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAPINFOHEADER from %s"),filename.c_str()));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	int offset=little_endian(fileheader.bfOffsetBits);

	if(offset!=sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2)
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPFILEHEADER in %s. (bfOffsetBits=%d, should be %d)"),filename.c_str(),offset,sizeof(synfig::BITMAPFILEHEADER)+sizeof(synfig::BITMAPINFOHEADER)-2));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	if(little_endian(infoheader.biSize)!=little_endian(40))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAPINFOHEADER in %s. (biSize=%d, should be 40)"),filename.c_str(),little_endian(infoheader.biSize)));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	int w,h,bit_count;

	w=little_endian(infoheader.biWidth);
	h=little_endian(infoheader.biHeight);
	bit_count=little_endian_short(infoheader.biBitCount);

	synfig::warning("w:%d\n",w);
	synfig::warning("h:%d\n",h);
	synfig::warning("bit_count:%d\n",bit_count);

	if(little_endian(infoheader.biCompression))
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
		else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
		return false;
	}

	if(bit_count!=24 && bit_count!=32)
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
		return false;
	}

	int x;
	int y;
	surface.set_wh(w,h);
	for(y=0;y<surface.get_h();y++)
		for(x=0;x<surface.get_w();x++)
		{
//			float b=(float)(unsigned char)fgetc(file)*(1.0/255.0);
//			float g=(float)(unsigned char)fgetc(file)*(1.0/255.0);
//			float r=(float)(unsigned char)fgetc(file)*(1.0/255.0);
			float b=gamma().b_U8_to_F32((unsigned char)fgetc(file));
			float g=gamma().g_U8_to_F32((unsigned char)fgetc(file));
			float r=gamma().r_U8_to_F32((unsigned char)fgetc(file));

			surface[h-y-1][x]=Color(
				r,
				g,
				b,
				1.0
			);
			if(bit_count==32)
				fgetc(file);
		}


	fclose(file);
	return true;
}
예제 #3
0
bool
mplayer_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &renddesc, synfig::Time time, synfig::ProgressCallback *callback)
{

#error This code has vulnerabilites: arbitrary shell command execution and tmpfile issues

	int ret;
	ret=system(
		strprintf("/usr/local/bin/mencoder \"%s\" -ovc rawrgb -ss %f -endpos 0 -nosound -o /tmp/tmp.synfig.rgbdata | grep \"VIDEO\" > /tmp/tmp.synfig.size",
			identifier.filename.c_str(),
			time
		).c_str()
	);
	/*
	if(ret!=0)
	{
		cerr<<"mencoder execution failed."<<endl;
		return false;
	}
*/
	FILE *sizefile=fopen("/tmp/tmp.synfig.size","rt");
	FILE *rgbfile=fopen("/tmp/tmp.synfig.rgbdata","rb");
	if(!rgbfile)
	{
		cerr<<"unable to open /tmp/tmp.synfig.rgbdata"<<endl;
		return false;
	}
	if(!sizefile)
	{
		cerr<<"unable to open /tmp/tmp.synfig.size"<<endl;
		return false;
	}

	int w=4,h=4,x,y;
	char bleh[500];

	fscanf(sizefile,"%499s %499s %dx%d",bleh,bleh,&w,&h);

	cerr<<strprintf("w:%d, h:%d, time:%f",w,h,time)<<endl;
	fseek(rgbfile,2047+3*8,SEEK_CUR);
	surface.set_wh(w,h);
	for(y=0;y<h;y++)
		for(x=0;x<w;x++)
		{
			unsigned char
				b=(unsigned char)fgetc(rgbfile),
				g=(unsigned char)fgetc(rgbfile),
				r=(unsigned char)fgetc(rgbfile);

			surface[h-y-1][x]=Color(
				(float)r/255.0,
				(float)g/255.0,
				(float)b/255.0,
				1.0
			);
		}

	fclose(rgbfile);
	fclose(sizefile);

	return true;
}
예제 #4
0
bool
bmp_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time /*time*/, synfig::ProgressCallback *cb)
{
	FileSystem::ReadStreamHandle stream = identifier.get_read_stream();
	if(!stream)
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),identifier.filename.c_str()));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unable to open %s"),identifier.filename.c_str()));
		return false;
	}

	synfig::BITMAP::FILEHEADER fileheader;
	synfig::BITMAP::INFOHEADER infoheader;

	if (!stream->read_variable(fileheader.bfType)
	 || fileheader.bfType[0] != 'B'
	 || fileheader.bfType[1] != 'M')
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),identifier.filename.c_str()));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("%s is not in BMP format"),identifier.filename.c_str()));
		return false;
	}

	if(!stream->read_whole_block(&fileheader.bfSize, sizeof(synfig::BITMAP::FILEHEADER)-2))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAP::FILEHEADER from %s"),identifier.filename.c_str()));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	if(!stream->read_whole_block(&infoheader, sizeof(synfig::BITMAP::INFOHEADER)))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Failure while reading BITMAP::INFOHEADER from %s"),identifier.filename.c_str()));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	int offset=little_endian(fileheader.bfOffsetBits);

	if(offset!=sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAP::FILEHEADER in %s. (bfOffsetBits=%d, should be %d)"),identifier.filename.c_str(),offset,sizeof(synfig::BITMAP::FILEHEADER)+sizeof(synfig::BITMAP::INFOHEADER)));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	if(little_endian(infoheader.biSize)!=sizeof(synfig::BITMAP::INFOHEADER))
	{
		String str("bmp_mptr::get_frame(): "+strprintf(_("Bad BITMAP::INFOHEADER in %s. (biSize=%d, should be %d)"),identifier.filename.c_str(),little_endian(infoheader.biSize),sizeof(synfig::BITMAP::INFOHEADER)));
		if(cb)cb->error(str);
		else synfig::error(str);
		return false;
	}

	int w,h,bit_count;

	w=little_endian(infoheader.biWidth);
	h=little_endian(infoheader.biHeight);
	bit_count=little_endian_short(infoheader.biBitCount);

	synfig::warning("w:%d\n",w);
	synfig::warning("h:%d\n",h);
	synfig::warning("bit_count:%d\n",bit_count);

	if(little_endian(infoheader.biCompression))
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
		else synfig::error("bmp_mptr::GetFrame(): "+string(_("Reading compressed bitmaps is not supported")));
		return false;
	}

	if(bit_count!=24 && bit_count!=32)
	{
		if(cb)cb->error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
		else synfig::error("bmp_mptr::GetFrame(): "+strprintf(_("Unsupported bit depth (bit_count=%d, should be 24 or 32)"),bit_count));
		return false;
	}

	int x;
	int y;
	surface.set_wh(w,h);
	for(y=0;y<surface.get_h();y++)
		for(x=0;x<surface.get_w();x++)
		{
//			float b=(float)(unsigned char)stream->getc()*(1.0/255.0);
//			float g=(float)(unsigned char)stream->getc()*(1.0/255.0);
//			float r=(float)(unsigned char)stream->getc()*(1.0/255.0);
			float b=gamma().b_U8_to_F32((unsigned char)stream->get());
			float g=gamma().g_U8_to_F32((unsigned char)stream->get());
			float r=gamma().r_U8_to_F32((unsigned char)stream->get());

			surface[h-y-1][x]=Color(
				r,
				g,
				b,
				1.0
			);
			if(bit_count==32)
				stream->get();
		}


	return true;
}
예제 #5
0
파일: mptr_png.cpp 프로젝트: synfig/synfig
bool
png_mptr::get_frame(synfig::Surface &surface, const synfig::RendDesc &/*renddesc*/, Time, synfig::ProgressCallback */*cb*/)
{
    /* Open the file pointer */
    FileSystem::ReadStream::Handle stream = identifier.get_read_stream();
    if (!stream)
    {
        //! \todo THROW SOMETHING
        throw strprintf("Unable to physically open %s",identifier.filename.c_str());
        return false;
    }

    /* Make sure we are dealing with a PNG format file */
    png_byte header[PNG_CHECK_BYTES];
    if (!stream->read_variable(header))
    {
        //! \todo THROW SOMETHING
        throw strprintf("Cannot read header from \"%s\"",identifier.filename.c_str());
        return false;
    }

    if (0 != png_sig_cmp(header, 0, PNG_CHECK_BYTES))
    {
        //! \todo THROW SOMETHING
        throw strprintf("This (\"%s\") doesn't appear to be a PNG file",identifier.filename.c_str());
        return false;
    }

    png_structp png_ptr = png_create_read_struct
                          (PNG_LIBPNG_VER_STRING, (png_voidp)this,
                           &png_mptr::png_out_error, &png_mptr::png_out_warning);
    if (!png_ptr)
    {
        //! \todo THROW SOMETHING
        throw String("error on importer construction, *WRITEME*3");
        return false;
    }

    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        png_destroy_read_struct(&png_ptr,
                                (png_infopp)NULL, (png_infopp)NULL);
        //! \todo THROW SOMETHING
        throw String("error on importer construction, *WRITEME*4");
        return false;
    }

    png_infop end_info = png_create_info_struct(png_ptr);
    if (!end_info)
    {
        png_destroy_read_struct(&png_ptr, &info_ptr,
                                (png_infopp)NULL);
        //! \todo THROW SOMETHING
        throw String("error on importer construction, *WRITEME*4");
        return false;
    }

    png_set_read_fn(png_ptr, stream.get(), read_callback);
    png_set_sig_bytes(png_ptr,PNG_CHECK_BYTES);

    png_read_info(png_ptr, info_ptr);

    int bit_depth,color_type,interlace_type, compression_type,filter_method;
    png_uint_32 width,height;

    png_get_IHDR(png_ptr, info_ptr, &width, &height,
                 &bit_depth, &color_type, &interlace_type,
                 &compression_type, &filter_method);

    if (bit_depth == 16)
        png_set_strip_16(png_ptr);

    if (bit_depth < 8)
        png_set_packing(png_ptr);

    double fgamma;
    if (png_get_gAMA(png_ptr, info_ptr, &fgamma))
    {
        synfig::info("PNG: Image gamma is %f",fgamma);
        png_set_gamma(png_ptr, gamma().get_gamma(), fgamma);
    }


    /*
    if (setjmp(png_jmpbuf(png_ptr)))
    {
    	synfig::error("Unable to setup longjump");
    	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    	fclose(file);
        //! \todo THROW SOMETHING
    	throw String("error on importer construction, *WRITEME*5");
    	return;
    }
    */

    // man libpng tells me:
    //   You must use png_transforms and not call any
    //   png_set_transform() functions when you use png_read_png().
    // but we used png_set_gamma(), which may be why we were seeing a crash at the end
    //   png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING|PNG_TRANSFORM_STRIP_16, NULL);

    png_read_update_info(png_ptr, info_ptr);
    png_uint_32 rowbytes = png_get_rowbytes(png_ptr, info_ptr);

    // allocate buffer to read image data into
    png_bytep *row_pointers=new png_bytep[height];
    png_byte *data = new png_byte[rowbytes*height];
    for (png_uint_32 i = 0; i < height; i++)
        row_pointers[i] = &(data[rowbytes*i]);

    png_read_image(png_ptr, row_pointers);

    png_uint_32 x, y;
    surface.set_wh(width,height);

    switch(color_type)
    {
    case PNG_COLOR_TYPE_RGB:
        for(y=0; y<height; y++)
            for(x=0; x<width; x++)
            {
                float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*3+0]);
                float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*3+1]);
                float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*3+2]);
                surface[y][x]=Color(
                                  r,
                                  g,
                                  b,
                                  1.0
                              );
                /*
                				surface[y][x]=Color(
                					(float)(unsigned char)row_pointers[y][x*3+0]*(1.0/255.0),
                					(float)(unsigned char)row_pointers[y][x*3+1]*(1.0/255.0),
                					(float)(unsigned char)row_pointers[y][x*3+2]*(1.0/255.0),
                					1.0
                				);
                */
            }
        break;

    case PNG_COLOR_TYPE_RGB_ALPHA:
        for(y=0; y<height; y++)
            for(x=0; x<width; x++)
            {
                float r=gamma().r_U8_to_F32((unsigned char)row_pointers[y][x*4+0]);
                float g=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*4+1]);
                float b=gamma().b_U8_to_F32((unsigned char)row_pointers[y][x*4+2]);
                surface[y][x]=Color(
                                  r,
                                  g,
                                  b,
                                  (float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
                              );
                /*
                surface[y][x]=Color(
                	(float)(unsigned char)row_pointers[y][x*4+0]*(1.0/255.0),
                	(float)(unsigned char)row_pointers[y][x*4+1]*(1.0/255.0),
                	(float)(unsigned char)row_pointers[y][x*4+2]*(1.0/255.0),
                	(float)(unsigned char)row_pointers[y][x*4+3]*(1.0/255.0)
                );
                */
            }
        break;

    case PNG_COLOR_TYPE_GRAY:
        for(y=0; y<height; y++)
            for(x=0; x<width; x++)
            {
                float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x]);
                //float gray=(float)(unsigned char)row_pointers[y][x]*(1.0/255.0);
                surface[y][x]=Color(
                                  gray,
                                  gray,
                                  gray,
                                  1.0
                              );
            }
        break;

    case PNG_COLOR_TYPE_GRAY_ALPHA:
        for(y=0; y<height; y++)
            for(x=0; x<width; x++)
            {
                float gray=gamma().g_U8_to_F32((unsigned char)row_pointers[y][x*2]);
//				float gray=(float)(unsigned char)row_pointers[y][x*2]*(1.0/255.0);
                surface[y][x]=Color(
                                  gray,
                                  gray,
                                  gray,
                                  (float)(unsigned char)row_pointers[y][x*2+1]*(1.0/255.0)
                              );
            }
        break;

    case PNG_COLOR_TYPE_PALETTE:
    {
        png_colorp palette;
        int num_palette;
        png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
        png_bytep trans_alpha = NULL;
        int num_trans = 0;
        bool has_alpha = (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans,
                                       NULL) & PNG_INFO_tRNS);
        for(y=0; y<height; y++)
            for(x=0; x<width; x++)
            {
                float r=gamma().r_U8_to_F32((unsigned char)palette[row_pointers[y][x]].red);
                float g=gamma().g_U8_to_F32((unsigned char)palette[row_pointers[y][x]].green);
                float b=gamma().b_U8_to_F32((unsigned char)palette[row_pointers[y][x]].blue);
                float a=1.0;

                if (has_alpha && num_trans > 0 && trans_alpha != NULL)
                {
                    a = row_pointers[y][x] < num_trans ?
                        (trans_alpha[row_pointers[y][x]]*(1.0/255.0)) : 1.0;
                }

                surface[y][x]=Color(
                                  r,
                                  g,
                                  b,
                                  a
                              );
            }
        break;
    }
    default:
        png_read_end(png_ptr, end_info);
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        synfig::error("png_mptr: error: Unsupported color type");
        //! \todo THROW SOMETHING
        throw String("error on importer construction, *WRITEME*6");
        return false;
    }

    png_read_end(png_ptr, end_info);
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);

    return true;
}