1 /*
2 Copyright (c) 2019- Ferhat Kurtulmuş
3 Boost Software License - Version 1.0 - August 17th, 2003
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 The copyright notices in the Software and this entire statement, including
11 the above license grant, this restriction and the following disclaimer,
12 must be included in all copies of the Software, in whole or in part, and
13 all derivative works of the Software, unless such copies or derivative
14 works are solely in the form of machine-executable object code generated by
15 a source language processor.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24 
25 /* 
26 C Interface. Created for the python binding, but I think it can also be used
27 to create bindings to other languages.
28 */
29 module rpcinterface;
30 
31 import std.stdio;
32 
33 import measure.regionprops;
34 import measure.types;
35 import core.stdc.stdlib;
36 
37 private {
38     RegionProps rp;
39 }
40 
41 extern (C) void _rprops(ubyte* _data, uint rows, uint cols, ulong *n) { 
42     ubyte[] data = _data[0..rows*cols];
43     
44     auto imgbin = Mat2D!ubyte(data, rows, cols);
45     rp = new RegionProps(imgbin);
46     rp.calculateProps();
47     *n = rp.regions.length;
48 }
49 
50 extern (C) {
51     ulong getArea(ulong i){ return rp.regions[i].area;}
52     void getRawMoments(ulong i, double[10] m){
53         m = [rp.regions[i].m00, 
54             rp.regions[i].m10, 
55             rp.regions[i].m01, 
56             rp.regions[i].m20, 
57             rp.regions[i].m11,
58             rp.regions[i].m02,
59             rp.regions[i].m30,
60             rp.regions[i].m21,
61             rp.regions[i].m12, 
62             rp.regions[i].m03];
63     }
64     double getAreaFromContour(ulong i){return rp.regions[i].areaFromContour;}
65     double getPerimeter(ulong i){return rp.regions[i].perimeter;}
66     void getCentroid(ulong i, double[2] centroid){
67         centroid = [rp.regions[i].centroid.x, rp.regions[i].centroid.y];
68     }
69     double getAspect_Ratio(ulong i){return rp.regions[i].aspect_Ratio;}
70     void getBbox(ulong i, double[4] bBox){
71         bBox = [rp.regions[i].bBox.x, rp.regions[i].bBox.y, rp.regions[i].bBox.width, rp.regions[i].bBox.height];
72     }
73     
74     ulong getChLen(ulong i){return rp.regions[i].convexHull.xs.length;}
75     void getConvh(ulong i, int *convhXs, int *convhYs){
76         auto n = rp.regions[i].convexHull.xs.length;
77         convhXs[0..n] = rp.regions[i].convexHull.xs[];
78         convhYs[0..n] = rp.regions[i].convexHull.ys[];
79     }
80     
81     double getConvexArea(ulong i) {return rp.regions[i].convexArea;}
82     void getEllipse(ulong i, double[5] ellipse){
83         ellipse = [rp.regions[i].ellipse.angle,
84                 rp.regions[i].ellipse.center_x,
85                 rp.regions[i].ellipse.center_y,
86                 rp.regions[i].ellipse.maj,
87                 rp.regions[i].ellipse.min];
88     }
89     double getExtent(ulong i){return rp.regions[i].extent; }
90     double getSolidity(ulong i){return rp.regions[i].solidity; }
91     double getMajorAxisLength(ulong i){return rp.regions[i].majorAxisLength; }
92     double getMinorAxisLength(ulong i){return rp.regions[i].minorAxisLength; }
93     double getOrientation(ulong i){return rp.regions[i].orientation; }
94     double getEccentricity(ulong i){return rp.regions[i].eccentricity; }
95     double getEquivalentDiameter(ulong i){return rp.regions[i].equivalentDiameter; }
96     
97     ulong getConPxLen(ulong i){return rp.regions[i].contourPixelList.xs.length;}
98     void getContourPixelList(ulong i, int *contXs, int *contYs){
99         auto n = rp.regions[i].contourPixelList.xs.length;
100         contXs[0..n] = rp.regions[i].contourPixelList.xs[];
101         contYs[0..n] = rp.regions[i].contourPixelList.ys[];
102     }
103     
104     ulong getPxLen(ulong i){return rp.regions[i].pixelList.xs.length;}
105     void getPixelList(ulong i, int *pixXs, int *pixYs){
106         auto n = rp.regions[i].pixelList.xs.length;
107         pixXs[0..n] = rp.regions[i].pixelList.xs[];
108         pixYs[0..n] = rp.regions[i].pixelList.ys[];
109     }
110 }