Ejemplo n.º 1
0
int PDFwin::handle(int event)
{
	static bool mbMoveRect=false;
	static TRect mPushedRect;
	static Int2D mPushedCursor;
	switch(event)
	{
/* Receiving Drag and Drop */
    case FL_DND_ENTER:
    case FL_DND_RELEASE:
    case FL_DND_LEAVE:
    case FL_DND_DRAG:
        return 1;

	case FL_PASTE:
		{
			TString fn=Fl::event_text();

			if(fn.length() && fn.right(4).toUpper()==".PDF")
			{
				load(fn);
			}
			else
				Msg::msgBox("Error! Unknown file format");
			// If there is a callback registered, call it.
            // The callback must access Fl::event_text() to
            // get the string or file path(s) that was dropped.
            // Note that do_callback() is not called directly.
            // Instead it will be executed by the FLTK main-loop
            // once we have finished handling the DND event.
            // This allows caller to popup a window or change widget focus.
            //if(callback() && ((when() & FL_WHEN_RELEASE) || (when() & FL_WHEN_CHANGED)))
              //  Fl::add_timeout(0.0, Fl_DND_Box::callback_deferred, (void*)this);
            return 1;
		}
	case FL_KEYUP:
		if(mModel)
		{
			if(Fl::event_key()==FL_Page_Down)
			{
				mCurrPage=(mCurrPage+1)%mModel->_pdfDoc->getNumPages();
				pageChanged();
				redraw();
			}
			else if(Fl::event_key()==FL_Page_Up)
			{
				mCurrPage=(mCurrPage+mModel->_pdfDoc->getNumPages()-1)%mModel->_pdfDoc->getNumPages();
				pageChanged();
				redraw();
			}
			else if(Fl::event_key()==FL_Delete)
			{
				if(mSelectedRect!=mRects.end())
				{
					mRects.erase(mSelectedRect);
					mSelectedRect=mRects.end();
					redraw();
				}
			}
		}
		break;
	case FL_ENTER:
		return 1;
	case FL_LEAVE:
		cursor(FL_CURSOR_DEFAULT);
		return 1;
	case FL_MOVE:
		{
			if(mSelectedRect!=mRects.end() &&
				mSelectedRect->corner(Int2D(Fl::event_x(), Fl::event_y())))
			{
				cursor(FL_CURSOR_NWSE);
				return 1;
			}
			if(findRect(Fl::event_x(), Fl::event_y())==mRects.end())
				cursor(FL_CURSOR_DEFAULT);
			else
				cursor(FL_CURSOR_MOVE);
		}
		return 1;
	case FL_PUSH:
		if(mModel && Fl::event_button() == FL_LEFT_MOUSE)
		{
			if(mSelectedRect!=mRects.end())
			{
				int corner=mSelectedRect->corner(Int2D(Fl::event_x(), Fl::event_y()));
				if(corner)
				{
					if(corner==1)
						std::swap(mSelectedRect->p1, mSelectedRect->p2);
					cursor(FL_CURSOR_NWSE);
					return 1;
				}
			}
			
			std::list<SelectionRectangle>::iterator i
				=findRect(Fl::event_x(), Fl::event_y());

			if(i!=mRects.end())
			{
				// move selected rect
				mSelectedRect=i;
				redraw();
				mbMoveRect=true;
				mPushedCursor.x=Fl::event_x();
				mPushedCursor.y=Fl::event_y();
				mPushedRect=*mSelectedRect;
				cursor(FL_CURSOR_MOVE);

				return 1;
			}

			

			mRects.push_back(SelectionRectangle());
			mSelectedRect=mRects.end();
			mSelectedRect--;
			if(selectedRect().handle(*this, "push"))
			{
				redraw();
				cursor(FL_CURSOR_HAND);
				return 1;
			}
		}
		return 0;
		break;
	case FL_DRAG:
		{
			if(mbMoveRect)
			{
				cursor(FL_CURSOR_MOVE);

				mSelectedRect->p1=
					toDocCoord(mPushedRect.left+Fl::event_x()-mPushedCursor.x,
							mPushedRect.top+Fl::event_y()-mPushedCursor.y);
				mSelectedRect->p2=
					toDocCoord(mPushedRect.right+Fl::event_x()-mPushedCursor.x,
							mPushedRect.bottom+Fl::event_y()-mPushedCursor.y);

				mSelectedRect->updateScreenCoord(*this);
				redraw();
				return 1;
			}

			if(selectedRect().handle(*this, "drag"))
				redraw();
			cursor(FL_CURSOR_HAND);

			return 1;
		}
		break;
	case FL_RELEASE:
		{
			mbMoveRect=false;
			
			if(mRects.size())
			{
				if(selectedRect().isValid())
				{
					if(selectedRect().p1.x> selectedRect().p2.x)
						std::swap(selectedRect().p1.x, selectedRect().p2.x);
				
					if(selectedRect().p1.y> selectedRect().p2.y)
						std::swap(selectedRect().p1.y, selectedRect().p2.y);
				}
				else
				{
					mRects.erase(mSelectedRect);
					mSelectedRect=mRects.end();
					redraw();
				}
				
			}
			return 1;
		}
		break;
	}
	return Fl_Double_Window::handle(event);
}
Ejemplo n.º 2
0
void PDFwin::pageChanged()
{
	int ww=w()-2;
	int hh=h()-2;

	if(!mModel) return;

	if(!mModel->cacheValid(ww, hh, mCurrPage))
	{
		mState="load";
		redraw();
		Fl::check();
		mState="none";
	}

	CImage* bmp_orig=mModel->getPage(ww, hh, mCurrPage);
	
	
	if(mLayout->findCheckButton("Use automatic segmentation")->value())
	{
		CImage temp;
		temp.CopyFrom(*bmp_orig);
#ifdef DEBUG_FONT_DETECTION
		CImage* bmp=bmp_orig;
#else
		CImage* bmp=&temp;
#endif	
		{

			intmatrixn& textCache=*mModel->_textCache[mCurrPage];


#ifdef USE_FONT_DETECTION
			const bool drawLetterBoundingBox=true;
			const bool drawDetectedSpace=true;
#else
			const bool drawLetterBoundingBox=false;
			const bool drawDetectedSpace=false;
#endif
			if(drawLetterBoundingBox)
			{
				CImagePixel ip(bmp);
				for(int i=0; i<textCache.rows()-1; i++)
				{
					int* info=textCache[i];
					int c=info[TC_c];
					int x=info[TC_x];
					int y=info[TC_y];
					int w=info[TC_w];
					int h=info[TC_h];
					int fx=info[TC_fx];
					int fy=info[TC_fy];
					if (std::abs(y-textCache[i+1][TC_y])<=3) // draw excluding trailing space
						{

#ifdef DEBUG_FONT_DETECTION
					ip.DrawLineBox(TRect(x-fx,y-fy, x-fx+w, y-fy+h), CPixelRGB8(0,0,0));				
#else
					ip.DrawBox(TRect(x-fx,y-fy, x-fx+w, y-fy+h), CPixelRGB8(0,0,0));
#endif
					ip.DrawHorizLine(x, y, w, CPixelRGB8(255,0,0));
						}
					// else printf("%d %d %d %d\n", x,y, textCache[i+1][TC_x], textCache[i+1][TC_y]);
				}
			}


			FlLayout* layout=mLayout->findLayout("Automatic segmentation");
			double min_text_gap=layout->findSlider("MIN text-gap")->value();
			if(drawDetectedSpace)
			{
				CImagePixel ip(bmp);
				for(int i=1; i<textCache.rows(); i++)
				{
					int* pinfo=textCache[i-1];
					int* info=textCache[i];

					if(pinfo[TC_y]==info[TC_y])
					{						
						int prevEnd=pinfo[TC_x]-pinfo[TC_fx]+pinfo[TC_w]-1;
						int curStart=info[TC_x]-info[TC_fx];

						int bottom=MIN(pinfo[TC_y]-pinfo[TC_fy], info[TC_y]-info[TC_fy]);
						int top=MAX(pinfo[TC_y]-pinfo[TC_fy]+pinfo[TC_h], info[TC_y]-info[TC_fy]+info[TC_h]);
						int space_thr=MAX(pinfo[TC_h], pinfo[TC_w]);
						space_thr=MAX(space_thr, info[TC_h]);
						space_thr=MAX(space_thr, info[TC_w]);
						space_thr*=min_text_gap;

						if(curStart-prevEnd<=space_thr && curStart>prevEnd)
						{
							// mark as space.
							ip.DrawBox(TRect(prevEnd, bottom, curStart, top), CPixelRGB8(0,0,0));
						}
					}					
				}

			}


		}	

		SummedAreaTable t(*bmp);//bmp->getWidth(), bmp->getHeight(), bmp->getDataPtr(), bmp->getRowSize());

		
		


		FlLayout* layout=mLayout->findLayout("Automatic segmentation");
		double min_gap_percentage=layout->findSlider("MIN gap")->value();
		double margin_percentage=layout->findSlider("Margin")->value();
		int thr_white=layout->findSlider("white point")->value();
		double max_width=1.0/layout->findSlider("N columns")->value();
		double cropT=layout->findSlider("Crop T")->value()/100.0;
		double cropB=layout->findSlider("Crop B")->value()/100.0;
		double cropL=layout->findSlider("Crop L")->value()/100.0;
		double cropR=layout->findSlider("Crop R")->value()/100.0;


//		TRect domain(0,0, bmp->GetWidth(), bmp->GetHeight());

		TRect domain(cropL*bmp->GetWidth(),cropT*bmp->GetHeight(), (1-cropR)*bmp->GetWidth()
, (1-cropB)*bmp->GetHeight());

	
		ImageSegmentation s(t, true, domain, 0, min_gap_percentage, thr_white);
		s.segment();
		std::list<TRect> results;
		s.getResult(results, max_width, margin_percentage);

		/*

		std::list<TRect> results;
		int min_gap=int((double)bmp->GetWidth()*min_gap_percentage*0.01 +0.5);
		int min_box_size=100;	// 10�ȼ������� �ڽ��� ������.

		CImagePixel orig(bmp_orig);

		std::list<index2> LRcorners;
		std::list<index2> ULcorners;
		for(int i=0, ni=bmp->GetWidth()-min_box_size-min_gap; i<ni; i++)
		{
			for(int j=0, nj=bmp->GetHeight()-min_box_size-min_gap; j<nj; j++)
			{
				// search for LR corner
				TRect outerBox(i, j, i+min_box_size+min_gap, j+min_box_size+min_gap);
				TRect innerBox(i, j, i+min_box_size, j+min_box_size);
				
				int sum1=t.sum(outerBox);
				int sum2=t.sum(innerBox);

				int area=outerBox.Width()*outerBox.Height()-
					innerBox.Width()*innerBox.Height();
				if(sum1-sum2 >= thr_white*area)
				{
					//orig.SetPixel(i+min_box_size+min_gap/2,j+min_box_size+min_gap/2,CPixelRGB8(255,0,0));

					LRcorners.push_back(index2(i+min_box_size+min_gap/2,j+min_box_size+min_gap/2));
				}

				// search for UL corner
				{
					TRect outerBox(i, j, i+min_box_size+min_gap, j+min_box_size+min_gap);
					TRect innerBox(i+min_gap, j+min_gap, i+min_box_size+min_gap, j+min_box_size+min_gap);
					
					int sum1=t.sum(outerBox);
					int sum2=t.sum(innerBox);

					int area=outerBox.Width()*outerBox.Height()-
						innerBox.Width()*innerBox.Height();
					if(sum1-sum2 >= thr_white*area)
					{
			//			orig.SetPixel(i+min_gap/2,j+min_gap/2,CPixelRGB8(0,0,255));
						ULcorners.push_back(index2(i+min_gap/2,j+min_gap/2));
					}
				}
			}
		}


		{
			std::list<index2>::iterator i;

			for(i=ULcorners.begin(); i!=ULcorners.end(); ++i)
			{
				orig.SetPixel((*i).x(), (*i).y(), CPixelRGB8(0,0,255));
			}
		}
		
		*/
		mRects.clear();

		int x=toWindowCoord(0,0).x;
		int y=toWindowCoord(0,0).y;

		std::list<TRect>::iterator i;
		for(i=results.begin(); i!=results.end(); i++)
		{
			mRects.push_back(SelectionRectangle());
			mSelectedRect=mRects.end();
			mSelectedRect--;
			SelectionRectangle& mRect=selectedRect();
			mRect.p1=toDocCoord((*i).left+x, (*i).top+y);
			mRect.p2=toDocCoord((*i).right+x, (*i).bottom+y);
			mRect.updateScreenCoord(*this);
		}
	}
}
Ejemplo n.º 3
0
void AHUD::GetActorsInSelectionRectangle(TSubclassOf<class AActor> ClassFilter, const FVector2D& FirstPoint, const FVector2D& SecondPoint, TArray<AActor*>& OutActors, bool bIncludeNonCollidingComponents, bool bActorMustBeFullyEnclosed)
{
	// Because this is a HUD function it is likely to get called each tick,
	// so make sure any previous contents of the out actor array have been cleared!
	OutActors.Reset();

	//Create Selection Rectangle from Points
	FBox2D SelectionRectangle(0);

	//This method ensures that an appropriate rectangle is generated, 
	//		no matter what the coordinates of first and second point actually are.
	SelectionRectangle += FirstPoint;
	SelectionRectangle += SecondPoint;


	//The Actor Bounds Point Mapping
	const FVector BoundsPointMapping[8] =
	{
		FVector(1, 1, 1),
		FVector(1, 1, -1),
		FVector(1, -1, 1),
		FVector(1, -1, -1),
		FVector(-1, 1, 1),
		FVector(-1, 1, -1),
		FVector(-1, -1, 1),
		FVector(-1, -1, -1)
	};

	//~~~

	//For Each Actor of the Class Filter Type
	for (TActorIterator<AActor> Itr(GetWorld(), ClassFilter); Itr; ++Itr)
	{
		AActor* EachActor = *Itr;

		//Get Actor Bounds				//casting to base class, checked by template in the .h
		const FBox EachActorBounds = Cast<AActor>(EachActor)->GetComponentsBoundingBox(bIncludeNonCollidingComponents); /* All Components? */

		//Center
		const FVector BoxCenter = EachActorBounds.GetCenter();

		//Extents
		const FVector BoxExtents = EachActorBounds.GetExtent();

		// Build 2D bounding box of actor in screen space
		FBox2D ActorBox2D(0);
		for (uint8 BoundsPointItr = 0; BoundsPointItr < 8; BoundsPointItr++)
		{
			// Project vert into screen space.
			const FVector ProjectedWorldLocation = Project(BoxCenter + (BoundsPointMapping[BoundsPointItr] * BoxExtents));
			// Add to 2D bounding box
			ActorBox2D += FVector2D(ProjectedWorldLocation.X, ProjectedWorldLocation.Y);
		}

		//Selection Box must fully enclose the Projected Actor Bounds
		if (bActorMustBeFullyEnclosed)
		{
			if(SelectionRectangle.IsInside(ActorBox2D))
			{
				OutActors.Add(Cast<AActor>(EachActor));
			}
		}
		//Partial Intersection with Projected Actor Bounds
		else
		{
			if (SelectionRectangle.Intersect(ActorBox2D))
			{
				OutActors.Add(Cast<AActor>(EachActor));
			}
		}
	}
}