OSDN Git Service

Added headers.
[bluetank/bluetank.git] / soft / utils / lcdtool / main.c
1 /**
2  * @file main.c
3  * @author Copyright(C) 2012 Shinichiro Nakamura
4  */
5
6 /*
7  * ===============================================================
8  *  BlueTank
9  * ===============================================================
10  * Copyright (c) 2012 Shinichiro Nakamura
11  *
12  * Permission is hereby granted, free of charge, to any person
13  * obtaining a copy of this software and associated documentation
14  * files (the "Software"), to deal in the Software without
15  * restriction, including without limitation the rights to use,
16  * copy, modify, merge, publish, distribute, sublicense, and/or
17  * sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following
19  * conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
26  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
28  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
29  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31  * OTHER DEALINGS IN THE SOFTWARE.
32  * ===============================================================
33  */
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include "bmpimg.h"
39 #include "lcdimg.h"
40 #include "canvas.h"
41
42 int main(int argc, char **argv);
43
44 /**
45  * @brief ストリームからデータを読み込む。
46  * @details
47  * インターフェースでは、何からどのように読み込むかについて一切感知していない。
48  * この関数では、何からどのように読み込むかについて解決する。
49  *
50  * @param buf バッファへのポインタ。
51  * @param size 読み込みバイトサイズ。
52  * @param extobj ユーザが指定した拡張オブジェクト。
53  */
54 int func_fread(void *buf, const unsigned int size, void *extobj)
55 {
56     FILE *fp = (FILE *)extobj;
57     return fread(buf, size, 1, fp);
58 }
59
60 /**
61  * @brief ストリームへデータを書き込む。
62  * @details
63  * インターフェースでは、何にどのように書き込むかについて一切感知していない。
64  * この関数では、何にどのように書き込むかについて解決する。
65  *
66  * @param buf バッファへのポインタ。
67  * @param size 書き込みバイトサイズ。
68  * @param extobj ユーザが指定した拡張オブジェクト。
69  */
70 int func_fwrite(const void *buf, const unsigned int size, void *extobj)
71 {
72     FILE *fp = (FILE *)extobj;
73     return fwrite(buf, size, 1, fp);
74 }
75
76 int main(int argc, char **argv)
77 {
78     const int imgw = 240;
79     const int imgh = 110;
80     canvas_t canvas;
81     bmpimg_t bmpimg;
82     bmpcol_t col;
83     LCDIMG *lcdimg;
84     FILE *fp;
85     int i;
86
87     if (argc != 4) {
88         printf("lcdtool [file] [line1] [line2]\n");
89         exit(1);
90     }
91
92     /*
93      * 画像ピクセルを格納する領域を確保する。
94      */
95     canvas.w = imgw;
96     canvas.h = imgh;
97     canvas.buffer = (bmpcol_t *)malloc(sizeof(bmpcol_t) * imgw * imgh);
98
99     /*
100      * 開始処理。
101      *
102      * ピクセル入出力関数を渡して初期化する。
103      * ユーザが指定可能な拡張オブジェクトに、独自に規定したキャンバスを渡しておく。
104      */
105     bmpimg_open(&bmpimg, imgw, imgh, canvas_pixel_writer, &canvas, canvas_pixel_reader, &canvas);
106
107     BMPIMG_SET_COLOR(col, 124, 120, 230);
108     bmpimg_fill_box(&bmpimg, 0, 0, imgw - 1, imgh - 1, &col);
109
110     lcdimg = lcdimg_open(8, 2);
111     for (i = 0; i < strlen(argv[2]); i++) {
112         lcdimg_text(lcdimg, i, 0, argv[2][i]);
113     }
114     for (i = 0; i < strlen(argv[3]); i++) {
115         lcdimg_text(lcdimg, i, 1, argv[3][i]);
116     }
117
118     lcdimg_draw(lcdimg, canvas_drawer, &canvas);
119
120     fp = fopen(argv[1], "wb");
121     if (fp != NULL) {
122         bmpimg_bmp_write(&bmpimg, func_fwrite, fp);
123         fclose(fp);
124     }
125
126     lcdimg_close(lcdimg);
127
128     /*
129      * 終了処理。
130      */
131     bmpimg_close(&bmpimg);
132
133     /*
134      * 画像ピクセルを格納する領域を破棄する。
135      */
136     free(canvas.buffer);
137
138     return 0;
139 }
140