ARCS6 AR6-REV.24062600
読み取り中…
検索中…
一致する文字列を見つけられません
FrameGraphics.hh
[詳解]
1
18//
19// Copyright (C) 2011-2024 Yokokura, Yuki
20// MIT License. For details, see the LICENSE file.
21
22#ifndef FRAMEGRAPHICS
23#define FRAMEGRAPHICS
24
25#include <linux/fb.h>
26#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <png.h>
32#include <cassert>
33#include <cstdint>
34#include <array>
35#include <string>
36#include <cstring>
37#include <cmath>
38#include "FrameFontSmall.hh"
39
40// ARCS組込み用マクロ
41#ifdef ARCS_IN
42 // ARCSに組み込まれる場合
43 #include "ARCSassert.hh"
44 #include "ARCSeventlog.hh"
45#else
46 // ARCSに組み込まれない場合
47 #define arcs_assert(a) (assert(a))
48 #define PassedLog()
49 #define EventLog(a)
50 #define EventLogVar(a)
51#endif
52
53// 下記の警告の抑制のためのプラグマ (-Wno-error=strict-overflow と同値)
54// "warning: assuming signed overflow does not occur when assuming that (X - c) > X is always false"
55#pragma GCC diagnostic ignored "-Wstrict-overflow"
56
57namespace ARCS { // ARCS名前空間
58
60enum class FGcolors {
61 RED,
62 GREEN,
63 BLUE,
64 CYAN,
65 MAGENTA,
66 YELLOW,
67 ORANGE,
68 WHITE,
69 GRAY75,
70 GRAY50,
71 GRAY25,
72 BLACK,
73 ALPHA
74};
75
77enum class FGsize {
78 PX_1,
79 PX_2,
80 PX_3
81};
82
84enum class FGalign {
88};
89
92 public:
94 FrameGraphics(int Width, int Height) :
95 Frame(nullptr),
96 Screen(nullptr),
97 Background(nullptr),
98 width(Width),
99 height(Height),
100 depth(32), // [bit] 色深度は32bitに固定
101 length((size_t)width*(size_t)height), // [-] 画像配列の長さを計算
102 size(length*(size_t)(32/8)), // [bytes] 1画面データの大きさを計算
103 fbfd(0),
104 finfo(),
105 vinfo(),
106 xofst(0),
107 yofst(0),
108 xlen(0),
109 bppx(0),
110 IsFontDataLocked(true),
111 FontPrepared()
112 {
113 Screen = (uint32_t*) malloc(length*sizeof(uint32_t)); // 画面バッファ用配列
114 Background = (uint32_t*) malloc(length*sizeof(uint32_t)); // 背景バッファ用配列
115 ClearScreen(); // 画面バッファのクリア
116 ClearBackground(); // 背景バッファのクリア
117 PrepareFontData(FGcolors::WHITE, FGcolors::BLACK); // フォントデータの準備(初期値は白文字に黒背景)
118 }
119
121 explicit FrameGraphics(const std::string& DeviceName) :
122 Frame(nullptr),
123 Screen(nullptr),
124 Background(nullptr),
125 width(0),
126 height(0),
127 depth(0),
128 length(0),
129 size(0),
130 fbfd(0),
131 finfo(),
132 vinfo(),
133 xofst(0),
134 yofst(0),
135 xlen(0),
136 bppx(0),
137 IsFontDataLocked(true),
138 FontPrepared()
139 {
140 // フレームバッファデバイスを開く
141 fbfd = open(DeviceName.c_str(), O_RDWR, 0);
142 if(fbfd != -1){
143 // フレームバッファが開けたとき
144 // 固定画面情報の取得
145 ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo);
146 xlen = finfo.line_length;
147
148 // 変動画面情報の取得
149 ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo);
150 width = xlen/4; // padding対策のため width = vinfo.xres は使用しない
151 height = vinfo.yres;
152 depth = vinfo.bits_per_pixel;
153 xofst = vinfo.xoffset;
154 yofst = vinfo.yoffset;
155 bppx = vinfo.bits_per_pixel;
156 length = (size_t)width*(size_t)height; // フレームバッファの長さを計算
157 size = length*(size_t)depth/8; // 1画面データの大きさを計算 [bytes]
158 arcs_assert(depth == 32 && "[ERROR] FrameGraphics : Not supported color depth of display settings.");
159
160 Frame = (uint32_t*) mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fbfd, 0);// フレームバッファをマッピング
161 arcs_assert(Frame != MAP_FAILED && "[ERROR] FrameGraphics : mmap(...)"); // マッピングできないとき
162 }else{
163 // フレームバッファが開けなかったとき
164 EventLog("Could not open the frame buffer device. Trying to use a dummy buffer.");
165 xlen = 1920; // とりあえず画面サイズをFullHDに設定
166 width = 1920;
167 height = 1080;
168 depth = 32;
169 xofst = 0;
170 yofst = 0;
171 bppx = 32;
172 length = (size_t)width*(size_t)height; // フレームバッファの長さを計算
173 size = length*(size_t)depth/8; // 1画面データの大きさを計算 [bytes]
174 Frame = (uint32_t*) malloc(length*sizeof(uint32_t)); // ダミーフレームバッファ配列
175 }
176
177 Screen = (uint32_t*) malloc(length*sizeof(uint32_t)); // 画面バッファ用配列
178 Background = (uint32_t*) malloc(length*sizeof(uint32_t)); // 背景画面バッファ用配列
179 ClearScreen(); // 画面バッファのクリア
180 ClearBackground(); // 背景バッファのクリア
181 PrepareFontData(FGcolors::WHITE, FGcolors::BLACK); // フォントデータの準備(初期値は白文字に黒背景)
182 }
183
186 free(Background); Background = nullptr;
187 free(Screen); Screen = nullptr;
188 if(fbfd != -1){
189 // フレームバッファが開けたとき
190 munmap(Frame, size); Frame = nullptr;
191 close(fbfd); fbfd = 0;
192 }else{
193 // フレームバッファが開けなかったとき
194 free(Frame); Frame = nullptr;
195 }
196 }
197
200 void SavePngImageFile(const std::string& FileName){
201 // 初期化処理
202 FILE *fp; // PNGファイルポインタ
203 png_structp png_ptr; // PNG画像ポインタ
204 png_infop info_ptr; // PNG情報ポインタ
205 fp = fopen(FileName.c_str(), "wb"); // ファイルを開く
206 arcs_assert(fp != nullptr); // ファイルが開けるかチェック
207 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr); // png_ptr構造体を確保・初期化
208 info_ptr = png_create_info_struct(png_ptr); // info_ptr構造体を確保・初期化
209 png_init_io(png_ptr, fp); // libpngにファイルポインタを知らせる
210 png_set_IHDR( // IHDRチャンク情報を設定
211 png_ptr,
212 info_ptr,
213 width,
214 height,
215 8,
216 PNG_COLOR_TYPE_RGB_ALPHA,
217 PNG_INTERLACE_NONE,
218 PNG_COMPRESSION_TYPE_DEFAULT,
219 PNG_FILTER_TYPE_DEFAULT
220 );
221 png_write_info(png_ptr, info_ptr); // PNGファイルのヘッダを書き込む
222 png_set_invert_alpha(png_ptr); // αチャネルをFFで透過にする(デフォは00で透過)
223 png_set_bgr(png_ptr); // uint32_tデータの順序をABGRからARGBに変更
224
225 // 画像書き出し
226 for(size_t y = 0; y < static_cast<size_t>(height); ++y) png_write_row(png_ptr, (png_bytep)(&(Screen[width*y])));
227
228 // 終了処理
229 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
230 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
231 info_ptr = nullptr;
232 png_ptr = nullptr;
233 fclose(fp); fp = nullptr;
234 }
235
237 void RefreshFrame(void){
238 memcpy(Frame, Screen, size);
239 }
240
246 void RefreshFrame(int x, int y, int w, int h){
247 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
248 for(int j = 0; j < h; ++j){
249 // 1ラインずつフレームバッファへ書き込む
250 memcpy(Frame + i + width*j, Screen + i + width*j, sizeof(uint32_t)*w);
251 }
252 }
253
256 memcpy(Background, Screen, size);
257 }
258
264 void StoreScreenAsBackground(int x, int y, int w, int h){
265 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
266 for(int j = 0; j < h; ++j){
267 // 1ラインずつフレームバッファへ書き込む
268 memcpy(Background + i + width*j, Screen + i + width*j, sizeof(uint32_t)*w);
269 }
270 }
271
274 memcpy(Screen, Background, size);
275 }
276
282 void LoadBackgroundToScreen(int x, int y, int w, int h){
283 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
284 for(int j = 0; j < h; ++j){
285 // 1ラインずつフレームバッファへ書き込む
286 memcpy(Screen + i + width*j, Background + i + width*j, sizeof(uint32_t)*w);
287 }
288 }
289
292 memcpy(Screen, Frame, size);
293 }
294
297 void FillFrame(uint32_t ColorData){
298 std::fill(Frame, Frame + length, ColorData);
299 }
300
303 void FillScreen(uint32_t ColorData){
304 std::fill(Screen, Screen + length, ColorData);
305 }
306
309 void FillBackground(uint32_t ColorData){
310 std::fill(Background, Background + length, ColorData);
311 }
312
314 void ClearFrame(void){
315 FillFrame(0);
316 }
317
319 void ClearScreen(void){
320 FillScreen(0);
321 }
322
324 void ClearBackground(void){
326 }
327
333 template <FGsize T = FGsize::PX_1>
334 void DrawPoint(int x, int y, uint32_t ColorData){
335 // 点の太さに従ってコンパイル時条件分岐
336 switch(T){
337 case FGsize::PX_1: // 1[px] の場合
338 DrawDot(x, y, ColorData);
339 break;
340 case FGsize::PX_2: // 2[px] の場合
341 DrawDot(x , y , ColorData);
342 DrawDot(x , y + 1, ColorData);
343 DrawDot(x + 1, y , ColorData);
344 DrawDot(x + 1, y + 1, ColorData);
345 break;
346 case FGsize::PX_3: // 3[px] の場合
347 DrawDot(x , y , ColorData);
348 DrawDot(x , y + 1, ColorData);
349 DrawDot(x + 1, y , ColorData);
350 DrawDot(x + 1, y + 1, ColorData);
351 DrawDot(x , y - 1, ColorData);
352 DrawDot(x - 1, y , ColorData);
353 DrawDot(x - 1, y - 1, ColorData);
354 DrawDot(x + 1, y - 1, ColorData);
355 DrawDot(x - 1, y + 1, ColorData);
356 break;
357 default:
358 arcs_assert(false); // ここには来ない
359 break;
360 }
361 }
362
368 template <FGsize T = FGsize::PX_1>
369 void DrawPoint(int x, int y, FGcolors color){
370 DrawPoint<T>(x, y, ColorNameToData(color)); // バイナリ色データに変換して点描画関数を呼び出し
371 }
372
378 template <FGsize T = FGsize::PX_1>
379 void DrawPoint(int x, int y, double r, double g, double b){
380 DrawPoint<T>(x, y, RGBcolorToData(r,g,b)); // バイナリ色データに変換して点描画関数を呼び出し
381 }
382
388 template <FGsize T = FGsize::PX_1>
389 void DrawPoint(int x, int y, double a, double r, double g, double b){
390 DrawPoint<T>(x, y, ARGBcolorToData(a,r,g,b)); // バイナリ色データに変換して点描画関数を呼び出し
391 }
392
397 void DrawCross(int x, int y, uint32_t ColorData){
398 DrawDot(x + 2, y, ColorData);
399 DrawDot(x - 2, y, ColorData);
400 DrawDot(x + 1, y, ColorData);
401 DrawDot(x - 1, y, ColorData);
402 DrawDot(x, y , ColorData);
403 DrawDot(x, y + 1, ColorData);
404 DrawDot(x, y - 1, ColorData);
405 DrawDot(x, y + 2, ColorData);
406 DrawDot(x, y - 2, ColorData);
407 }
408
413 void DrawCross(int x, int y, FGcolors color){
414 DrawCross(x, y, ColorNameToData(color));
415 }
416
421 void DrawCross(int x, int y, double r, double g, double b){
422 DrawCross(x, y, RGBcolorToData(r, g ,b));
423 }
424
430 template <FGsize T = FGsize::PX_1>
431 void DrawLine(int x1, int y1, int x2, int y2, uint32_t ColorData){
432 // 垂直の線のとき(ブレゼンハムのアルゴリズムでは描けないので,別に実装)
433 if(x1 == x2){
434 DrawVerticalLine<T>(x1, y1, y2, ColorData);
435 return;
436 }
437
438 // 水平の線のとき(ブレゼンハムのアルゴリズムでも描けるが,高速化のために別に実装)
439 if( y1 == y2 ){
440 DrawHorizontalLine<T>(x1, x2, y1, ColorData);
441 return;
442 }
443
444 // 以下はブレゼンハムのアルゴリズム
445 bool IsHighGradient = std::abs(x2 - x1) < std::abs(y2 - y1); // 直線の傾きが45度を境に急勾配かどうかのフラグ
446 if(IsHighGradient == true){ // 45度を境に急勾配のとき
447 std::swap(x1, y1); // x1とy1を入れ替え
448 std::swap(x2, y2); // x2とy2を入れ替え
449 }
450 if(x2 < x1){ // 始点の方が終点よりも右のとき
451 std::swap(x1, x2);
452 std::swap(y1, y2);
453 }
454 int Dx = x2 - x1;
455 int Dy = std::abs(y2 - y1);
456 int Error = Dx/2;
457 int ystep;
458 if(y1 < y2){
459 ystep = 1;
460 }else{
461 ystep = -1;
462 }
463 int y = y1;
464 for(int x = x1; x <= x2; ++x){
465 if(IsHighGradient == true){
466 DrawPoint<T>(y, x, ColorData);
467 }else{
468 DrawPoint<T>(x, y, ColorData);
469 }
470 Error -= Dy;
471 if(Error < 0){
472 y += ystep;
473 Error += Dx;
474 }
475 }
476 }
477
483 template <FGsize T = FGsize::PX_1>
484 void DrawVerticalLine(int x, int y1, int y2, uint32_t ColorData){
485 int ysta, yend;
486 if(y1 < y2){
487 // y1→y2で増加する場合
488 ysta = y1;
489 yend = y2;
490 }else{
491 // y2→y1で増加する場合
492 ysta = y2;
493 yend = y1;
494 }
495 for(int y = ysta; y <= yend;y ++)
496 DrawPoint<T>(x, y, ColorData); // 直線に沿って点を描画
497 }
498
504 template <FGsize T = FGsize::PX_1>
505 void DrawHorizontalLine(int x1, int x2, int y, uint32_t ColorData){
506 int xsta, xend;
507 if(x1 < x2){
508 // x1→x2で増加する場合
509 xsta = x1;
510 xend = x2;
511 }else{
512 // x2→x1で増加する場合
513 xsta = x2;
514 xend = x1;
515 }
516 for(int x = xsta; x <= xend; x++)
517 DrawPoint<T>(x, y, ColorData); // 直線に沿って点を描画
518 }
519
525 template <FGsize T = FGsize::PX_1>
526 void DrawLine(int x1, int y1, int x2, int y2, FGcolors color){
527 DrawLine<T>(x1, y1, x2, y2, ColorNameToData(color));
528 }
529
535 template <FGsize T = FGsize::PX_1>
536 void DrawLine(int x1, int y1, int x2, int y2, double r, double g, double b){
537 DrawLine<T>(x1, y1, x2, y2, RGBcolorToData(r,g,b));
538 }
539
545 template <FGsize T = FGsize::PX_1>
546 void DrawStairs(int x1, int y1, int x2, int y2, uint32_t ColorData){
547 DrawLine<T>(x1, y1, x2, y1, ColorData);
548 DrawLine<T>(x2, y1, x2, y2, ColorData);
549 }
550
556 template <FGsize T = FGsize::PX_1>
557 void DrawStairs(int x1, int y1, int x2, int y2, FGcolors color){
558 DrawStairs<T>(x1, y1, x2, y2, ColorNameToData(color));
559 }
560
566 template <FGsize T = FGsize::PX_1>
567 void DrawStairs(int x1, int y1, int x2, int y2, double r, double g, double b){
568 DrawStairs<T>(x1, y1, x2, y2, RGBcolorToData(r,g,b));
569 }
570
576 template <FGsize T = FGsize::PX_1>
577 void DrawRect(int x, int y, int w, int h, uint32_t ColorData){
578 DrawHorizontalLine<T>(x , x+w, y , ColorData);
579 DrawHorizontalLine<T>(x+w, x , y+h, ColorData);
580 DrawVerticalLine<T>(x+w, y , y+h, ColorData);
581 DrawVerticalLine<T>(x , y+h, y , ColorData);
582 }
583
589 template <FGsize T = FGsize::PX_1>
590 void DrawRect(int x, int y, int w, int h, FGcolors color){
591 DrawRect<T>(x, y, w, h, ColorNameToData(color));
592 }
593
599 template <FGsize T = FGsize::PX_1>
600 void DrawRect(int x, int y, int w, int h, double r, double g, double b){
601 DrawRect<T>(x, y, w, h, RGBcolorToData(r,g,b));
602 }
603
608 void DrawRectFill(int x, int y, int w, int h, uint32_t ColorData){
609 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
610 for(int j = 0; j < h; ++j){
611 // バイナリ色データを1ラインずつフレームバッファへ書き込む
612 size_t sta = i + width*j;
613 size_t end = sta + w;
614 if(end < length) std::fill(Screen + sta, Screen + end, ColorData);
615 }
616 }
617
622 void DrawRectFill(int x, int y, int w, int h, FGcolors color){
623 DrawRectFill(x, y, w, h, ColorNameToData(color));
624 }
625
630 void DrawRectFill(int x, int y, int w, int h, double r, double g, double b){
631 DrawRectFill(x, y, w, h, RGBcolorToData(r,g,b));
632 }
633
638 void DrawRectFill(int x, int y, int w, int h, double r, double g, double b, double a){
639 DrawRectFill(x, y, w, h, ARGBcolorToData(a,r,g,b));
640 }
641
646 void DrawCircle(int cx, int cy, int radius, unsigned int N, uint32_t ColorData){
647 // 気が向いたらブレゼンハム・ミッチェナーのアルゴリズムに変更する予定
648 const double TwoPIdivN = 2.0*M_PI/(double)N;
649 int x_prev = (double)radius + cx;
650 int y_prev = cy;
651 for(unsigned int i=0;i<=N;i++){
652 double theta = TwoPIdivN*(double)i;
653 int x = (double)radius*cos(theta) + cx;
654 int y = (double)radius*sin(theta) + cy;
655 DrawLine(x_prev,y_prev, x,y, ColorData);
656 x_prev = x;
657 y_prev = y;
658 }
659 }
660
665 void DrawCircle(int cx, int cy, int radius, unsigned int N, FGcolors color){
666 DrawCircle(cx, cy, radius, N, ColorNameToData(color));
667 }
668
673 void DrawCircle(int cx, int cy, int radius, unsigned int N, double r, double g, double b){
674 DrawCircle(cx, cy, radius, N, RGBcolorToData(r,g,b));
675 }
676
678 void PrepareFontData(FGcolors fore_color, FGcolors back_color){
679 // この関数は結構遅いはずなので注意。アホっぽいので設計から見直す必要あり。
680 unsigned int i,j,k;
681 IsFontDataLocked = true; // フォントデータスピンロック
682 // フォントデータの中身を書き換え
683 for(i=0;i<FrameFontSmall::NUM;i++){
684 for(j=0;j<FrameFontSmall::HEIGHT;j++){
685 for(k=0;k<FrameFontSmall::WIDTH;k++){
686 if(FrameFontSmall::DATA[i][j][k] == 1){
687 FontPrepared[i][j][k] = ColorNameToData(fore_color); // 前景色
688 }else{
689 FontPrepared[i][j][k] = ColorNameToData(back_color); // 背景色
690 }
691 }
692 }
693 }
694 IsFontDataLocked = false; // フォントデータスピンロック解除
695 }
696
699 void PrintText(int x, int y, FGalign align, std::string text){
700 // 文字列の揃え指定に従って座標にオフセットを持たせる
701 switch(align){
702 case FGalign::ALIGN_LEFT:
703 // 左揃えのときはなにもしない
704 break;
705 case FGalign::ALIGN_CENTER:
706 // 中央揃えのときは横位置から文字列の長さの半分を引く
707 x = x - text.length()*(FrameFontSmall::WIDTH + TEXT_INTERVAL)/2;
708 break;
709 case FGalign::ALIGN_RIGHT:
710 // 右揃えのときは横位置から文字列の長さを引く
711 x = x - text.length()*(FrameFontSmall::WIDTH + TEXT_INTERVAL);
712 break;
713 }
714
715 // 1文字ずつ描画
716 for(size_t i = 0; i < text.length();i ++){
717 WriteFont(x + i*(FrameFontSmall::WIDTH + TEXT_INTERVAL), y, (unsigned int)text.at(i));
718 }
719 }
720
724 void PrintValue(int x, int y, FGalign align, std::string format, double val){
725 char str[TEXT_MAXLEN] = {'\0'};
726 sprintf(str, format.c_str(), val);
727 PrintText(x, y, align, str);
728 }
729
731 void DrawTestPattern(void){
732 DrawRect(4*width/6, 1*height/6, width/6, height/6, FGcolors::WHITE);
733 DrawRectFill(3*width/6, 2*height/6, width/6, height/6, FGcolors::RED);
734 DrawRectFill(4*width/6, 2*height/6, width/6, height/6, FGcolors::GREEN);
735 DrawRectFill(5*width/6, 2*height/6, width/6, height/6, FGcolors::BLUE);
736 DrawRectFill(12*width/2/12, 0, width/2/12, height/6, FGcolors::RED);
737 DrawRectFill(13*width/2/12, 0, width/2/12, height/6, FGcolors::GREEN);
738 DrawRectFill(14*width/2/12, 0, width/2/12, height/6, FGcolors::BLUE);
739 DrawRectFill(15*width/2/12, 0, width/2/12, height/6, FGcolors::CYAN);
740 DrawRectFill(16*width/2/12, 0, width/2/12, height/6, FGcolors::MAGENTA);
741 DrawRectFill(17*width/2/12, 0, width/2/12, height/6, FGcolors::YELLOW);
742 DrawRectFill(18*width/2/12, 0, width/2/12, height/6, FGcolors::ORANGE);
743 DrawRectFill(19*width/2/12, 0, width/2/12, height/6, FGcolors::WHITE);
744 DrawRectFill(20*width/2/12, 0, width/2/12, height/6, FGcolors::GRAY75);
745 DrawRectFill(21*width/2/12, 0, width/2/12, height/6, FGcolors::GRAY50);
746 DrawRectFill(22*width/2/12, 0, width/2/12, height/6, FGcolors::GRAY25);
747 DrawRectFill(23*width/2/12, 0, width/2/12, height/6, FGcolors::BLACK);
748
749 size_t N = width/2;
750 for(size_t i = 0; i < N; ++i){
751 double beta = (double)i/(double)N;
752 DrawLine(i+N, 3*height/6, i+N, 4*height/6, beta, 0, 0);
753 DrawLine(i+N, 4*height/6, i+N, 5*height/6, 0, beta, 0);
754 DrawLine(i+N, 5*height/6, i+N, 6*height/6, 0, 0, beta);
755 DrawLine(i, 3*height/6, i, 4*height/6, beta, beta, beta);
756 }
757
758 N = width/2/3;
759 for(size_t i = 0; i < N; ++i){
760 double beta = (double)i/(double)N;
761 DrawLine(i, 4*height/6, i, height - 1, beta, 0, 1.0 - beta);
762 DrawLine(i + N, 4*height/6, i + N, height - 1, 1.0 - beta, beta, 0 );
763 DrawLine(i + 2*N, 4*height/6, i + 2*N, height - 1, 0, 1.0 - beta, beta );
764 }
765
766 DrawCircle(3*width/4, height/4, height/6, 100, FGcolors::GRAY50);
767
768 DrawLine(width/2, height/2, width - 1, 0, FGcolors::GRAY75);
769 DrawLine(width/2, 0, width - 1, height/2, FGcolors::GRAY75);
770 DrawLine(0, height/2, width - 1, height/2, FGcolors::WHITE);
771 DrawLine(width/2, 0, width/2, height - 1, FGcolors::WHITE);
772
774 PrintValue(10, 20, FGalign::ALIGN_LEFT, "WIDTH = %15.0f [px]", width);
775 PrintValue(10, 30, FGalign::ALIGN_LEFT, "HEIGHT = %15.0f [px]", height);
776 PrintValue(10, 40, FGalign::ALIGN_LEFT, "DEPTH = %15.0f [bit]", depth);
777 PrintValue(10, 50, FGalign::ALIGN_LEFT, "LENGTH = %15.0f [-]", length);
778 PrintValue(10, 60, FGalign::ALIGN_LEFT, "SIZE = %15.0f [byte]", size);
779 PrintValue(10, 70, FGalign::ALIGN_LEFT, "XOFST = %15.0f [-]", xofst);
780 PrintValue(10, 80, FGalign::ALIGN_LEFT, "YOFST = %15.0f [-]", yofst);
781 PrintValue(10, 90, FGalign::ALIGN_LEFT, "XLEN = %15.0f [-]", xlen);
782 PrintValue(10,100, FGalign::ALIGN_LEFT, "BPPX = %15.0f [bit/px]", bppx);
783 PrintText(10, 120, FGalign::ALIGN_LEFT, "FRAME GRAPHICS TEST PATTERN");
784 PrintText(10, 140, FGalign::ALIGN_LEFT, "frame graphics test pattern");
785 }
786
789 for(unsigned int i = FrameFontSmall::FST_ASCII; i <= FrameFontSmall::END_ASCII; ++i){
790 WriteFont((i - FrameFontSmall::FST_ASCII)*(FrameFontSmall::WIDTH + TEXT_INTERVAL), 0, i);
791 }
792 }
793
794
797 uint32_t ColorNameToData(FGcolors color){
798 return ColorSet[static_cast<size_t>(color)];
799 }
800
803 uint32_t RGBcolorToData(double Red, double Green, double Blue){
804 uint32_t r = 0, g = 0, b = 0;
805 r = DoubleToRedIntensity(Red); // 赤を変換
806 g = DoubleToGreenIntensity(Green); // 緑を変換
807 b = DoubleToBlueIntensity(Blue); // 青を変換
808 return r | g | b; // ORをとって返す
809 }
810
813 uint32_t ARGBcolorToData(double Alpha, double Red, double Green, double Blue){
814 uint32_t r = 0, g = 0, b = 0, a = 0;
815 a = DoubleToAlphaIntensity(Alpha); // 透過チャネルを変換
816 r = DoubleToRedIntensity(Red); // 赤を変換
817 g = DoubleToGreenIntensity(Green); // 緑を変換
818 b = DoubleToBlueIntensity(Blue); // 青を変換
819 return a | r | g | b; // ORをとって返す
820 }
821
822 private:
823 FrameGraphics(const FrameGraphics& r) = delete;
824 FrameGraphics(FrameGraphics&& r) = delete;
825 const FrameGraphics& operator=(const FrameGraphics&) = delete;
826
827 // 色の定義
828 static constexpr size_t NUM_COLOR_SET = 13;
829
836 static constexpr std::array<uint32_t, NUM_COLOR_SET> ColorSet = {
837 0x00FF0000,
838 0x0000FF00,
839 0x000000FF,
840 0x0000FFFF,
841 0x00FF00FF,
842 0x00FFFF00,
843 0x00FF8000,
844 0x00FFFFFF,
845 0x00C0C0C0,
846 0x00808080,
847 0x00404040,
848 0x00000000,
849 0xFF000000
850 };
851
852 // 共通画像情報関連
853 uint32_t *Frame;
854 uint32_t *Screen;
855 uint32_t *Background;
856 int width;
857 int height;
858 int depth;
859 size_t length;
860 size_t size;
861
862 // フレームバッファ関連
863 int fbfd;
864 struct fb_fix_screeninfo finfo;
865 struct fb_var_screeninfo vinfo;
866 int xofst;
867 int yofst;
868 int xlen;
869 int bppx;
870
871 // 文字列関連
872 static constexpr unsigned int TEXT_INTERVAL = 1;
873 static constexpr unsigned int TEXT_MAXLEN = 256;
874 bool IsFontDataLocked;
876
881 inline size_t ConvCoordinateToIndex(int x, int y){
882 return (size_t)width*(size_t)y + (size_t)x;
883 }
884
889 inline void DrawDot(int x, int y, uint32_t ColorData){
890 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
891 if(length <= i) return; // 範囲外のときは何もせず終了
892 Screen[i] = ColorData; // 画面バッファに書き込み
893 }
894
896 uint32_t DoubleToIntensity(double u){
897 if(1 < u) u = 1;
898 if(u < 0) u = 0;
899 return (uint32_t)((double)0x00FFFFFF*u);
900 }
901
903 uint32_t DoubleToRedIntensity(double u){
904 if(1 < u) u = 1;
905 if(u < 0) u = 0;
906 return ((uint32_t)((double)0x000000FF*u)) << 16;
907 }
908
910 uint32_t DoubleToGreenIntensity(double u){
911 if(1 < u) u = 1;
912 if(u < 0) u = 0;
913 return ((uint32_t)((double)0x000000FF*u)) << 8;
914 }
915
917 uint32_t DoubleToBlueIntensity(double u){
918 if(1 < u) u = 1;
919 if(u < 0) u = 0;
920 return ((uint32_t)((double)0x000000FF*u));
921 }
922
924 uint32_t DoubleToAlphaIntensity(double u){
925 if(1 < u) u = 1;
926 if(u < 0) u = 0;
927 return ((uint32_t)((double)0x000000FF*u)) << 24;
928 }
929
933 unsigned int ConvAsciiToIndex(unsigned int ascii){
934 // アスキーコードが範囲外のときは範囲外用のフォントを使用
936 ascii = FrameFontSmall::END_ASCII + 1;
937 }
938 return ascii - FrameFontSmall::FST_ASCII;
939 }
940
946 void WriteFont(int x, int y, unsigned int ascii){
947 if(IsFontDataLocked == true)return; // フォントデータが準備中のときは何もせずに抜ける
948 size_t i = ConvCoordinateToIndex(x, y); // 座標からフレームバッファの要素番号を計算
949 unsigned int index = ConvAsciiToIndex(ascii); // アスキーコードから要素番号へ変換
950 for(unsigned int j = 0; j < FrameFontSmall::HEIGHT; ++j){
951 // フォントデータを1ラインずつフレームバッファへ書き込む
952 memcpy(Screen + i + width*j, FontPrepared[index][j], FrameFontSmall::LINEBYTE32);
953 }
954 }
955
956 };
957
958}
959
960#endif
ARCS イベントログクラス
#define EventLog(a)
イベントログ用マクロ (任意メッセージ記録版)
Definition ARCSeventlog.hh:27
ARCS用ASSERTクラス
#define arcs_assert(a)
ARCS用assertマクロ a : assert条件
Definition ARCSassert.hh:17
FGcolors
色の定義
Definition FrameGraphics.hh:60
@ CYAN
水(シアン)
@ GRAY25
灰 輝度25%
@ YELLOW
黄(イエロー)
@ GRAY50
灰 輝度50%
@ GRAY75
灰 輝度75%
@ MAGENTA
紫(マゼンタ)
FGsize
点および線の太さの定義
Definition FrameGraphics.hh:77
@ PX_1
太さ1px
@ PX_3
太さ3px
@ PX_2
太さ2px
FGalign
文字列揃え位置の定義
Definition FrameGraphics.hh:84
@ ALIGN_LEFT
文字列左揃え
@ ALIGN_RIGHT
文字列右揃え
@ ALIGN_CENTER
文字列中央揃え
フレームフォント(Small版)
static constexpr unsigned int WIDTH
[px] フォントの幅
Definition FrameFontSmall.hh:25
static constexpr unsigned int LINEBYTE32
32bitフォントデータ1ラインのバイト数
Definition FrameFontSmall.hh:30
static constexpr unsigned int NUM
収録しているフォントの数
Definition FrameFontSmall.hh:24
static constexpr int DATA[NUM][HEIGHT][WIDTH]
フォントデータ (フォントが気に食わない人は各自で好きなように変えてネ)
Definition FrameFontSmall.hh:34
static constexpr unsigned int END_ASCII
フォントデータに収録している最後の文字のアスキーコード
Definition FrameFontSmall.hh:28
static constexpr unsigned int HEIGHT
[px] フォントの高さ
Definition FrameFontSmall.hh:26
static constexpr unsigned int FST_ASCII
フォントデータに収録している最初の文字のアスキーコード
Definition FrameFontSmall.hh:27
フレームグラフィックスクラス(新型テンプレート版)
Definition FrameGraphics.hh:91
void DrawStairs(int x1, int y1, int x2, int y2, FGcolors color)
階段直線(x1,y1)ー(x2,y2)を引く関数(色の名前版)
Definition FrameGraphics.hh:557
void DrawLine(int x1, int y1, int x2, int y2, double r, double g, double b)
直線(x1,y1)ー(x2,y2)を引く関数(RGB版)
Definition FrameGraphics.hh:536
void FillBackground(uint32_t ColorData)
背景バッファを指定した色で埋める関数
Definition FrameGraphics.hh:309
void DrawPoint(int x, int y, uint32_t ColorData)
点(x,y)を描画する関数(バイナリデータ版)
Definition FrameGraphics.hh:334
void DrawRect(int x, int y, int w, int h, uint32_t ColorData)
長方形の描画をする関数(バイナリ色データ版)(色は塗らない)
Definition FrameGraphics.hh:577
void DrawCircle(int cx, int cy, int radius, unsigned int N, FGcolors color)
円の描画をする(色の名前版)
Definition FrameGraphics.hh:665
void PrintAllFontData(void)
収録されているすべてのフォントデータを描画するテスト用関数
Definition FrameGraphics.hh:788
void DrawCross(int x, int y, double r, double g, double b)
十字(x,y)を描画する関数(RGB版)
Definition FrameGraphics.hh:421
void DrawHorizontalLine(int x1, int x2, int y, uint32_t ColorData)
水平の直線(x1,y)ー(x2,y)を引く関数(バイナリデータ版)
Definition FrameGraphics.hh:505
FrameGraphics(const std::string &DeviceName)
コンストラクタ(フレームバッファ版)
Definition FrameGraphics.hh:121
void DrawCross(int x, int y, uint32_t ColorData)
十字(x,y)を描画する関数(バイナリデータ版)
Definition FrameGraphics.hh:397
void ClearFrame(void)
フレームバッファをクリアする関数
Definition FrameGraphics.hh:314
void DrawRect(int x, int y, int w, int h, FGcolors color)
長方形の描画をする関数(色の名前版)(色は塗らない)
Definition FrameGraphics.hh:590
void DrawStairs(int x1, int y1, int x2, int y2, double r, double g, double b)
階段直線(x1,y1)ー(x2,y2)を引く関数(RGB版)
Definition FrameGraphics.hh:567
void DrawPoint(int x, int y, double a, double r, double g, double b)
点(x,y)を描画する関数(αRGB版)
Definition FrameGraphics.hh:389
uint32_t ARGBcolorToData(double Alpha, double Red, double Green, double Blue)
0~1の浮動小数点で表現された赤緑青透過色の輝度値をバイナリ色データに変える
Definition FrameGraphics.hh:813
void SavePngImageFile(const std::string &FileName)
PNG画像ファイルを保存する関数
Definition FrameGraphics.hh:200
void DrawStairs(int x1, int y1, int x2, int y2, uint32_t ColorData)
階段直線(x1,y1)ー(x2,y2)を引く関数(バイナリデータ版)
Definition FrameGraphics.hh:546
void DrawRectFill(int x, int y, int w, int h, FGcolors color)
長方形の範囲内に色を塗る関数(色の名前版) (色を塗る)
Definition FrameGraphics.hh:622
void DrawTestPattern(void)
テストパターンを描画する関数
Definition FrameGraphics.hh:731
void DrawCircle(int cx, int cy, int radius, unsigned int N, uint32_t ColorData)
円の描画をする関数(バイナリ色データ版)
Definition FrameGraphics.hh:646
void DrawLine(int x1, int y1, int x2, int y2, FGcolors color)
直線(x1,y1)ー(x2,y2)を引く関数(色の名前版)
Definition FrameGraphics.hh:526
void FillScreen(uint32_t ColorData)
画面バッファを指定した色で埋める関数
Definition FrameGraphics.hh:303
uint32_t ColorNameToData(FGcolors color)
色の名前からバイナリ色データに変換する関数
Definition FrameGraphics.hh:797
void RefreshFrame(int x, int y, int w, int h)
指定した矩形範囲のみフレームバッファを更新する関数
Definition FrameGraphics.hh:246
void RefreshFrame(void)
フレームバッファを更新する関数
Definition FrameGraphics.hh:237
void PrepareFontData(FGcolors fore_color, FGcolors back_color)
指定した色のフォントデータを準備する関数
Definition FrameGraphics.hh:678
void DrawPoint(int x, int y, FGcolors color)
点(x,y)を描画する関数(色の名前版)
Definition FrameGraphics.hh:369
void LoadBackgroundToScreen(void)
背景バッファを画面バッファへ読み込む関数
Definition FrameGraphics.hh:273
void DrawLine(int x1, int y1, int x2, int y2, uint32_t ColorData)
直線(x1,y1)ー(x2,y2)を引く関数(バイナリデータ版)
Definition FrameGraphics.hh:431
void DrawCross(int x, int y, FGcolors color)
十字(x,y)を描画する関数(色の名前版)
Definition FrameGraphics.hh:413
void DrawRectFill(int x, int y, int w, int h, double r, double g, double b)
長方形の範囲内に色を塗る関数(RGB色データ版) (色を塗る)
Definition FrameGraphics.hh:630
void DrawVerticalLine(int x, int y1, int y2, uint32_t ColorData)
垂直の直線(x,y1)ー(x,y2)を引く関数(バイナリデータ版)
Definition FrameGraphics.hh:484
void PrintText(int x, int y, FGalign align, std::string text)
文字列を描画する関数 x:[px]横位置,y:[px] 縦位置,align:揃え位置,text:所望の文字列
Definition FrameGraphics.hh:699
void DrawCircle(int cx, int cy, int radius, unsigned int N, double r, double g, double b)
円の描画をする(RGB色データ版)
Definition FrameGraphics.hh:673
~FrameGraphics()
デストラクタ
Definition FrameGraphics.hh:185
void ClearBackground(void)
背景バッファをクリアする関数
Definition FrameGraphics.hh:324
void LoadFrameToScreen(void)
フレームバッファから画面バッファへ読み込む関数
Definition FrameGraphics.hh:291
FrameGraphics(int Width, int Height)
コンストラクタ(PNG画像ファイル版)
Definition FrameGraphics.hh:94
void StoreScreenAsBackground(int x, int y, int w, int h)
指定した矩形範囲のみ現在の画面バッファを背景バッファとして保存する関数
Definition FrameGraphics.hh:264
void DrawRect(int x, int y, int w, int h, double r, double g, double b)
長方形の描画をする関数(RGB版)(色は塗らない)
Definition FrameGraphics.hh:600
uint32_t RGBcolorToData(double Red, double Green, double Blue)
0~1の浮動小数点で表現された赤緑青色の輝度値をバイナリ色データに変える
Definition FrameGraphics.hh:803
void FillFrame(uint32_t ColorData)
フレームバッファを指定した色で埋める関数
Definition FrameGraphics.hh:297
void DrawRectFill(int x, int y, int w, int h, double r, double g, double b, double a)
長方形の範囲内に色を塗る関数(ARGB色データ版) (色を塗る)
Definition FrameGraphics.hh:638
void LoadBackgroundToScreen(int x, int y, int w, int h)
指定した矩形範囲のみ背景バッファを画面バッファへ読み込む関数
Definition FrameGraphics.hh:282
void DrawPoint(int x, int y, double r, double g, double b)
点(x,y)を描画する関数(RGB版)
Definition FrameGraphics.hh:379
void DrawRectFill(int x, int y, int w, int h, uint32_t ColorData)
長方形の範囲内に色を塗る関数(バイナリ色データ版) (色を塗る)
Definition FrameGraphics.hh:608
void StoreScreenAsBackground(void)
現在の画面バッファを背景バッファとして保存する関数
Definition FrameGraphics.hh:255
void ClearScreen(void)
画面バッファをクリアする関数
Definition FrameGraphics.hh:319
void PrintValue(int x, int y, FGalign align, std::string format, double val)
指定した書式で文字列を描画する関数 x:[px] 横位置,y:[px] 縦位置,align:揃え位置,format:書式指定子,val:所望の数値 formatの書式指定子は printf関数 の場合の...
Definition FrameGraphics.hh:724