hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
350
351
352
353
354
355
356
357
358
359
360
/*
 * Rockchip App
 *
 * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
 * author: hogan.wang@rock-chips.com
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
#include "draw_rect.h"
 
#ifndef ABS
#define ABS(x)              (((x)<0) ? -(x) : (x))
#endif
 
static void YUV_SWAP(int *x, int *y)
{
    int tmp = 0;
 
    tmp = *x;
    *x = *y;
    *y = tmp;
}
 
YUV_Color set_yuv_color(COLOR_Type color_type)
{
    YUV_Color color;
 
    switch (color_type) {
        case COLOR_Y:
            color.Y = 226;
            color.U = 0;
            color.V = 149;
        break;
 
        case COLOR_R:
            color.Y = 76;
            color.U = 85;
            color.V = 255;
        break;
 
        case COLOR_G:
            color.Y = 150;
            color.U = 44;
            color.V = 21;
        break;
 
        case COLOR_B:
            color.Y = 29;
            color.U = 255;
            color.V = 107;
        break;
 
        case COLOR_BK:
            color.Y = 16;
            color.U = 128;
            color.V = 128;
        break;
 
        default:
            color.Y = 128;
            color.U = 128;
            color.V = 128;
        break;
    }
 
    return color;
}
 
static int yuv420_draw_point(unsigned char* imgdata,
    int width,
    int height,
    YUV_Point Point,
    YUV_Color color)
{
    int imgSize;
    int x, y;
 
    if (!imgdata)
        return -1;
 
    if (width < 0 || height < 0)
        return -1;
 
    if (Point.x < 0 || Point.x > width ||
        Point.y < 0 || Point.y > height)
        return -1;
 
    if (Point.x % 2)
        Point.x--;
    if (Point.y % 2)
        Point.y--;
 
    imgSize = width * height;
    x = Point.x;
    y = Point.y;
 
    imgdata[y * width + x] = color.Y;
    imgdata[imgSize + y * width / 2 + x] = color.U;
    imgdata[imgSize + y * width / 2 + x + 1] = color.V;
 
    return 0;
}
 
static int yuv420_draw_vline(void* imgdata,
    int width,
    int height,
    YUV_Point startPoint,
    YUV_Point endPoint,
    YUV_Color color)
{
    int ret = 0;
    int x0, y0, y1;
    YUV_Point Point;
 
    x0 = startPoint.x;
    y0 = startPoint.y;
    y1 = endPoint.y;
 
    if (y0 > y1)
        YUV_SWAP(&y0, &y1);
 
    Point.x = x0;
    Point.y = y0;
 
    while (Point.y < y1) {
        Point.y++;
        ret = yuv420_draw_point((unsigned char *)imgdata,
            width, height, Point, color);
        if (ret)
            return -1;
    }
 
    return 0;
}
 
static int yuv420_draw_hline(void* imgdata,
    int width,
    int height,
    YUV_Point startPoint,
    YUV_Point endPoint,
    YUV_Color color)
{
    int ret = 0;
    int x0, y0, x1;
    YUV_Point Point;
 
    x0 = startPoint.x;
    y0 = startPoint.y;
    x1 = endPoint.x;
 
    if (x0 > x1)
        YUV_SWAP(&x0, &x1);
 
    Point.x = x0;
    Point.y = y0;
 
    while (Point.x < x1) {
        Point.x++;
        ret = yuv420_draw_point((unsigned char *)imgdata,
            width, height, Point, color);
        if (ret)
            return -1;
    }
 
    return 0;
}
 
static int yuv420_draw_tline(void* imgdata,
    int width,
    int height,
    YUV_Point startPoint,
    YUV_Point endPoint,
    YUV_Color color)
{
    int x0, y0, x1, y1;
    int dx, dy, error;
    YUV_Point Point;
 
    x0 = startPoint.x;
    y0 = startPoint.y;
    x1 = endPoint.x;
    y1 = endPoint.y;
 
    if (x0 > x1) {
        YUV_SWAP(&x0, &x1);
        YUV_SWAP(&y0, &y1);
    }
 
    dx = x1 - x0;
    dy = y1 - y0;
    Point.x = x0;
    Point.y = y0;
 
    if (dx == 0) {
        yuv420_draw_vline(imgdata, width,
            height, startPoint, endPoint, color);
        return 0;
    }
    if (dy == 0){
        yuv420_draw_hline(imgdata, width,
            height, startPoint, endPoint, color);
        return 0;
    }
 
    yuv420_draw_point((unsigned char *)imgdata,
        width, height, Point, color);
 
    if ((dx >= dy) && (dy > 0)) {/* 0<k<=1 */
        error = (dy*2) - dx;
        while (Point.x < x1) {
            if (error > 0) {
                error += (dy - dx) * 2;
                Point.x++;
                Point.y++;
            } else {
                error += dy * 2;
                Point.x++;
            }
            yuv420_draw_point((unsigned char *)imgdata,
                width, height, Point, color);
        }
        return 0;
    }
 
    if ((dy > dx) && (dy > 0)) {/* k>1 */
        error = dy - (dx * 2);
        while (Point.y < y1) {
            if (error < 0) {
                error += (dy - dx) * 2;
                Point.x++;
                Point.y++;
            } else {
                error += (-dx) * 2;
                Point.y++;
            }
            yuv420_draw_point((unsigned char *)imgdata,
                width, height, Point, color);
        }
        return 0;
    }
 
    if ((dx >= ABS(dy)) && (dy < 0)) { /* -1=<k<0 */
        error = (dy * 2) + dx;
        while (Point.x < x1) {
            if (error < 0) {
                error += (dy + dx) * 2;
                Point.x++;
                Point.y--;
            } else {
                error += dy * 2;
                Point.x++;
            }
            yuv420_draw_point((unsigned char *)imgdata,
                width, height, Point, color);
        }
        return 0;
    }
 
    if ((ABS(dy) > dx) && (dy < 0)) { /* k<-1 */
        error = dy + (dx * 2);
        while (Point.y > y1) {
            if (error > 0) {
                error += (dy + dx) * 2;
                Point.x++;
                Point.y--;
            } else {
                error += dx * 2;
                Point.y--;
            }
            yuv420_draw_point((unsigned char *)imgdata,
                width, height, Point, color);
        }
        return 0;
    }
 
    return 0;
}
 
void yuv420_draw_line(void* imgdata,
    int width,
    int height,
    YUV_Point startPoint,
    YUV_Point endPoint,
    YUV_Color color)
{
    YUV_Point sPoint;
    YUV_Point ePoint;
 
    sPoint.x = startPoint.x - 2;
    sPoint.y = startPoint.y - 2;
    ePoint.x = endPoint.x - 2;
    ePoint.y = endPoint.y - 2;
    yuv420_draw_tline(imgdata, width, height, sPoint, ePoint, color);
 
    sPoint.x = startPoint.x;
    sPoint.y = startPoint.y;
    ePoint.x = endPoint.x;
    ePoint.y = endPoint.y;
    yuv420_draw_tline(imgdata, width, height, sPoint, ePoint, color);
 
    sPoint.x = startPoint.x + 2;
    sPoint.y = startPoint.y + 2;
    ePoint.x = endPoint.x + 2;
    ePoint.y = endPoint.y + 2;
    yuv420_draw_tline(imgdata, width, height, sPoint, ePoint, color);
}
 
void yuv420_draw_rectangle(void* imgdata,
    int width,
    int height,
    YUV_Rect rect_rio,
    YUV_Color color)
{
    int RoiWidth = rect_rio.width;
    int RoiHeight = rect_rio.height;
    int x = rect_rio.x;
    int y = rect_rio.y;
    YUV_Point  Point[4];
 
    Point[0].x = x;
    Point[0].y = y;
    Point[1].x = x + RoiWidth - 2;
    Point[1].y = y;
    Point[2].x = x + RoiWidth - 2;
    Point[2].y = y + RoiHeight - 2;
    Point[3].x = x;
    Point[3].y = y + RoiHeight - 2;
 
    yuv420_draw_line(imgdata, width, height, Point[0], Point[1], color);
    yuv420_draw_line(imgdata, width, height, Point[1], Point[2], color);
    yuv420_draw_line(imgdata, width, height, Point[3], Point[2], color);
    yuv420_draw_line(imgdata, width, height, Point[0], Point[3], color);
}