Exemplo n.º 1
0
int decode_jpeg(char *buffer, int pattern, int cnt) {
	size_t bufsize;
	shjpeg_context_t *context;
	int format, pitch;
	char *jpeg_buf;
	void *encode_buffer;
	void *decode_result;
	struct jpeg_buffer jbuf;
	int i;

	if ((context = shjpeg_init(0)) == NULL) {
		fprintf(stderr, "shjpeg_init() failed\n");
		return 1;
	}

	context->width = IMG_WIDTH;
	context->height = IMG_HEIGHT;
	format = SHJPEG_PF_RGB16;
	pitch  = (SHJPEG_PF_PITCH_MULTIPLY(format) * context->width + 7) & ~7;

	encode_buffer = shjpeg_malloc(context, format, context->width,
				      context->height, pitch, &bufsize);
	decode_result = shjpeg_malloc(context, format, context->width,
				      context->height, pitch, &bufsize);

	if (!encode_buffer || !decode_result) {
		fprintf(stderr, "Out of UIO memory\n");
		return 1;
	}

	jpeg_buf = malloc(bufsize);

	jbuf.ptr = jpeg_buf;
	/* set callbacks to context */
	context->sops = &my_sops;
	context->priv_data = (void*)&jbuf;
	context->libjpeg_disabled = 0;

	memset(encode_buffer, 0, bufsize);

	for (i = 0; i < cnt || cnt < 0; i++) {
		/* start of frame */
			fill_pattern(encode_buffer, context->width,
				context->height, i, pattern);
		if (shjpeg_encode(context, format, encode_buffer,
			      context->width, context->height, pitch) < 0) {
			fprintf(stderr, "shjpeg_encode() failed.\n");
			return 1;
		}

		/* init decoding (read jpeg header) */
		if (shjpeg_decode_init(context) < 0) {
			fprintf(stderr, "shjpeg_decode_init() failed\n");
			return 1;
		}

		/* start decoding */
		if (shjpeg_decode_run(context, format, decode_result,
				context->width, context->height, pitch) < 0) {
			fprintf(stderr, "shjpeg_deocde_run() failed\n");
		}
		/* shutdown decoder */
		shjpeg_decode_shutdown(context);
		/* get framebuffer information */
		write_framebuffer(buffer, decode_result, pitch, context->width,
			context->height);
		/* end of frame */
	}
	shjpeg_shutdown(context);
	free(jpeg_buf);
	shjpeg_free(context, encode_buffer, bufsize);
	shjpeg_free(context, decode_result, bufsize);
	return 0;
}
Exemplo n.º 2
0
void rasterize(const std::vector<tinyobj::shape_t>& shapes,const std::vector<tinyobj::material_t>& materials,uint32_t width,uint32_t height,Eigen::Matrix4f camera_matrix,bool drawdepth)
{
	//camera_matrix=Eigen::Matrix4f::Identity();
	uraster::Framebuffer<BunnyPixel> tp(width,height);
	
	/*BunnyVert vertsin[3]={	{-1.0f,-1.0f,0.0f,	1.0f,0.0f,0.0f,	0.0f,0.0f},
				{0.0f,0.0f,0.0f,	0.0f,1.0f,0.0f,	0.0f,1.0f},
				{1.0f,-1.0f,0.0f,	0.0f,0.0f,1.0f,	1.0f,0.0f}};
	size_t indexin[3]={0,1,2};
	
	
	uraster::draw(tp,
		      vertsin,vertsin+3,
		      indexin,indexin+3,
		      (BunnyVertVsOut*)NULL,(BunnyVertVsOut*)NULL,
		      std::bind(example_vertex_shader,std::placeholders::_1,camera_matrix),
		      example_fragment_shader
	);*/
	Eigen::Vector3f mm=Eigen::Vector3f(100000000.0,100000000.0,100000000.0);
	Eigen::Vector3f mx=-Eigen::Vector3f(100000000.0,100000000.0,100000000.0);
	
	
	std::vector<TinyImage> images;
	for(const tinyobj::material_t& mat : materials)
	{
		std::cerr << "Loading " << mat.diffuse_texname << std::endl;
		images.emplace_back(mat.diffuse_texname);
	}
	
	for(const tinyobj::shape_t& shs : shapes)
	{
		std::vector<BunnyVert> vertsin(shs.mesh.positions.size()/3);
		for(size_t i=0;i<vertsin.size();i++)
		{
			BunnyVert& vert=vertsin[i];
			vert.x=shs.mesh.positions[3*i];
			vert.y=shs.mesh.positions[3*i+1];
			vert.z=shs.mesh.positions[3*i+2];
			
			Eigen::Vector4f tproj=camera_matrix*Eigen::Vector4f(vert.x,vert.y,vert.z,1.0);
			//std::cerr << tproj.z() << ",";
			
			mm.x()=std::min(tproj.x(),mm.x());mm.y()=std::min(tproj.y(),mm.y());mm.z()=std::min(tproj.z(),mm.z());
			mx.x()=std::max(tproj.x(),mx.x());mx.y()=std::max(tproj.y(),mx.y());mx.z()=std::max(tproj.z(),mx.z());
			
			vert.nx=-shs.mesh.normals[3*i];
			vert.ny=-shs.mesh.normals[3*i+1];
			vert.nz=-shs.mesh.normals[3*i+2];
			
			vert.s=shs.mesh.texcoords[2*i];
			vert.t=shs.mesh.texcoords[2*i+1];
		}
		std::vector<size_t> indices(shs.mesh.indices.cbegin(),shs.mesh.indices.cend());
		
		uraster::draw(tp,
			&vertsin[0],&vertsin[0]+vertsin.size(),
			&indices[0],&indices[0]+indices.size(),
			(BunnyVertVsOut*)NULL,(BunnyVertVsOut*)NULL,
			std::bind(example_vertex_shader,std::placeholders::_1,camera_matrix),
			std::bind(example_fragment_shader,std::placeholders::_1,images[shs.mesh.material_ids[0]])
		);
	}
	
	std::cerr << "mn:" << mm.z() << ",\n mx: " << mx.z() << std::endl;
	
 	//std::cerr << "mn':" << camera_matrix*Eigen::Vector4f(mm.x(),mm.y(),mm.z(),1.0) << ",\n mx': " << camera_matrix*Eigen::Vector4f(mx.x(),mx.y(),mx.z(),1.0) << std::endl;
	std::cerr << "Rendering complete.  Postprocessing." << std::endl;
	write_framebuffer(tp,[](const BunnyPixel& bp){ return bp.diffuse_color;},"out_diffuse.png");
	write_framebuffer(tp,[](const BunnyPixel& bp){ std::array<float,1> t; t[0]=bp.new_depth; return t;},"out_height.png");
	write_framebuffer(tp,[](const BunnyPixel& bp){ return bp.normal;},"out_normals.png");
	
}