Page 1 of 1

Optimizing Start/Stopdrawing?

Posted: Sat Mar 02, 2024 10:42 am
by Michael Vogel
I need to get characters ('0'...'9') into a matrix representing the gray values of the pixels, where all characters have to be set to a fixed size.
I did the job by finding the position and size of each character, then I find the minimum box outlining all characters, crop the bitmaps and calculate the matrix values.

Everything works fine but it would be fine to do some tuning (speed up, reducing memory, etc.).

For instance, how to get rid of the sequence StopVectorDrawing() / GrabImage(11...) / StartVectorDrawing(...) ?

Code: Select all

; Define

	#Shades=4

	Structure CharType
		Width.i
		Height.i
		DotWidth.i
		Bytes.i
	EndStructure

	Global Char.CharType
	Global Dim Matrix(11,1)

	With Char
		\Width=12
		\Height=25
	EndWith

	LoadFont(0,"Segoe UI",10,#PB_Font_Italic)
	;LoadFont(0,"Vivaldi",10,#PB_Font_Bold)

; EndDefine

Procedure CreateFont()

	Protected.f cw,ch,fw,fh,mx,my,ox,oy,px,py
	Protected i,xmin,xmax,ymin,ymax,z
	Protected.s s

	CreateImage(11,320,280,32,#White)
	StartVectorDrawing(ImageVectorOutput(11))
	VectorFont(FontID(0),200)

	xmin=9999
	ymin=9999

	For i=0 To 10
		s=Chr('0'+i)

		If i=0
			fw=VectorTextWidth(s,#PB_VectorText_Default)
			fh=VectorTextHeight(s,#PB_VectorText_Default)
		EndIf
		cw=VectorTextWidth(s,#PB_VectorText_Visible)
		ch=VectorTextHeight(s,#PB_VectorText_Visible)
		ox=VectorTextWidth(s,#PB_VectorText_Visible|#PB_VectorText_Offset)
		oy=VectorTextHeight(s,#PB_VectorText_Visible|#PB_VectorText_Offset)
		mx=(fw-cw)/2
		my=(fh-ch)/2

		z=Round(mx,#PB_Round_Down) :	If xmin>z : xmin=z : EndIf :
		z=Round(mx+cw,#PB_Round_Up) :	If xmax<z : xmax=z : EndIf :
		z=Round(my,#PB_Round_Down) :	If ymin>z : ymin=z : EndIf :
		z=Round(my+ch,#PB_Round_Up) :	If ymax<z : ymax=z : EndIf :

		#KerningX=50
		MovePathCursor(#KerningX+mx-ox,my-oy)
		DrawVectorText(s)

		StopVectorDrawing()
		GrabImage(11,i,0,0,fw+#KerningX*2,fh)
		StartVectorDrawing(ImageVectorOutput(11))
		VectorSourceColor($FFFFFFFF)
		FillVectorOutput()
		VectorSourceColor($FF0000000)
		VectorFont(FontID(0),200)
	Next i

	For i=0 To 10
		GrabImage(i,i,#KerningX+xmin,ymin,xmax-xmin,ymax-ymin)
		ResizeImage(i,Char\Width,Char\Height,#PB_Image_Smooth)
	Next i

	StopVectorDrawing()

	Char\DotWidth=cw*Char\Width/(xmax-xmin); Breite des Doppelpunkts
	Char\Bytes=Char\Width*Char\Height

	Protected n
	Protected ix,iy
	Protected w,o

	ReDim Matrix(11,Char\Bytes)

	w=Char\Width

	For i=0 To 10
		If i=10
			w=Char\DotWidth
			o=(Char\Width-w)/2
		EndIf
		StartDrawing(ImageOutput(i))
		n=0
		iy=0
		While iy<Char\Height
			ix=0
			While ix<w
				z=($FF-(Point(o+ix,iy)&$FF))*#Shades/255
				Matrix(i,n)=Round(z,#PB_Round_Nearest)
				n+1
				ix+1
			Wend
			iy+1
		Wend
		StopDrawing()
	Next i

EndProcedure
Procedure Dot(x,y,shade)

	#Size=8

	x*#Size
	y*#Size

	x+20
	y+20
	Box(x,y,#Size,#Size,$ffffff-$3f3f3f*shade)

EndProcedure
Procedure test()

	CreateFont()

	OpenWindow(0,0,0,800,200,"*",#PB_Window_ScreenCentered)
	CanvasGadget(0,0,0,800,200)

	StartDrawing(CanvasOutput(0))
	With Char

		y=0
		n=0
		While y<\Height
			x=0
			While x<\Width
				For c=0 To 9
					Dot(x+c*\Width,y,Matrix(c,n))
				Next c
				n+1
				x+1
			Wend
			y+1
		Wend

	EndWith
	StopDrawing()



	Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow

	EndProcedure
test()