summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_Image.cxx
blob: a1fb82afee4969cc016774d3018033f4ad8b67e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#define WTO0001         //GG_140596

#define PRO5663         //GG_291196

#include <Graphic2d_Image.ixx>
#include <Graphic2d_Image.pxx>
#include <Aspect_WindowDriver.hxx>
#include <Aspect_RGBPixel.hxx>
#include <TShort_Array1OfShortReal.hxx>
#include <Quantity_Array1OfColor.hxx>

Graphic2d_Image::Graphic2d_Image (const Handle(Graphic2d_GraphicObject)& aGraphicObject, const Handle(Image_Image)& anImage, const Quantity_Length X, const Quantity_Length Y, const Quantity_Length adx, const Quantity_Length ady, const Aspect_CardinalPoints aTypeOfPlacement)
  : Graphic2d_Primitive (aGraphicObject) , 
    myImage(anImage), 
    mydx(Standard_ShortReal(adx)), 
    mydy(Standard_ShortReal(ady)), 
    myPlacement(aTypeOfPlacement), 
    myIsModified(Standard_False) {

        SetFamily(Graphic2d_TOP_IMAGE);
        SetCenter(X,Y);
#ifndef PRO5663
        myMinX = myX; myMinY = myY;
        myMaxX = myX; myMaxY = myY;
#endif
}

void Graphic2d_Image::Clear () {

    myIsModified = Standard_True;
}

void Graphic2d_Image::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {

#ifdef OK
Standard_Boolean IsIn = Standard_False;
  if (! myGOPtr->IsTransformed ())
    IsIn = aDrawer->IsIn (myMinX,myMaxX,myMinY,myMaxY);
  else {
    Standard_ShortReal minx, miny, maxx, maxy;
#ifdef WTO0001
    MinMax(minx,maxx,miny,maxy);
#else
gp_GTrsf2d aTrsf = myGOPtr->Transform ();
Standard_Real MINX, MINY, MAXX, MAXY;

    MINX = Standard_Real (myMinX);
    MINY = Standard_Real (myMinY);
    MAXX = Standard_Real (myMaxX);
    MAXY = Standard_Real (myMaxY);
    aTrsf.Transforms (MINX, MINY);
    aTrsf.Transforms (MAXX, MAXY);
    minx = Standard_ShortReal (MINX);
    miny = Standard_ShortReal (MINY);
    maxx = Standard_ShortReal (MAXX);
    maxy = Standard_ShortReal (MAXY);
#endif
    IsIn = aDrawer->IsIn (minx,maxx,miny,maxy);
  }
#else
Standard_Boolean IsIn = Standard_True;
#endif

  if (IsIn) {

Standard_ShortReal a, b;
Standard_Real A, B;

        ComputeCenter (aDrawer, a, b);
#ifdef PRO5663
        if( myMinX == myMaxX ) { 
          Standard_ShortReal dx = aDrawer->Convert(myImage->Width());
          Standard_ShortReal dy = aDrawer->Convert(myImage->Height());
          Standard_ShortReal width  = aDrawer->ConvertMapToFrom(dx);
          Standard_ShortReal height = aDrawer->ConvertMapToFrom(dy);
          myMinX = a - Standard_ShortReal(width/2.); myMaxX = a + Standard_ShortReal(width/2.);
          myMinY = b - Standard_ShortReal(height/2.); myMaxY = b + Standard_ShortReal(height/2.);
        }
#endif
        if (myGOPtr->IsTransformed ()) {
                A = Standard_Real (a);
                B = Standard_Real (b);
                (myGOPtr->Transform ()).Transforms (A, B);
                a = Standard_ShortReal (A);
                b = Standard_ShortReal (B);
        }
        if (myIsModified) {
                myIsModified = Standard_False;
                aDrawer->ClearImage (myImage);
        }
        if (aDrawer->IsKnownImage (myImage))
                aDrawer->DrawImage (myImage, a, b);
        else
                FillAndDraw (aDrawer);

        // In case of GraphicObject Highlighted
        // bounding box of the image is traced
        if (myGOPtr->IsHighlighted ()) {

Standard_Integer Width  = myImage->Width ();
Standard_Integer Height = myImage->Height ();
                //
                // - 2 on Width and Height to avoid
                // border outside of the image.
                //
                if (Width > 2) Width -= 2;
                if (Height > 2) Height -= 2;
Standard_ShortReal dx = aDrawer->Convert (Width);
Standard_ShortReal dy = aDrawer->Convert (Height);

Standard_ShortReal width  = aDrawer->ConvertMapToFrom (dx);
Standard_ShortReal height = aDrawer->ConvertMapToFrom (dy);

TShort_Array1OfShortReal TheX (1, 5);
TShort_Array1OfShortReal TheY (1, 5);

                TheX (1)        = a - Standard_ShortReal(width / 2.);
                TheY (1)        = b - Standard_ShortReal(height / 2.);
                TheX (2)        = a - Standard_ShortReal(width / 2.);
                TheY (2)        = b + Standard_ShortReal(height / 2.);
                TheX (3)        = a + Standard_ShortReal(width / 2.);
                TheY (3)        = b + Standard_ShortReal(height / 2.);
                TheX (4)        = a + Standard_ShortReal(width / 2.);
                TheY (4)        = b - Standard_ShortReal(height / 2.);
                TheX (5)        = a - Standard_ShortReal(width / 2.);
                TheY (5)        = b - Standard_ShortReal(height / 2.);

                aDrawer->SetLineAttrib (myColorIndex, 0, 0);
                aDrawer->MapPolylineFromTo (TheX, TheY);
        }

  }
}

Standard_Boolean Graphic2d_Image::Pick (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal aPrecision,const Handle(Graphic2d_Drawer)& aDrawer) {

Standard_ShortReal dx = aDrawer->Convert(myImage->Width());
Standard_ShortReal dy = aDrawer->Convert(myImage->Height());

Standard_ShortReal width  = aDrawer->ConvertMapToFrom(dx);
Standard_ShortReal height = aDrawer->ConvertMapToFrom(dy);

  Standard_ShortReal a,b;
  ComputeCenter(aDrawer,a,b);

Standard_ShortReal SRX = X, SRY = Y;

  if (myGOPtr->IsTransformed ()) {
gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
Standard_Real RX = Standard_Real (SRX), RY = Standard_Real (SRY);
        aTrsf.Transforms (RX, RY);
        SRX = Standard_ShortReal (RX); SRY = Standard_ShortReal (RY);
  }

  return
      (       SRX >= a - width  / 2. - aPrecision
         &&   SRX <= a + width  / 2. + aPrecision

         &&   SRY >= b - height / 2. - aPrecision
         &&   SRY <= b + height / 2. + aPrecision  );

}

void Graphic2d_Image::SetCenter (const Quantity_Length X, const Quantity_Length Y) {

        myX     = Standard_ShortReal(X);
        myY     = Standard_ShortReal(Y);
#ifdef PRO5663
        myMinX = myMaxX = myX;
        myMinY = myMaxY = myY;
#endif

}

void Graphic2d_Image::Translate (const Quantity_Length DX, const Quantity_Length DY) {

        myX     = myX + Standard_ShortReal(DX);
        myY     = myY + Standard_ShortReal(DY);
#ifdef PRO5663
        myMinX = myMaxX = myX;
        myMinY = myMaxY = myY;
#endif

}

void Graphic2d_Image::SetSmallSize (const Standard_Integer aSize) {

        TheSmallSize    = aSize;

}

Standard_Integer Graphic2d_Image::SmallSize () {

        return (TheSmallSize);

}

// Private methods

void Graphic2d_Image::FillAndDraw (const Handle(Graphic2d_Drawer)& aDrawer) const {

Standard_ShortReal a,b;

Standard_Integer LowerY;
Standard_Integer UpperY;
Standard_Integer Width, Height;
Aspect_RGBPixel *pixels;

Standard_Integer indice;
Standard_Integer i, ii;
Standard_Integer j;

Standard_Real R, G, B;

        ComputeCenter(aDrawer,a,b);

        Width   = myImage->Width ();
        Height  = myImage->Height ();

        LowerY  = myImage->LowerY ();
        UpperY  = myImage->UpperY ();

        if (Height*Width <= Graphic2d_Image::SmallSize ()) {
                // Small images are filled at once
                pixels  = new Aspect_RGBPixel [Height*Width];

                Quantity_Array1OfColor Array (0, Width-1);

                // Parsing of the image Lines-Columns (Height-Width)
                for (i=0, ii=LowerY; ii<=UpperY; i++, ii++) {
                        myImage->RowColor (ii, Array);
                        for (j=0; j<Width; j++) {
                                Array (j).Values (R, G, B, Quantity_TOC_RGB);
                                indice  = i * Width + j;
                                pixels[indice].red      = float (R);
                                pixels[indice].green    = float (G);
                                pixels[indice].blue     = float (B);
                        }
                }

                aDrawer->FillAndDrawImage
                        (myImage, a, b, Width, Height,
                        (Standard_Address*) pixels);

                delete [] pixels;
        }
        else {
                // Great images are filled line by line
                pixels  = new Aspect_RGBPixel [Width];

                Quantity_Array1OfColor Array (0, Width-1);

                // Parsing of image Lines-Columns (Height-Width)
                for (i=0, ii=LowerY; ii<=UpperY; i++, ii++) {
                        myImage->RowColor (ii, Array);
                        for (j=0; j<Width; j++) {
                                Array (j).Values (R, G, B, Quantity_TOC_RGB);
                                pixels[j].red   = float (R);
                                pixels[j].green = float (G);
                                pixels[j].blue  = float (B);
                        }

                        aDrawer->FillAndDrawImage
                                (myImage, a, b, i, Width, Height,
                                (Standard_Address*)  pixels);
                }

                delete [] pixels;
        }

}
void Graphic2d_Image::ComputeCenter(const Handle(Graphic2d_Drawer)& aDrawer,
                                    Standard_ShortReal& cx,
                                    Standard_ShortReal& cy) const {
     Standard_ShortReal dx = aDrawer->Convert(myImage->Width());
     Standard_ShortReal dy = aDrawer->Convert(myImage->Height());

     Standard_ShortReal width  = aDrawer->ConvertMapToFrom(dx);
     Standard_ShortReal height = aDrawer->ConvertMapToFrom(dy);

     Standard_ShortReal ox = aDrawer->ConvertMapToFrom(mydx);
     Standard_ShortReal oy = aDrawer->ConvertMapToFrom(mydy);

     switch (myPlacement) {
        case Aspect_CP_North:     { cx = myX + ox           ; 
                                    cy = myY + oy - Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_NorthEast: { cx = myX + ox - Standard_ShortReal(width/2.); 
                                    cy = myY + oy - Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_East:      { cx = myX + ox - Standard_ShortReal(width/2.); 
                                    cy = myY + oy ; break; };
        case Aspect_CP_SouthEast: { cx = myX + ox - Standard_ShortReal(width/2.); 
                                    cy = myY + oy + Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_South:     { cx = myX + ox           ; 
                                    cy = myY + oy + Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_SouthWest: { cx = myX + ox + Standard_ShortReal(width/2.); 
                                    cy = myY + oy + Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_West:      { cx = myX + ox + Standard_ShortReal(width/2.); 
                                    cy = myY + oy ; break; };
        case Aspect_CP_NorthWest: { cx = myX + ox + Standard_ShortReal(width/2.); 
                                    cy = myY + oy - Standard_ShortReal(height/2.) ; break; };
        case Aspect_CP_Center:    { cx = myX + ox; 
                                    cy = myY + oy ; break; };
     }
}

void Graphic2d_Image::SetOffset(const Quantity_Length dx, const Quantity_Length dy) {

        mydx    = Standard_ShortReal(dx);
        mydy    = Standard_ShortReal(dy);
#ifdef PRO5663
        myMinX = myMaxX = myX;
        myMinY = myMaxY = myY;
#endif
}

void Graphic2d_Image::SetPlacement(const Aspect_CardinalPoints aPlacement) {

        myPlacement     = aPlacement;
#ifdef PRO5663
        myMinX = myMaxX = myX;
        myMinY = myMaxY = myY;
#endif

}

void Graphic2d_Image::Position( Quantity_Length& X,Quantity_Length& Y ) const {
  
  X = Quantity_Length( myX );
  Y = Quantity_Length( myY );

}

void Graphic2d_Image::Offset(Quantity_Length& aX,Quantity_Length& aY) const {
    
  aX = Quantity_Length( mydx );
  aY = Quantity_Length( mydy );
}

Aspect_CardinalPoints Graphic2d_Image::Placement() const {
  return myPlacement;
}

Handle(Image_Image) Graphic2d_Image::Image() const {
  return myImage;
}

void Graphic2d_Image::Save(Aspect_FStream& aFStream) const
{
}