1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2022 Rockchip Electronics Co., Ltd.
3*4882a593Smuzhiyun * Authors:
4*4882a593Smuzhiyun * YuQiaowei <cerf.yu@rock-chips.com>
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
7*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
8*4882a593Smuzhiyun * You may obtain a copy of the License at
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
13*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
14*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*4882a593Smuzhiyun * See the License for the specific language governing permissions and
16*4882a593Smuzhiyun * limitations under the License.
17*4882a593Smuzhiyun */
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <iostream>
20*4882a593Smuzhiyun #include <fstream>
21*4882a593Smuzhiyun #include <sstream>
22*4882a593Smuzhiyun #include <cstddef>
23*4882a593Smuzhiyun #include <cmath>
24*4882a593Smuzhiyun #include <stdlib.h>
25*4882a593Smuzhiyun #include <string.h>
26*4882a593Smuzhiyun #include <sys/time.h>
27*4882a593Smuzhiyun #include <unistd.h>
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "RgaUtils.h"
30*4882a593Smuzhiyun
get_cur_us()31*4882a593Smuzhiyun int64_t get_cur_us() {
32*4882a593Smuzhiyun struct timeval tv;
33*4882a593Smuzhiyun gettimeofday(&tv,NULL);
34*4882a593Smuzhiyun return tv.tv_sec * 1000000 + tv.tv_usec;
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun
get_cur_ms()37*4882a593Smuzhiyun int64_t get_cur_ms() {
38*4882a593Smuzhiyun struct timeval tv;
39*4882a593Smuzhiyun gettimeofday(&tv,NULL);
40*4882a593Smuzhiyun return tv.tv_sec * 1000 + tv.tv_usec / 1000;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
draw_rgba(char * buffer,int width,int height)43*4882a593Smuzhiyun void draw_rgba(char *buffer, int width, int height) {
44*4882a593Smuzhiyun for (int i = 0; i < height; i++) {
45*4882a593Smuzhiyun for (int j = 0; j < width/4; j++) {
46*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 0] = 0xff; //R
47*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 1] = 0x00; //G
48*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 2] = 0x00; //B
49*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 3] = 0xff; //A
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun for (int j = width/4; j < width/4*2; j++) {
52*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 0] = 0x00;
53*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 1] = 0xff;
54*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 2] = 0x00;
55*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 3] = 0xff;
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun for (int j = width/4*2; j < width/4*3; j++) {
58*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 0] = 0x00;
59*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 1] = 0x00;
60*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 2] = 0xff;
61*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 3] = 0xff;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun for (int j = width/4*3; j < width; j++) {
64*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 0] = 0xff;
65*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 1] = 0xff;
66*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 2] = 0xff;
67*4882a593Smuzhiyun buffer[(i*width*4) + j*4 + 3] = 0xff;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
draw_YUV420(char * buffer,int width,int height)72*4882a593Smuzhiyun void draw_YUV420(char *buffer, int width, int height) {
73*4882a593Smuzhiyun /* Y channel */
74*4882a593Smuzhiyun memset(buffer, 0xa8, width * height / 2);
75*4882a593Smuzhiyun memset(buffer + width * height / 2, 0x54, width * height / 2);
76*4882a593Smuzhiyun /* UV channel */
77*4882a593Smuzhiyun memset(buffer + width * height, 0x80, width * height / 4);
78*4882a593Smuzhiyun memset(buffer + (int)(width * height * 1.25), 0x30, width * height / 4);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
draw_YUV422(char * buffer,int width,int height)81*4882a593Smuzhiyun void draw_YUV422(char *buffer, int width, int height) {
82*4882a593Smuzhiyun /* Y channel */
83*4882a593Smuzhiyun memset(buffer, 0xa8, width * height / 2);
84*4882a593Smuzhiyun memset(buffer + width * height / 2, 0x54, width * height / 2);
85*4882a593Smuzhiyun /* UV channel */
86*4882a593Smuzhiyun memset(buffer + width * height, 0x80, width * height / 2);
87*4882a593Smuzhiyun memset(buffer + (int)(width * height * 1.5), 0x30, width * height / 2);
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
draw_gray256(char * buffer,int width,int height)90*4882a593Smuzhiyun void draw_gray256(char *buffer, int width, int height) {
91*4882a593Smuzhiyun for (int i = 0; i < height; i++) {
92*4882a593Smuzhiyun for (int j = 0; j < width/4; j++) {
93*4882a593Smuzhiyun buffer[(i*width*4) + j*4] = 0xa8;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun for (int j = width/4; j < width/4*2; j++) {
96*4882a593Smuzhiyun buffer[(i*width*4) + j*4] = 0x80;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun for (int j = width/4*2; j < width/4*3; j++) {
99*4882a593Smuzhiyun buffer[(i*width*4) + j*4] = 0x54;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun for (int j = width/4*3; j < width; j++) {
102*4882a593Smuzhiyun buffer[(i*width*4) + j*4] = 0x30;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
read_image_from_fbc_file(void * buf,const char * path,int sw,int sh,int fmt,int index)107*4882a593Smuzhiyun int read_image_from_fbc_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
108*4882a593Smuzhiyun int size;
109*4882a593Smuzhiyun char filePath[100];
110*4882a593Smuzhiyun const char *inputFbcFilePath = "%s/in%dw%d-h%d-%s-fbc.bin";
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun snprintf(filePath, 100, inputFbcFilePath,
113*4882a593Smuzhiyun path, index, sw, sh, translate_format_str(fmt));
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun FILE *file = fopen(filePath, "rb");
116*4882a593Smuzhiyun if (!file) {
117*4882a593Smuzhiyun fprintf(stderr, "Could not open %s\n", filePath);
118*4882a593Smuzhiyun return -EINVAL;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun size = sw * sh * get_bpp_from_format(fmt) * 1.5;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun fread(buf, size, 1, file);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun fclose(file);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun return 0;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
read_image_from_file(void * buf,const char * path,int sw,int sh,int fmt,int index)130*4882a593Smuzhiyun int read_image_from_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
131*4882a593Smuzhiyun int size;
132*4882a593Smuzhiyun char filePath[100];
133*4882a593Smuzhiyun const char *inputFilePath = "%s/in%dw%d-h%d-%s.bin";
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun snprintf(filePath, 100, inputFilePath,
136*4882a593Smuzhiyun path, index, sw, sh, translate_format_str(fmt));
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun FILE *file = fopen(filePath, "rb");
139*4882a593Smuzhiyun if (!file) {
140*4882a593Smuzhiyun fprintf(stderr, "Could not open %s\n", filePath);
141*4882a593Smuzhiyun return -EINVAL;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun size = sw * sh * get_bpp_from_format(fmt);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun fread(buf, size, 1, file);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun fclose(file);
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun return 0;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun
write_image_to_fbc_file(void * buf,const char * path,int sw,int sh,int fmt,int index)153*4882a593Smuzhiyun int write_image_to_fbc_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
154*4882a593Smuzhiyun int size;
155*4882a593Smuzhiyun char filePath[100];
156*4882a593Smuzhiyun const char *outputFbcFilePath = "%s/out%dw%d-h%d-%s-fbc.bin";
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun snprintf(filePath, 100, outputFbcFilePath,
159*4882a593Smuzhiyun path, index, sw, sh, translate_format_str(fmt));
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun FILE *file = fopen(filePath, "wb+");
162*4882a593Smuzhiyun if (!file) {
163*4882a593Smuzhiyun fprintf(stderr, "Could not open %s\n", filePath);
164*4882a593Smuzhiyun return false;
165*4882a593Smuzhiyun } else {
166*4882a593Smuzhiyun fprintf(stderr, "open %s and write ok\n", filePath);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun size = sw * sh * get_bpp_from_format(fmt) * 1.5;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun fwrite(buf, size, 1, file);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun fclose(file);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return 0;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
write_image_to_file(void * buf,const char * path,int sw,int sh,int fmt,int index)178*4882a593Smuzhiyun int write_image_to_file(void *buf, const char *path, int sw, int sh, int fmt, int index) {
179*4882a593Smuzhiyun int size;
180*4882a593Smuzhiyun char filePath[100];
181*4882a593Smuzhiyun const char *outputFilePath = "%s/out%dw%d-h%d-%s.bin";
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun snprintf(filePath, 100, outputFilePath,
184*4882a593Smuzhiyun path, index, sw, sh, translate_format_str(fmt));
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun FILE *file = fopen(filePath, "wb+");
187*4882a593Smuzhiyun if (!file) {
188*4882a593Smuzhiyun fprintf(stderr, "Could not open %s\n", filePath);
189*4882a593Smuzhiyun return false;
190*4882a593Smuzhiyun } else {
191*4882a593Smuzhiyun fprintf(stderr, "open %s and write ok\n", filePath);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun size = sw * sh * get_bpp_from_format(fmt);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun fwrite(buf, size, 1, file);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun fclose(file);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun return 0;
201*4882a593Smuzhiyun }
202