version of unit 2.03 English version(Czech text follows) If you hate more text run the program DEMO.EXO with parameter LYNNE.PCX (or other picture) first. 1. Compilation of unit The source code was written in environment Turbo Pascal r 7.0 (real, protected and windows) and should be translated in new releases. It was successfully tried to using this unit under Delphi r 1.0. The file MODIF.INC must be created for compilation. This file may be empty. If a program will run in graphics mode, the following line must be included in file MODIF.INC: {$define graph} 2. Data structures In the base lie a picture object with a following items: picture=object(AbstractPicture) filename:pathstr; x,y:integer; planes:byte; data:^lines; Function Valid:Boolean; virtual; procedure View; virtual; procedure Create(SizeX,SizeY:Word;pln:Byte); procedure Erase; Constructor Init; Destructor Done; end; where:x,y mean size of picture planes number of bit planes (lze pouzit pouze 1,2,4,8) If picture is loaded this value is not recomended to change data pointer to data stored in memory filename name of disk file, where is picture stored And following object describes vector picture data: VectorPicture=object(AbstractPicture) xscale,yscale:integer; lines:Word; v:^AVectorObject; Procedure View; virtual; Procedure Erase; constructor Init; destructor Done; end; where xscale,yscale sizes of vector picture lines number of vector objects in item v v pointer to item of pointers to single vector objects Here is definition of vector objects: VectorObject=record points:word; color:Byte; data:array[0..0] of point; end; where points number of points (vertexes) obsa‘en˜ch v objektu color the color of object data pole jednotliv˜ch bod– pat©¡c¡ch objektu Structure where is stored histogram data: PHistogram=^THistogram; THistogram=Array[0..255]of Longint; Agreement:Axis Y is position of row. (Rows are stored separately) Axis X is position of column (pixel in row). 3. Basic methods of object picture Function picture.Valid:Boolean; virtual; Returning value is True, if picture contains valid data. Else False is returned. procedure picture.View; virtual; Object is showed on the screen. (see also procedure View). Switch {$define graph} must be defined. procedure picture.Create(SizeX,SizeY:Word;pln:Byte); A new free picture is created, which have needed size and depth of a color. procedure picture.Erase; All object data are erased. Only one exception is the disk file name. This method can be called anywhere in program. Constructor picture.Init; Initialization of all data object. This method must be called before first using of this object. Destructor picture.Done; At first is called method picture.Erase and then all information are destructed. 4. Procedures for processing pictures 4.1. Mask filtration Procedure Mask(var Obr1,Obr2:Picture;n:Byte); The mask filtration is done. Size of a mask is n. Input picture is stored in obr1 and output picture is stored in obr 2. Algorithm of decomposed mask is used, and its speed doesn't depend on sizes of mask. 4.2. Median filtration Procedure Median(var Obr1,Obr2:Picture;n:Byte); This procedure accomplish median filtration from picture Obr1 to pictur Obr2 with size n. After calling procedure is one of 2 algorithms selected according to size of element. For n<8 is selected fast algorithm with time consumption n^3 and for greater n another algorithm is used with time consumption k*n^2. The time for n>20 may be too long. 4.3. Copy of image Procedure MovePicture(var srP,destP:picture); This procedure copy contents of the source picture to the destination picture. If destination picture was not clear, then its contents was overwritten. No changes are performed on the source picture. 4.4. Gaussian mask Procedure Gauss(var Obr1,Obr2:Picture;n:Byte); This procedure accomplish filtration with gaussian from picture Obr1 to pictur Obr2 with size n. 4.5. Crop of image: Procedure Crop(var Obr1,Obr2:Picture;x0,y0,x1,y1:word); Cropped obr1 with axises x0,y0,x1,y1.is stored to obr2. If parameter y1=0, then axises y0 and y1 are selected automatically If parameter x1=0, then axises x0 and x1 are selected automatically Automatical selection cuts border of picture with the same color. 4.6. Edge detection Procedure Canny(var im_in,im_out:picture;GausN:integer;scale:real); This procedure accomplish detection of edges from picture im_in. Result of this operation is stored in picture im_out. Parameter GausN tells size of Gausssian mask. It means number of iteration steps with using elemental mask 3x3 - it is scale of image. Parameter scale avoid multiplying result with const. This may be useful if results of canny detection is too slow or too high. Null or negative value of parameter GausN modify activity of edge detection following way: Algorithm now perform edge back-tracking throw scales from starting scale in parameter GausN downto 1. (gausN,abs(scale)) - >...->(1,abs(scale)). If GausN=0 then starting value of the scale is 40. 4.7. Operations with one picture Procedure Operation1(var obr1:picture; Operation:func1; ukParameters:Pointer); This procedure perform transformation of brightness in one picture, passed as parameter, and result is stored to same picture. Parameter Operation contains selected function. User defined data, pointer to it is stored in parameter Param are passed to this procedure, when this is called. These following functions are predefined: Procedure Invert(P1:Pointer;MaxX:Word;Dummy:pointer); {R=-R} Procedure ReTabB(P1:Pointer;MaxX:Word;TabPtr:pointer); {R:=Tab[R]} User may define any new function and pass it as parameter. Only one conditions must be realized: procedure must be far type and its header must be same with the following definition: func1 = Procedure(P1:Pointer;MaxX:Word;ukParameters:Pointer); 4.8. Operations with two pictures Procedure Operation2(var obr1,obr2:picture; Operation:func2; Param:Pointer); This procedure perform transformation of brightness from two pictures and result is stored to obr2. Parameter Operation contains selected function. User defined data, pointer to it is stored in parameter Param are passed to this procedure, when this is called. These following functions are predefined: Procedure SubbS(P1,P2:Pointer;MaxX:Word;Dummy:pointer); {R1:=R2-R1} Procedure Add(P1,P2:Pointer;MaxX:Word;Dummy:pointer); {R1:=R2+R1} 4.9. Computation of histogram Procedure Histogram(var p:picture;var H:Phistogram); 5. Operations with binary pictures Definition of structure element: For morphology operation it is required as parameter form of structure element. This element will be used for convolution. Following types of structure elements are predefined: const ElLup= 4; ElUp =8; ElRup =16; ElLeft= 2; ElCentr=1; ElRight=32; ElLdown=256; ElDown =128; ElRdown=64; Using combination of this single elements you may make up any structure element till size 3x3. There are also predefined two arranged elements: El8=ElLup+ElUp+ElRup+ElLeft+ElCentr+ElRight+ElLdown+ElDown+ElRdown; El4=ElUp+ElLeft+ElCentr+ElRight+ElDown; 5.1. Tresholding Procedure Thr(var Obr,ph:picture;h:integer); It is executed tresholding of image Obr with threshold value h. Final binary image is stored into structure ph. Threshold value 0 is reserved for automatic choice of the value h {!!it will be implemented in future releases!!}. 5.2. Dilatation Procedure Dilatation(var Obr,ObrD:Picture;typ:Word); Input picture is dilatated with using structure element typ. Final image is stored to structure ObrD (For the present this procedure works only with binary images!). Variable typ contains fashion of the structure element. 5.3. Erosion Procedure Erosion(var Obr,ObrD:Picture;typ:Word); Input picture is erosed with using structure element typ. Final image is stored to structure ObrD (For the present this procedure works only with binary images!). Variable typ contains fashion of the structure element. 5.4. Binary operations with two images Procedure BinarOperation2(var Obr1,Obr2:Picture; Operation:BinarFunc2); This procedure perform passed binary function from two binary images. Parameter Operation contains desired function. For the present there are defined following functions: Procedure XorR(maxX:Word;R1,R2:Pointer); {R1:=R1 xor R2} Procedure OrR(maxX:Word;R1,R2:Pointer); {R1:=R1 or R2} Procedure AndR(maxX:Word;R1,R2:Pointer); {R1:=R1 and R2} User can add any new function. This function may be then passed as parameter. Only one condition is then procedure must be far type and its header must be same with this definition: Binarfunc2 = Procedure(maxX:word;p1,p2:pointer); 6. Input/Output Operations 6.1. Loading the image Procedure LoadPicture(var p:AbstractPicture;const s:PathStr); Image with engaged name s is loaded from disk. Type of the image is recognized from type of passed variable p. The file name is contained in variable s. If empty string is passed, it is used implicit name of image in variable p.filename.. This name can be changed using procedure AssignPicture. It is supported following formats of bitmap images: (BMP, GIF, PCX, RAS, OKO, FTG, TXT). For vector images there are available two formats: .TXT (text) a .HPG (text for plotter). 6.2. Saving image Function SavePicture(var p:picture;const s:PathStr):Boolean; This function store image to disk. Type of the image data is founded from type of image variable passed in parameter p. The file name is included in variable s. If the string is empty, then implicit name of image is used. Implicit name is located in variable p.filename. You may change implicit name using procedure AssignPicture. Following bitmap image formats are supported:(BMP, PCX, RAS, OKO, FTG, TXT). For vector image are supported these two formats: .TXT (text) and .HPG (text for plotter). 6.3. New image format FTG FTG is defined in additional unit FTGR.PAS. It contains new method of image compression, that has best performance of compression ratio. Comparison of lengths: data type: Fig A Fig B Fig C OKO 262656 442112 106940 ZIP 227173 220369 10224 ARJ 228451 220547 10380 AIN 228496 221045 10559 PCX 288650 377897 38624 FTG 199697 177539 8922 - new format 196440 - + LZW Figure A is gray scaled 256 levels of brightness scanned using the camera - artificial scene. Figure B is gray scaled 256 levels of brightness scanned using the camera -sight into the room. Figure C is binary image - technical drawing (graphs). 7. Other procedures for manipulation with images 7.1. Displaying Procedure view(var p:abstractPicture); Image stored in variable p is displayed. For using this function must be feature graphics enabled. In file MODIF.INC must be included {$define graph}. 7.2. Reading value of the pixel Function pixel(var p:picture;x,y:integer):Word; This function returns value stored in the image pixel on position [x,y]. If needed coordinates of the actual point lies outside figure, then nearest point in picture is selected. Algorithm uses this equation for the selection: [max(0,min(SizeX,X)), max(0,min(SizeY,Y))]. 7.3. Set pixel value Procedure SetPixel(var p:picture; x,y:integer; n:Word); This procedure modify value of the pixel on position [x,y]. If needed coordinates of the actual point lies outside figure, then nearest point in picture is selected. Algorithm uses this equation for the selection: [max(0,min(SizeX,X)), max(0,min(SizeY,Y))]. 7.4. Format of a row Procedure FormR(var p:picture; i:integer; Data:Pointer); All row is converted into unified format words. Pointer Data points onto array of words. Memory with correct size must be allocated before calling this procedure. If needed number of row exceeds size of image then compensatory line is selected according to equation: max(0,min(SizeY,Y)). 7.5. Set of row Procedure RfrmB(var p:picture;i: integer; NewData:Pointer); All rows elements are changed. New values are getted from array of words. Pointer NewData points onto this array. This perform inverse operation to previous FormR. If needed number of row exceeds size of image then compensatory line is selected according to equation: max(0,min(SizeY,Y)). 8. Ready programs Some complete programs are added to unit pictures. These demonstrate its abilities in processing images. You may use some of these routines independently. Executable files are stored into 3 directories according to used environment: DOS works only in real mode DPMI works only in DPMI mode - you may work with great pictures WIN works in Windows 8.1. Convert Parametters:input_image output_image [OutputPlanes] [/Gray] [/NoScale] /? print text help /Gray destroy palette and transfer colors into gray scale /NoScale In conversions of bit planes data will be not recounted or scaled, but they'll be cutted. OutputPlanes Number of bit planes in the output image. This program make conversion input image into output image. In this conversion is change of number bitplanes or removing palette allowed. Number of bitplanes may be one only of these numbers [1, 2, 4, 8, 16, 24]. If the noscale switch is on, data isn't transformed (or multiplied) any way. If some value overcome maximal allowed number, this max number depend on number of bit planes, then the pixel is set to this maximal value (cutting). 8.2. View Parametters:name_of_image show image on the screen /? print text help This programm is designated for viewing images on the monitor screen. It support modes with 256 colors. It is abble to display all formats which procedure LoadPicture can load: BMP 1,4,8,24 bit planes + palette FTG 1,2,4,8,16,24,32 planes (+selectable palette) GIF 1,2,4,8 planes + paltte OKO 1,8 bit planes (only gray scalled) PCX 1*1,1*2,1*4,8*1 + palette RAS types 0(1b),1(8b),2(8b),3(4b),4(4b) In viewing user can interactively change resolution of screen using function keys from F1 till F6. If user press key G, then palette is temporary converted to gray/restored to original colors.