void DjVuSource::ReadAnnotations(GP<ByteStream> pInclStream,
		set<GUTF8String>& processed, GP<ByteStream> pAnnoStream)
{
	// Look for shared annotations
	GUTF8String strInclude;
	char buf[1024];
	int nLength;
	while ((nLength = pInclStream->read(buf, 1024)))
		strInclude += GUTF8String(buf, nLength);

	// Eat '\n' in the beginning and at the end
	while (strInclude.length() > 0 && strInclude[0] == '\n')
		strInclude = strInclude.substr(1, static_cast<unsigned int>(-1));

	while (strInclude.length() > 0 && strInclude[static_cast<int>(strInclude.length()) - 1] == '\n')
		strInclude.setat(strInclude.length() - 1, 0);

	if (strInclude.length() > 0 && processed.find(strInclude) == processed.end())
	{
		processed.insert(strInclude);

		GURL urlInclude = m_pDjVuDoc->id_to_url(strInclude);
		GP<DataPool> pool = m_pDjVuDoc->request_data(NULL, urlInclude);
		GP<ByteStream> stream = pool->get_stream();
		GP<IFFByteStream> iff(IFFByteStream::create(stream));

		// Check file format
		GUTF8String chkid;
		if (!iff->get_chunk(chkid) ||
			(chkid != "FORM:DJVI" && chkid != "FORM:DJVU" &&
			chkid != "FORM:PM44" && chkid != "FORM:BM44"))
		{
			return;
		}

		// Find chunk with page info
		while (iff->get_chunk(chkid) != 0)
		{
			GP<ByteStream> chunk_stream = iff->get_bytestream();

			if (chkid == "INCL")
			{
				ReadAnnotations(pInclStream, processed, pAnnoStream);
			}
			else if (chkid == "FORM:ANNO")
			{
				pAnnoStream->copy(*chunk_stream);
			}
			else if (chkid == "ANTa" || chkid == "ANTz")
			{
				const GP<IFFByteStream> iffout = IFFByteStream::create(pAnnoStream);
				iffout->put_chunk(chkid);
				iffout->copy(*chunk_stream);
				iffout->close_chunk();
			}

			iff->seek_close_chunk();
		}
	}
}
Beispiel #2
0
static void debug_parse(char *string, const char *error)
{
	if (*string && strchr("~@&", *string))
	{
		char *text = string + 1;
		const char *end;

		if (*text == '"')
		{
			end = parse_string(text, '\n');
			dc_output(1, text, -1);
		}
		else
		{
			dc_output(1, string, -1);
			end = NULL;
		}

		if (error)
			dc_error("%s, ignoring to EOLN", error);
		else if (!end)
			dc_error("\" expected");
		else if (g_str_has_prefix(string, "~^(Scope)#07"))
			on_inspect_signal(string + 12);
	}
 	else if (!strcmp(string, "(gdb) "))  /* gdb.info says "(gdb)" */
 	{
		dc_output(3, "(gdb) ", 6);
		wait_prompt = wait_result;
	}
	else
	{
		char *message;

		for (message = string; isdigit(*message); message++);

		if (error || option_library_messages || !g_str_has_prefix(message, "=library-"))
			dc_output_nl(1, string, -1);

		if (*message == '^')
		{
			iff (wait_result, "extra result")
				wait_result--;
		}

		if (*string == '0' && message > string + 1)
		{
			memmove(string, string + 1, message - string - 1);
			message[-1] = '\0';
		}
		else
			string = NULL;  /* no token */

		if (error)
			dc_error("%s, ignoring to EOLN", error);
		else
			parse_message(message, string);
	}
}
Beispiel #3
0
void INIConfig::init(const string filename)
{
  ifstream iff(filename.c_str());
  string s, last_s, tag;
  char buf[1024];
  while (iff >> s){
    if ('#' == s[0]){ iff.getline(buf, 1023); continue; }
    if (s[0] == '[' && s[s.length() -1 ] == ']'){ tag = s; }
    else if (s == "=") {iff >> s;  _config[tag][last_s] = s; }
    else
Beispiel #4
0
void on_debug_list_source(GArray *nodes)
{
	ParseLocation loc;

	parse_location(nodes, &loc);

	iff (loc.line, "no line or abs file")
		debug_send_format(N, "02-break-insert -t %s:%d\n05", loc.file, loc.line);

	parse_location_free(&loc);
}
Beispiel #5
0
void readsFeature(char* filename,double* e,int* p,int* index,double* threshold)
{
	int t_p,t_index;
	double t_threshold,t_e;
	ifstream iff(filename);
	for(int i = 0;i < 200;i++)
	{
		iff>>t_e>>t_threshold>>t_p>>t_index;
		*(e+i) = t_e;
		*(threshold+i) = t_threshold;
		*(p+i) = t_p;
		*(index+i) = t_index;
	}
}
Beispiel #6
0
/**
 *  Return true if the object looks to be in good shape.  Centralizes a number
 *  of consistency checks that would otherwise clutter up the code, and, since
 *  only ever called from within assertions, can be eliminated entirely by the
 *  compiler from the release build.
 */
bool LimitedArena::consistent() const
{
    assert(_parent      != 0);                           // Validate the parent
    assert(_limit       <= unlimited);                   // Now check that our
    assert(_available   <= _limit);                      //  various counters
    assert(_allocated   <= _peakusage);                  //  are within their
    assert(_peakusage   <= _limit);                      //  legal ranges...
    assert(_allocations <= unlimited);                   //
    assert(iff(_allocated==0,_allocations==0));          // Live=0 <=> Total=0

    if (_available < unlimited)                          // Enforcing a limit?
    {
        assert(_allocated + _available == _limit);       // ...check accounts
    }

    return true;                                         // Seems to be kosher
}
DataTable* DataTableManager::open(const std::string& table)
{
	FATAL(!m_installed, ("DataTableManager::open: not installed."));
	DataTable *retVal = getTable(table, false);
	if (retVal)
		return retVal;

	if (!TreeFile::exists(table.c_str()))
	{
		DEBUG_WARNING(true, ("Could not find treefile table for open [%s]", table.c_str()));
		return 0;
	}

	Iff iff(table.c_str(), false);
	retVal = new DataTable;
	retVal->load(iff);

	m_cachedTable = retVal;
	m_cachedTableName = table;
	m_tables[table] = retVal;

	return retVal;
}
Beispiel #8
0
void readFeature(char* filename,int* feature,int featureNum)
{
	ifstream iff(filename,ios::binary);
	if(!iff)
	{
		cout<<"error"<<endl;
		return;
	}
	int buff;


	int num = 0;
	while(iff.read((char*)&buff,sizeof(int)))
	{
		*(feature+num*featureNum) = buff;
		for(int i = 1;i < featureNum;i++)
		{
			iff.read((char*)&buff,sizeof(int));
			*(feature+num*featureNum+i) = buff;
		}
		num++;
	}

}
Beispiel #9
0
bool run(struct context *context,
         struct byte_array *program,
         struct map *env,
         bool in_context)
{
    null_check(context);
    null_check(program);
    program = byte_array_copy(program);
    program->current = program->data;
    struct program_state *state = NULL;
    enum Opcode inst = VM_NIL;
    if (context->runtime) {
        if (in_context) {
            if (!state)
                state = (struct program_state*)stack_peek(context->program_stack, 0);
            env = state->named_variables; // use the caller's variable set in the new state
        }
        else
            state = program_state_new(context, env);
    }

    while (program->current < program->data + program->length) {
        inst = (enum Opcode)*program->current;
        bool really = inst & VM_RLY;
        inst &= ~VM_RLY;
#ifdef DEBUG
        display_program_counter(context, program);
#endif
        program->current++; // increment past the instruction
        int32_t pc_offset = 0;

        switch (inst) {
            case VM_COM:
            case VM_ITR:    if (iterate(context, inst, state, program)) goto done;  break;
            case VM_RET:    if (ret(context, program))                  goto done;  break;
            case VM_TRO:    if (tro(context))                           goto done;  break;
            case VM_TRY:    if (vm_trycatch(context, program))          goto done;  break;
            case VM_EQU:
            case VM_MUL:
            case VM_DIV:
            case VM_ADD:
            case VM_SUB:
            case VM_NEQ:
            case VM_GTN:
            case VM_LTN:
            case VM_GRQ:
            case VM_LEQ:
            case VM_BND:
            case VM_BOR:
            case VM_MOD:
            case VM_XOR:
            case VM_INV:
            case VM_RSF:
            case VM_LSF:    binary_op(context, inst);                       break;
            case VM_ORR:
            case VM_AND:    pc_offset = boolean_op(context, program, inst); break;
            case VM_NEG:
            case VM_NOT:    unary_op(context, inst);                        break;
            case VM_SRC:    src(context, inst, program);                    break;
            case VM_DST:    dst(context, really);                           break;
            case VM_STX:
            case VM_SET:    set(context, inst, state, program);             break;
            case VM_JMP:    pc_offset = jump(context, program);             break;
            case VM_IFF:    pc_offset = iff(context, program);              break;
            case VM_CAL:    func_call(context, inst, program, NULL);        break;
            case VM_LST:    push_list(context, program);                    break;
            case VM_MAP:    push_map(context, program);                     break;
            case VM_NIL:    push_nil(context);                              break;
            case VM_INT:    push_int(context, program);                     break;
            case VM_FLT:    push_float(context, program);                   break;
            case VM_BUL:    push_bool(context, program);                    break;
            case VM_STR:    push_str(context, program);                     break;
            case VM_VAR:    push_var(context, program);                     break;
            case VM_FNC:    push_fnc(context, program);                     break;
            case VM_GET:    list_get(context, really);                      break;
            case VM_PTX:
            case VM_PUT:    list_put(context, inst, really);                break;
            case VM_MET:    method(context, program, really);               break;
            default:
                vm_exit_message(context, ERROR_OPCODE);
                return false;
        }
        program->current += pc_offset;
    }

    if (!context->runtime)
        return false;
done:
    if (!in_context)
        stack_pop(context->program_stack);
    return inst == VM_RET;
}
Beispiel #10
0
int main() {
     machines_state_t *root;
     machines_state_t *level_1;
     machines_state_t *level_2;
     machines_state_t *menu;
     machines_state_t *level_1_play;
     machines_state_t *level_1_quit;
     machines_state_t *level_2_play;
     machines_state_t *level_2_quit;

     machines_dot_t *dot = machines_new_dot("test_gameplay", stdlib_memory);
     char *dot_out = (char*)malloc(16384);

     int condition_level_1 = 0;
     int condition_level_2 = 0;
     int condition_quit    = 0;

     root         = machines_new_state("root", NULL, stdlib_memory);
     level_1      = machines_new_state("level 1", root, stdlib_memory);
     level_2      = machines_new_state("level 2", root, stdlib_memory);
     menu         = machines_new_state("menu", root, stdlib_memory);
     level_1_play = machines_new_state("play level 1", level_1, stdlib_memory);
     level_1_quit = machines_new_state("quit level 1", level_1, stdlib_memory);
     level_2_play = machines_new_state("play level 2", level_2, stdlib_memory);
     level_2_quit = machines_new_state("quit level 2", level_2, stdlib_memory);

     root        ->add_entry(root        , dot->on_entry(dot, "root        "));
     level_1     ->add_entry(level_1     , dot->on_entry(dot, "level_1     "));
     level_2     ->add_entry(level_2     , dot->on_entry(dot, "level_2     "));
     menu        ->add_entry(menu        , dot->on_entry(dot, "menu        "));
     level_1_play->add_entry(level_1_play, dot->on_entry(dot, "level_1_play"));
     level_1_quit->add_entry(level_1_quit, dot->on_entry(dot, "level_1_quit"));
     level_2_play->add_entry(level_2_play, dot->on_entry(dot, "level_2_play"));
     level_2_quit->add_entry(level_2_quit, dot->on_entry(dot, "level_2_quit"));

     root        ->add_exit(root        , dot->on_exit(dot, "root        "));
     level_1     ->add_exit(level_1     , dot->on_exit(dot, "level_1     "));
     level_2     ->add_exit(level_2     , dot->on_exit(dot, "level_2     "));
     menu        ->add_exit(menu        , dot->on_exit(dot, "menu        "));
     level_1_play->add_exit(level_1_play, dot->on_exit(dot, "level_1_play"));
     level_1_quit->add_exit(level_1_quit, dot->on_exit(dot, "level_1_quit"));
     level_2_play->add_exit(level_2_play, dot->on_exit(dot, "level_2_play"));
     level_2_quit->add_exit(level_2_quit, dot->on_exit(dot, "level_2_quit"));

     root->entry_at(root, menu);
     menu->add_transition(menu, level_1, dot->transition(dot, "menu->level_1", iff("menu->level_1", &condition_level_1)));
     menu->add_transition(menu, level_2, dot->transition(dot, "menu->level_2", iff("menu->level_2", &condition_level_2)));
     level_1->add_transition(level_1, menu, dot->transition(dot, "level_1->menu", always_true("level 1 finished, back to menu")));
     level_2->add_transition(level_2, menu, dot->transition(dot, "level_1->menu", always_true("level 2 finished, back to menu")));

     level_1->entry_at(level_1, level_1_play);
     level_1->exit_at(level_1, level_1_quit);
     level_1_play->add_transition(level_1_play, level_1_quit, dot->transition(dot, "level_1_play->level_1_quit", iff("play->quit", &condition_quit)));
     level_1_play->add_transition(level_1_play, level_1_play, dot->transition(dot, "level_1_play->level_1_play", always_true("continue playing level 1"))); // note that transitions are checked in order until first match

     level_2->entry_at(level_2, level_2_play);
     level_2->exit_at(level_2, level_2_quit);
     level_2_play->add_transition(level_2_play, level_2_quit, dot->transition(dot, "level_2_play->level_2_quit", iff("play->quit", &condition_quit)));
     level_2_play->add_transition(level_2_play, level_2_play, dot->transition(dot, "level_2_play->level_2_play", always_true("continue playing level 2"))); // note that transitions are checked in order until first match

     // application startup
     printf("**** trigger: go to menu\n");
     root->trigger(root);
     assert(root->current(root) == menu);

     // the menu is displayed; the user chooses the first level
     condition_level_1 = 1;
     printf("**** trigger: go to first level\n");
     root->trigger(root);
     assert(root->current(root) == level_1);
     assert(level_1->current(level_1) == level_1_play);

     // the user keeps playing
     printf("**** trigger: keep playing\n");
     root->trigger(root);
     assert(root->current(root) == level_1);
     assert(level_1->current(level_1) == level_1_play);

     // the user reaches the end of the level
     condition_quit = 1;
     printf("**** trigger: exit level\n");
     root->trigger(root);
     assert(root->current(root) == level_1);
     assert(level_1->current(level_1) == level_1_quit);

     // the user returns to the menu
     printf("**** trigger: back to the menu\n");
     root->trigger(root);
     assert(root->current(root) == menu);

     // (a bit of internal cleanup)
     condition_level_1 = 0;
     condition_quit = 0;

     // the menu is displayed; the user chooses the second level
     condition_level_2 = 1;
     printf("**** trigger: go to second level\n");
     root->trigger(root);
     assert(root->current(root) == level_2);
     assert(level_2->current(level_2) == level_2_play);

     // the user keeps playing
     printf("**** trigger: keep playing\n");
     root->trigger(root);
     assert(root->current(root) == level_2);
     assert(level_2->current(level_2) == level_2_play);

     // the user keeps playing
     printf("**** trigger: keep playing\n");
     root->trigger(root);
     assert(root->current(root) == level_2);
     assert(level_2->current(level_2) == level_2_play);

     // the user keeps playing
     printf("**** trigger: keep playing\n");
     root->trigger(root);
     assert(root->current(root) == level_2);
     assert(level_2->current(level_2) == level_2_play);

     // the user reaches the end of the level
     condition_quit = 1;
     printf("**** trigger: exit level\n");
     root->trigger(root);
     assert(root->current(root) == level_2);
     assert(level_2->current(level_2) == level_2_quit);

     // the user returns to the menu
     printf("**** trigger: back to the menu\n");
     root->trigger(root);
     assert(root->current(root) == menu);

     dot->generate(dot, dot_out, 16384);
     printf("--8<--------------------------------------------------------------------\n");
     printf(dot_out);
     printf("-------------------------------------------------------------------->8--\n");

     return 0;
}
Beispiel #11
0
void figureFeature(char* img,double scale,int length,int width)
{
	int windowLen = scale*(double)len;
	int windowWid = scale*(double)wid;
	int windowMove = scale*4.0;

	unsigned char* smallImg = new unsigned char[windowLen*windowWid];
	int* accimg = new int[windowWid*windowLen];

	int ff_x1[200],ff_y1[200],ff_x2[200],ff_y2[200],f_kind[200];
	ifstream iff("finalFeature.txt");
	for(int i = 0;i < 200;i++)
	{
		iff>>ff_x1[i]>>ff_y1[i]>>ff_x2[i]>>ff_y2[i]>>f_kind[i];
		ff_x1[i] = ff_x1[i]*scale;
		ff_y1[i] = ff_y1[i]*scale;
		ff_x2[i] = ff_x2[i]*scale;
		ff_y2[i] = ff_y2[i]*scale;
	}
	iff.close();

	int* f_p = new int[200];
	double* f_e = new double[200];
	double* f_threshold = new double[200];
	int* f_featureIndex = new int[200];
	readsFeature("feature.txt",f_e,f_p,f_featureIndex,f_threshold);

	int face = 0;
	for(int i = 0;i < length;i+=windowMove)
		for(int j = 0;j < width;j+=windowMove)
		{
			if(i+windowLen>length || j+windowWid>width)
				continue;
			for(int m = 0;m < windowLen;m++)
				for(int n = 0;n < windowWid;n++)
				{	
					char tmp = *(img+(i+m)*width+n+j);
					*(smallImg+m*windowWid+n) = (unsigned char)tmp;
				}

			Mat graph(Size(windowWid,windowLen), CV_8U, smallImg);
			cv::namedWindow("foobar1");
			cv::imshow("foobar1", graph);
			cv::waitKey(0);

			accumulativeGraph((unsigned char*)smallImg,accimg,windowLen,windowWid);
			
/*
			for(int i = 0;i< 10;i++)
			{
				for(int j = 0;j < 10;j++)
					cout << (int)*(smallImg+i*windowWid+j)<<" ";
				cout<<endl;
			}
			for(int i = 0;i< 10;i++)
			{
				for(int j = 0;j < 10;j++)
					cout << (int)*(accimg+i*windowWid+j)<<" ";
				cout<<endl;
			}*/

			int tfeature = 0;
			double sum1 = 0,sum2 = 0;
			for(int i = 0;i < 200;i++)
			{ 
				if(f_kind[i] == 1)
					tfeature = calRectFeature1(accimg,ff_x1[i],ff_y1[i],ff_x2[i],ff_y2[i],windowWid,windowLen);
				else if(f_kind[i] == 2)
					tfeature = calRectFeature2(accimg,ff_x1[i],ff_y1[i],ff_x2[i],ff_y2[i],windowWid,windowLen);
				else if(f_kind[i] == 3)
					tfeature = calRectFeature3(accimg,ff_x1[i],ff_y1[i],ff_x2[i],ff_y2[i],windowWid,windowLen);
				else if(f_kind[i] == 4)
					tfeature = calRectFeature4(accimg,ff_x1[i],ff_y1[i],ff_x2[i],ff_y2[i],windowWid,windowLen);

				tfeature = tfeature/scale;
				double beta = *(f_e+i)/(1.0-*(f_e+i));
				double at = log(1.0/beta);
				if((*(f_p+i)*tfeature)<*(f_p+i)*(*(f_threshold+i)))
					sum1 += at;
				sum2 += at;
			}
			if(sum1 > sum2/2)
			{
				cout<<"face"<<endl;
				face++;
			}
			else
				cout<<"not face"<<endl;
		}
}
Beispiel #12
0
int main(int argc, char* argv[]){

  std::string pose_offset_filename = "./poseoffset";
  unsigned cv_width  = 128;
  unsigned cv_height = 128;
  unsigned cv_depth  = 128;
  float    cv_min_d  = 0.5;
  float    cv_max_d  = 3.0;
  bool undistort = false;

  unsigned idwneighbours = 10;
  bool using_nni = false;

  CMDParser p("basefilename samplesfilename checkerboardviewinitfilename");

  p.addOpt("p",1,"poseoffetfilename", "specify the filename of the poseoffset on disk, default: " + pose_offset_filename);
  p.addOpt("s",3,"size", "use this calibration volume size (width x height x depth), default: 128 128 256");
  p.addOpt("d",2,"depthrange", "use this depth range: 0.5 4.5");
p.addOpt("u", -1, "undistort", "enable undistortion of images before chessboardsampling, default: false");

  p.addOpt("n",1,"numneighbours", "the number of neighbours that should be used for IDW inverse distance weighting, default: 10");

  p.addOpt("i",-1,"nni", "do use natural neighbor interpolation if possible, default: false");

  p.init(argc,argv);

  if(p.getArgs().size() != 3)
    p.showHelp();

  if(p.isOptSet("p")){
    pose_offset_filename = p.getOptsString("p")[0];
    std::cout << "setting poseoffetfilename to " << pose_offset_filename << std::endl;
  }


  if(p.isOptSet("s")){
    cv_width = p.getOptsInt("s")[0];
    cv_height = p.getOptsInt("s")[1];
    cv_depth = p.getOptsInt("s")[2];
  }

  if(p.isOptSet("d")){
    cv_min_d = p.getOptsInt("d")[0];
    cv_max_d = p.getOptsInt("d")[1];
  }
  if(p.isOptSet("u")){
    undistort = true;
}


  if(p.isOptSet("n")){
    idwneighbours = p.getOptsInt("n")[0];
    std::cout << "setting to numneighbours " << idwneighbours << std::endl;
  }


  if(p.isOptSet("i")){
    using_nni = true;
  }



  std::string basefilename = p.getArgs()[0];
  std::string filename_xyz(basefilename + "_xyz");
  std::string filename_uv(basefilename + "_uv");
  const std::string filename_yml(basefilename + "_yml");
  std::string filename_samples(p.getArgs()[1]);


  CalibVolume cv(cv_width, cv_height, cv_depth, cv_min_d, cv_max_d);

  RGBDConfig cfg;
  cfg.read(filename_yml.c_str());

  RGBDSensor sensor(cfg);

  Checkerboard cb;
  cb.load_pose_offset(pose_offset_filename.c_str());


  ChessboardSampling cbs(p.getArgs()[2].c_str(), cfg, undistort);
  cbs.init();

  glm::mat4 eye_d_to_world = sensor.guess_eye_d_to_world_static(cbs, cb);
  std::cerr << "extrinsic of sensor is: " << eye_d_to_world << std::endl;
  std::cerr << "PLEASE note, the extrinsic guess can be improved by averaging" << std::endl;

  for(unsigned z = 0; z < cv.depth; ++z){
    for(unsigned y = 0; y < cv.height; ++y){
      for(unsigned x = 0; x < cv.width; ++x){

	const unsigned cv_index = (z * cv.width * cv.height) + (y * cv.width) + x;

	const float depth = (z + 0.5) * (cv.max_d - cv.min_d)/cv.depth + cv.min_d;
	const float xd = (x + 0.5) * sensor.config.size_d.x * 1.0/cv.width;
	const float yd = (y + 0.5) * sensor.config.size_d.y * 1.0/cv.height;

	glm::vec3 pos3D_local = sensor.calc_pos_d(xd, yd, depth);
	glm::vec2 pos2D_rgb   = sensor.calc_pos_rgb(pos3D_local);
	pos2D_rgb.x /= sensor.config.size_rgb.x;
	pos2D_rgb.y /= sensor.config.size_rgb.y;

	glm::vec4 pos3D_world = eye_d_to_world * glm::vec4(pos3D_local.x, pos3D_local.y, pos3D_local.z, 1.0);

	xyz pos3D;
	pos3D.x = pos3D_world.x;
	pos3D.y = pos3D_world.y;
	pos3D.z = pos3D_world.z;
	cv.cv_xyz[cv_index] = pos3D;

	uv posUV;
	posUV.u = pos2D_rgb.x;
	posUV.v = pos2D_rgb.y;
	cv.cv_uv[cv_index] = posUV;
      }
    }
  }


  // load samples from filename
  std::vector<samplePoint> sps;

  std::ifstream iff(filename_samples.c_str(), std::ifstream::binary);
  const unsigned num_samples_in_file = calcNumFrames(iff,
						     sizeof(float) +
						     sizeof(uv) +
						     sizeof(uv) +
						     sizeof(xyz) +
						     sizeof(uv) +
						     sizeof(glm::vec3) +
						     sizeof(float));
  for(unsigned i = 0; i < num_samples_in_file; ++i){
    samplePoint s;
    iff.read((char*) &s.depth, sizeof(float));
    iff.read((char*) &s.tex_color, sizeof(uv));
    iff.read((char*) &s.tex_depth, sizeof(uv));
    iff.read((char*) &s.pos_offset, sizeof(xyz));
    iff.read((char*) &s.tex_offset, sizeof(uv));
    iff.read((char*) glm::value_ptr(s.pos_real), sizeof(glm::vec3));
    iff.read((char*) &s.quality, sizeof(float));
    sps.push_back(s);
  }
  iff.close();  


  // reapply correction offsets
  for(unsigned i = 0; i < sps.size(); ++i){
    const unsigned cv_width = cv.width;
    const unsigned cv_height = cv.height;
    const unsigned cv_depth = cv.depth;

    const float x = cv_width  *  ( sps[i].tex_depth.u) / cfg.size_d.x;
    const float y = cv_height *  ( sps[i].tex_depth.v)/ cfg.size_d.y;
    const float z = cv_depth  *  ( sps[i].depth - cv.min_d)/(cv.max_d - cv.min_d);

    xyz pos = getTrilinear(cv.cv_xyz, cv_width, cv_height, cv_depth, x , y , z );
    uv  tex = getTrilinear(cv.cv_uv,  cv_width, cv_height, cv_depth, x , y , z );


    sps[i].pos_offset.x = sps[i].pos_real[0] - pos.x;
    sps[i].pos_offset.y = sps[i].pos_real[1] - pos.y;
    sps[i].pos_offset.z = sps[i].pos_real[2] - pos.z;
      
    sps[i].tex_offset.u = sps[i].tex_color.u/cfg.size_rgb.x - tex.u;
    sps[i].tex_offset.v = sps[i].tex_color.v/cfg.size_rgb.y - tex.v;

    sps[i].quality = 1.0f;
      
  }


  
  Calibrator   c;
  c.using_nni = using_nni;
  c.applySamples(&cv, sps, cfg, idwneighbours, basefilename.c_str());
  cv.save(filename_xyz.c_str(), filename_uv.c_str());


  return 0;
}
PageInfo DjVuSource::ReadPageInfo(int nPage, bool bNeedText, bool bNeedAnno)
{
	ASSERT(nPage >= 0 && nPage < m_nPageCount);
	PageInfo pageInfo;
	pageInfo.szPage.cx = 100;
	pageInfo.szPage.cy = 100;
	pageInfo.nDPI = 100;
	pageInfo.bDecoded = true;

	GP<ByteStream> pAnnoStream;
	if (bNeedAnno)
		pAnnoStream = ByteStream::create();

	GP<ByteStream> pTextStream;
	if (bNeedText)
		pTextStream = ByteStream::create();

	try
	{
		// Get raw data from the document and decode only requested chunks
		// DjVuFile is not used to ensure that we do not wait for a lock
		// to be released and thus do not block the UI thread
		GURL url = m_pDjVuDoc->page_to_url(nPage);
		GP<DataPool> pool = m_pDjVuDoc->request_data(NULL, url);
		GP<ByteStream> stream = pool->get_stream();
		GP<IFFByteStream> iff(IFFByteStream::create(stream));

		// Check file format
		GUTF8String chkid;
		if (!iff->get_chunk(chkid) ||
			(chkid != "FORM:DJVI" && chkid != "FORM:DJVU" &&
			 chkid != "FORM:PM44" && chkid != "FORM:BM44"))
		{
			return pageInfo;
		}

		bool bHasIW44 = false;

		// Find chunk with page info
		while (iff->get_chunk(chkid) != 0)
		{
			GP<ByteStream> chunk_stream = iff->get_bytestream();

			if (chkid == "INFO")
			{
				// Get page dimensions and resolution from info chunk
				GP<DjVuInfo> pInfo = DjVuInfo::create();
				pInfo->decode(*chunk_stream);

				// Check data for consistency
				pageInfo.szPage.cx = max(pInfo->width, 0);
				pageInfo.szPage.cy = max(pInfo->height, 0);
				pageInfo.nInitialRotate = pInfo->orientation;
				pageInfo.nDPI = max(pInfo->dpi, 0);

				if ((pInfo->orientation & 1) != 0)
					swap(pageInfo.szPage.cx, pageInfo.szPage.cy);
			}
			else if (!bHasIW44 && (chkid == "PM44" || chkid == "BM44"))
			{
				bHasIW44 = true;

				// Get image dimensions and resolution from bitmap chunk
				UINT serial = chunk_stream->read8();
				UINT slices = chunk_stream->read8();
				UINT major = chunk_stream->read8();
				UINT minor = chunk_stream->read8();

				UINT xhi = chunk_stream->read8();
				UINT xlo = chunk_stream->read8();
				UINT yhi = chunk_stream->read8();
				UINT ylo = chunk_stream->read8();

				pageInfo.szPage.cx = (xhi << 8) | xlo;
				pageInfo.szPage.cy = (yhi << 8) | ylo;
				pageInfo.nDPI = 100;
			}
			else if (chkid == "TXTa" || chkid == "TXTz")
			{
				pageInfo.bHasText = true;

				if (bNeedText)
				{
					const GP<IFFByteStream> iffout = IFFByteStream::create(pTextStream);
					iffout->put_chunk(chkid);
					iffout->copy(*chunk_stream);
					iffout->close_chunk();
				}
			}
			else if (bNeedAnno && chkid == "FORM:ANNO")
			{
				pAnnoStream->copy(*chunk_stream);
			}
			else if (bNeedAnno && (chkid == "ANTa" || chkid == "ANTz"))
			{
				const GP<IFFByteStream> iffout = IFFByteStream::create(pAnnoStream);
				iffout->put_chunk(chkid);
				iffout->copy(*chunk_stream);
				iffout->close_chunk();
			}
			else if (bNeedAnno && chkid == "INCL")
			{
				set<GUTF8String> processed;
				ReadAnnotations(chunk_stream, processed, pAnnoStream);
			}

			iff->seek_close_chunk();
		}

		if (bNeedText && pTextStream->tell())
			pageInfo.DecodeText(pTextStream);

		if (bNeedAnno && pAnnoStream->tell())
			pageInfo.DecodeAnno(pAnnoStream);
	}
	catch (GException&)
	{
	}
	catch (...)
	{
		if (pApplication != NULL)
			pApplication->ReportFatalError();
	}

	return pageInfo;
}