1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef C_INCLUDE_DRAWING_MATRIX_H
17 #define C_INCLUDE_DRAWING_MATRIX_H
18 
19 /**
20  * @addtogroup Drawing
21  * @{
22  *
23  * @brief Provides functions such as 2D graphics rendering, text drawing, and image display.
24  *
25  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
26  *
27  * @since 11
28  * @version 1.0
29  */
30 
31 /**
32  * @file drawing_matrix.h
33  *
34  * @brief Declares functions related to the <b>matrix</b> object in the drawing module.
35  *
36  * @kit ArkGraphics2D
37  * @library libnative_drawing.so
38  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
39  * @since 11
40  * @version 1.0
41  */
42 
43 #include "drawing_error_code.h"
44 #include "drawing_types.h"
45 
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49 
50 /**
51  * @brief Creates an <b>OH_Drawing_Matrix</b> object.
52  *
53  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
54  * @return Returns the pointer to the <b>OH_Drawing_Matrix</b> object created.
55  * @since 11
56  * @version 1.0
57  */
58 OH_Drawing_Matrix* OH_Drawing_MatrixCreate(void);
59 
60 /**
61  * @brief Creates an <b>OH_Drawing_Matrix</b> object with rotation. Sets matrix to
62  * rotate by degrees about a pivot point at (px, py).
63  *
64  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
65  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
66  * @param deg  angle of axes relative to upright axes
67  * @param x  pivot on x-axis.
68  * @param y  pivot on y-axis.
69  * @since 12
70  * @version 1.0
71  */
72 OH_Drawing_Matrix* OH_Drawing_MatrixCreateRotation(float deg, float x, float y);
73 
74 /**
75  * @brief Creates an <b>OH_Drawing_Matrix</b> object with scale. Sets matrix to scale
76  * by sx and sy, about a pivot point at (px, py).
77  *
78  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
79  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
80  * @param sx  horizontal scale factor.
81  * @param sy  vertical scale factor.
82  * @param px  pivot on x-axis.
83  * @param py  pivot on y-axis.
84  * @return Returns the pointer to the <b>OH_Drawing_Matrix</b> object created.
85  * @since 12
86  * @version 1.0
87  */
88 OH_Drawing_Matrix* OH_Drawing_MatrixCreateScale(float sx, float sy, float px, float py);
89 
90 /**
91  * @brief Creates an <b>OH_Drawing_Matrix</b> object with translation.
92  *
93  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
94  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
95  * @param dx  horizontal translation.
96  * @param dy  vertical translation.
97  * @return Returns the pointer to the <b>OH_Drawing_Matrix</b> object created.
98  * @since 12
99  * @version 1.0
100  */
101 OH_Drawing_Matrix* OH_Drawing_MatrixCreateTranslation(float dx, float dy);
102 
103 /**
104  * @brief Sets the params for a matrix.
105  *
106  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
107  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
108  * @param scaleX  horizontal scale factor to store
109  * @param skewX   horizontal skew factor to store
110  * @param transX  horizontal translation to store
111  * @param skewY   vertical skew factor to store
112  * @param scaleY  vertical scale factor to store
113  * @param transY  vertical translation to store
114  * @param persp0  input x-axis values perspective factor to store
115  * @param persp1  input y-axis values perspective factor to store
116  * @param persp2  perspective scale factor to store
117  * @since 11
118  * @version 1.0
119  */
120 void OH_Drawing_MatrixSetMatrix(OH_Drawing_Matrix*, float scaleX, float skewX, float transX,
121     float skewY, float scaleY, float transY, float persp0, float persp1, float persp2);
122 
123 /**
124  * @brief Enumerates of scale to fit flags, how matrix is constructed to map one rect to another.
125  *
126  * @since 12
127  * @version 1.0
128  */
129 typedef enum {
130     /**
131      * Scales in x and y to fill destination rect.
132      */
133     SCALE_TO_FIT_FILL,
134     /**
135      * Scales and aligns to left and top.
136      */
137     SCALE_TO_FIT_START,
138     /**
139      * Scales and aligns to center.
140      */
141     SCALE_TO_FIT_CENTER,
142     /**
143      * Scales and aligns to right and bottom.
144      */
145     SCALE_TO_FIT_END,
146 } OH_Drawing_ScaleToFit;
147 
148 /**
149  * @brief Sets matrix to scale and translate src rect to dst rect.
150  *
151  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
152  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
153  * @param src Indicates the pointer to an <b>OH_Drawing_Rect</b> object rect to map from.
154  * @param dst Indicates the pointer to an <b>OH_Drawing_Rect</b> object rect to map to.
155  * @param stf Scales to fit enum method.
156  * @return Returns true if dst is empty, and sets matrix to:
157  *         | 0 0 0 |
158  *         | 0 0 0 |
159  *         | 0 0 1 |
160  *
161  * @since 12
162  * @version 1.0
163  */
164 bool OH_Drawing_MatrixSetRectToRect(OH_Drawing_Matrix*, const OH_Drawing_Rect* src,
165     const OH_Drawing_Rect* dst, OH_Drawing_ScaleToFit stf);
166 
167 /**
168  * @brief Sets matrix to matrix multiplied by matrix constructed from rotating by degrees
169  * about pivot point(px, py), positive degrees rotates clockwise.
170  *        Given:
171  *
172  *                     | A B C |                        | c -s dx |
173  *            Matrix = | D E F |,  R(degrees, px, py) = | s  c dy |
174  *                     | G H I |                        | 0  0  1 |
175  *
176  *        where:
177  *
178  *            c  = cos(degrees)
179  *            s  = sin(degrees)
180  *            dx =  s * py + (1 - c) * px
181  *            dy = -s * px + (1 - c) * py
182  *
183  *        sets Matrix to:
184  *
185  *                                          | A B C | | c -s dx |   | Ac+Bs -As+Bc A*dx+B*dy+C |
186  *            Matrix * R(degrees, px, py) = | D E F | | s  c dy | = | Dc+Es -Ds+Ec D*dx+E*dy+F |
187  *                                          | G H I | | 0  0  1 |   | Gc+Hs -Gs+Hc G*dx+H*dy+I |
188  *
189  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
190  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
191  * @param degree Indicates the angle of axes relative to upright axes.
192  * @param px Indicates the pivot on x-axis.
193  * @param py Indicates the pivot on y-axis.
194  * @since 12
195  * @version 1.0
196  */
197 void OH_Drawing_MatrixPreRotate(OH_Drawing_Matrix*, float degree, float px, float py);
198 
199 /**
200  * @brief Sets matrix to forward scale by sx and sy, about a pivot point at (px, py).
201  *        Given:
202  *
203  *                    | A B C |                       | sx  0 dx |
204  *            Matrix =| D E F |,  S(sx, sy, px, py) = |  0 sy dy |
205  *                    | G H I |                       |  0  0  1 |
206  *
207  *        where:
208  *
209  *            dx = px - sx * px
210  *            dy = py - sy * py
211  *
212  *        sets Matrix to:
213  *
214  *                                         | A B C | | sx  0 dx |   | A*sx B*sy A*dx+B*dy+C |
215  *            Matrix * S(sx, sy, px, py) = | D E F | |  0 sy dy | = | D*sx E*sy D*dx+E*dy+F |
216  *                                         | G H I | |  0  0  1 |   | G*sx H*sy G*dx+H*dy+I |
217  *
218  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
219  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
220  * @param sx Horizontal scale factor.
221  * @param sy Vertical scale factor.
222  * @param px Pivot on x-axis.
223  * @param py Pivot on y-axis.
224  * @since 12
225  * @version 1.0
226  */
227 void OH_Drawing_MatrixPreScale(OH_Drawing_Matrix*, float sx, float sy, float px, float py);
228 
229 /**
230  * @brief Sets forward matrix to translate by dx and dy.
231  *        Given:
232  *                     | A B C |               | 1 0 dx |
233  *            Matrix = | D E F |,  T(dx, dy) = | 0 1 dy |
234  *                     | G H I |               | 0 0  1 |
235  *        sets Matrix to:
236  *                                 | A B C | | 1 0 dx |   | A B A*dx+B*dy+C |
237  *            Matrix * T(dx, dy) = | D E F | | 0 1 dy | = | D E D*dx+E*dy+F |
238  *                                 | G H I | | 0 0  1 |   | G H G*dx+H*dy+I |
239  *
240  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
241  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
242  * @param dx Indicates the horizontal translation.
243  * @param dy Indicates the vertical translation.
244  * @since 12
245  * @version 1.0
246  */
247 void OH_Drawing_MatrixPreTranslate(OH_Drawing_Matrix*, float dx, float dy);
248 
249 /**
250  * @brief Sets matrix to matrix constructed from rotating by degrees about pivot point(px, py),
251  * multiplied by matrix, positive degrees rotates clockwise.
252  *        Given:
253  *
254  *                     | J K L |                        | c -s dx |
255  *            Matrix = | M N O |,  R(degrees, px, py) = | s  c dy |
256  *                     | P Q R |                        | 0  0  1 |
257  *
258  *        where:
259  *
260  *            c  = cos(degrees)
261  *            s  = sin(degrees)
262  *            dx =  s * py + (1 - c) * px
263  *            dy = -s * px + (1 - c) * py
264  *
265  *        sets Matrix to:
266  *
267  *                                          |c -s dx| |J K L|   |cJ-sM+dx*P cK-sN+dx*Q cL-sO+dx+R|
268  *            R(degrees, px, py) * Matrix = |s  c dy| |M N O| = |sJ+cM+dy*P sK+cN+dy*Q sL+cO+dy*R|
269  *                                          |0  0  1| |P Q R|   |         P          Q          R|
270  *
271  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
272  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
273  * @param degree Indicates the angle of axes relative to upright axes.
274  * @param px Indicates the pivot on x-axis.
275  * @param py Indicates the pivot on y-axis.
276  * @since 12
277  * @version 1.0
278  */
279 void OH_Drawing_MatrixPostRotate(OH_Drawing_Matrix*, float degree, float px, float py);
280 
281 /**
282  * @brief Sets matrix to backward scale by sx and sy, about a pivot point at (px, py).
283  *        Given:
284  *                     | J K L |                       | sx  0 dx |
285  *            Matrix = | M N O |,  S(sx, sy, px, py) = |  0 sy dy |
286  *                     | P Q R |                       |  0  0  1 |
287  *        where:
288  *            dx = px - sx * px
289  *            dy = py - sy * py
290  *        sets Matrix to:
291  *                                         | sx  0 dx | | J K L |   | sx*J+dx*P sx*K+dx*Q sx*L+dx+R |
292  *            S(sx, sy, px, py) * Matrix = |  0 sy dy | | M N O | = | sy*M+dy*P sy*N+dy*Q sy*O+dy*R |
293  *                                         |  0  0  1 | | P Q R |   |         P         Q         R |
294  *
295  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
296  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
297  * @param sx Horizontal scale factor.
298  * @param sy Vertical scale factor.
299  * @param px Pivot on x-axis.
300  * @param py Pivot on y-axis.
301  * @since 12
302  * @version 1.0
303  */
304 void OH_Drawing_MatrixPostScale(OH_Drawing_Matrix*, float sx, float sy, float px, float py);
305 
306 /**
307  * @brief Sets backward matrix to translate by (dx, dy).
308  *        Given:
309  *
310  *                     | J K L |               | 1 0 dx |
311  *            Matrix = | M N O |,  T(dx, dy) = | 0 1 dy |
312  *                     | P Q R |               | 0 0  1 |
313  *
314  *        sets Matrix to:
315  *
316  *                                 | 1 0 dx | | J K L |   | J+dx*P K+dx*Q L+dx*R |
317  *            T(dx, dy) * Matrix = | 0 1 dy | | M N O | = | M+dy*P N+dy*Q O+dy*R |
318  *                                 | 0 0  1 | | P Q R |   |      P      Q      R |
319  *
320  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
321  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
322  * @param dx Indicates the horizontal translation.
323  * @param dy Indicates the vertical translation.
324  * @since 12
325  * @version 1.0
326  */
327 void OH_Drawing_MatrixPostTranslate(OH_Drawing_Matrix*, float dx, float dy);
328 
329 /**
330  * @brief Reset matrix to identity, which has no effect on mapped point, sets matrix to:
331  *        | 1 0 0 |
332  *        | 0 1 0 |
333  *        | 0 0 1 |
334  *
335  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
336  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
337  * @since 12
338  * @version 1.0
339  */
340 void OH_Drawing_MatrixReset(OH_Drawing_Matrix*);
341 
342 /**
343  * @brief Sets matrix total to matrix a multiplied by matrix b.
344  *       Given:
345  *                    | A B C |          | J K L |
346  *                a = | D E F |,     b = | M N O |
347  *                    | G H I |          | P Q R |
348  *       sets Matrix total to:
349  *                            | A B C |   | J K L |   | AJ+BM+CP AK+BN+CQ AL+BO+CR |
350  *           total = a * b =  | D E F | * | M N O | = | DJ+EM+FP DK+EN+FQ DL+EO+FR |
351  *                            | G H I |   | P Q R |   | GJ+HM+IP GK+HN+IQ GL+HO+IR |
352  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
353  * @param total Indicates the pointer to an <b>OH_Drawing_Matrix</b> object that a * b.
354  * @param a Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
355  * @param b Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
356  * @since 12
357  * @version 1.0
358  */
359 void OH_Drawing_MatrixConcat(OH_Drawing_Matrix* total, const OH_Drawing_Matrix* a,
360     const OH_Drawing_Matrix* b);
361 
362 /**
363  * @brief Gets nine matrix values contained by matrix into array.
364  *
365  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
366  * @param matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
367  * @param value Storages for nine matrix values.
368  * @return Returns the error code.
369  *         Returns {@link OH_DRAWING_SUCCESS} if the operation is successful.
370  *         Returns {@link OH_DRAWING_ERROR_INVALID_PARAMETER} if matrix or value is nullptr.
371  * @since 12
372  * @version 1.0
373  */
374 OH_Drawing_ErrorCode OH_Drawing_MatrixGetAll(OH_Drawing_Matrix* matrix, float value[9]);
375 
376 /**
377  * @brief Get one matrix value. Index is between the range of 0-8.
378  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
379  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
380  * @param index one of 0-8.
381  * @return Returns value corresponding to index.Returns 0 if out of range.
382  * @since 12
383  * @version 1.0
384  */
385 float OH_Drawing_MatrixGetValue(OH_Drawing_Matrix*, int index);
386 
387 /**
388  * @brief Sets matrix to rotate by degrees about a pivot point at (px, py). The pivot point is unchanged
389  * when mapped with matrix. Positive degrees rotates clockwise.
390  *
391  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
392  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
393  * @param degree Indicates the angle of axes relative to upright axes.
394  * @param px Indicates the pivot on x-axis.
395  * @param py Indicates the pivot on y-axis.
396  * @since 12
397  * @version 1.0
398  */
399 void OH_Drawing_MatrixRotate(OH_Drawing_Matrix*, float degree, float px, float py);
400 
401 /**
402  * @brief Sets matrix to translate by (dx, dy)
403  *
404  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
405  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
406  * @param dx Indicates the horizontal translation.
407  * @param dy Indicates the vertical translation.
408  * @since 12
409  * @version 1.0
410  */
411 void OH_Drawing_MatrixTranslate(OH_Drawing_Matrix*, float dx, float dy);
412 
413 /**
414  * @brief Sets matrix to scale by sx and sy, about a pivot point at (px, py).
415  *
416  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
417  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
418  * @param sx Indicates the horizontal scale factor.
419  * @param sy Indicates the vertical scale factor.
420  * @param px Indicates the pivot on x-axis.
421  * @param py Indicates the pivot on y-axis.
422  * @since 12
423  * @version 1.0
424  */
425 void OH_Drawing_MatrixScale(OH_Drawing_Matrix*, float sx, float sy, float px, float py);
426 
427 /**
428  * @brief Sets inverse to reciprocal matrix, returning true if matrix can be inverted.
429  *
430  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
431  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
432  * @param inverse Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
433  * @return Returns true if the matrix is not nullptr and can be inverted;
434  *         returns false if the matrix is nullptr or cannot be inverted.
435  * @since 12
436  * @version 1.0
437  */
438 bool OH_Drawing_MatrixInvert(OH_Drawing_Matrix*, OH_Drawing_Matrix* inverse);
439 
440 /**
441  * @brief Sets the params of matrix to map src to dst.
442  * Count must greater than or equal to zero, and less than or equal to four.
443  *
444  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
445  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
446  * @param src Points to map from.
447  * @param dst Points to map to.
448  * @param count Number of point in src and dst.
449  * @return Returns true if matrix is constructed successfully.
450  * @since 12
451  * @version 1.0
452  */
453 bool OH_Drawing_MatrixSetPolyToPoly(OH_Drawing_Matrix*, const OH_Drawing_Point2D* src,
454     const OH_Drawing_Point2D* dst, uint32_t count);
455 
456 /**
457  * @brief Maps the src point array to the dst point array by matrix transformation.
458  *
459  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
460  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
461  * @param src Points to map from.
462  * @param dst Points to map to.
463  * @param count Number of point in src and dst.
464  * @since 12
465  * @version 1.0
466  */
467 void OH_Drawing_MatrixMapPoints(const OH_Drawing_Matrix*, const OH_Drawing_Point2D* src,
468     OH_Drawing_Point2D* dst, int count);
469 
470 /**
471  * @brief Sets dst to bounds of src corners mapped by matrix transformation.
472  *
473  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
474  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
475  * @param src Rect to map from.
476  * @param dst Rect to map to.
477  * @return Returns true if the mapped src is equal to the dst; returns false is not equal.
478  * @since 12
479  * @version 1.0
480  */
481 bool OH_Drawing_MatrixMapRect(const OH_Drawing_Matrix*, const OH_Drawing_Rect* src, OH_Drawing_Rect* dst);
482 
483 /**
484  * @brief Returns true if the first matrix equals the second matrix.
485  *
486  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
487  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
488  * @param other Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
489  * @return Returns true if the two matrices are equal; returns false if not equal.
490  * @since 12
491  * @version 1.0
492  */
493 bool OH_Drawing_MatrixIsEqual(OH_Drawing_Matrix*, OH_Drawing_Matrix* other);
494 
495 /**
496  * @brief Returns true if matrix is identity.
497  * Identity matrix is :  | 1 0 0 |
498  *                       | 0 1 0 |
499  *                       | 0 0 1 |
500  *
501  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
502  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
503  * @return Returns true if matrix is identity; returns false if not identity.
504  * @since 12
505  * @version 1.0
506  */
507 bool OH_Drawing_MatrixIsIdentity(OH_Drawing_Matrix*);
508 
509 /**
510  * @brief Destroys an <b>OH_Drawing_Matrix</b> object and reclaims the memory occupied by the object.
511  *
512  * @syscap SystemCapability.Graphic.Graphic2D.NativeDrawing
513  * @param OH_Drawing_Matrix Indicates the pointer to an <b>OH_Drawing_Matrix</b> object.
514  * @since 11
515  * @version 1.0
516  */
517 void OH_Drawing_MatrixDestroy(OH_Drawing_Matrix*);
518 
519 #ifdef __cplusplus
520 }
521 #endif
522 /** @} */
523 #endif
524