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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
 * (C) Copyright 2020 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:     GPL-2.0+
 * Author: Wenping Zhang <wenping.zhang@rock-chips.com>
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rk_eink.h>
 
struct  bmp_header {
   /* Header */
   char signature[2];
   uint32_t    file_size;
   uint32_t    reserved;
   uint32_t    data_offset;
   /* InfoHeader */
   uint32_t    size;
   uint32_t    width;
   uint32_t    height;
   uint16_t    planes;
   uint16_t    bit_count;
   uint32_t    compression;
   uint32_t    image_size;
   uint32_t    x_pixels_per_m;
   uint32_t    y_pixels_per_m;
   uint32_t    colors_used;
   uint32_t    colors_important;
   /* ColorTable */
} __attribute__((packed));
 
struct bmp_image {
   struct bmp_header hdr;
   uint8_t color_table[0];
};
 
struct pixel_u16 {
   uint16_t blue  : 4,
       green : 4,
       red   : 4,
       alpha : 4;
} __attribute__((packed));
 
struct pixel_u24 {
   uint8_t blue;
   uint8_t green;
   uint8_t red;
} __attribute__((packed));
 
struct pixel_u32 {
   uint8_t blue;
   uint8_t green;
   uint8_t red;
   uint8_t alpha;
} __attribute__((packed));
 
//logo partition Header, 64byte
struct logo_part_header {
   char magic[4]; /* must be "RKEL" */
   uint32_t  totoal_size;
   uint32_t  screen_width;
   uint32_t  screen_height;
   uint32_t  logo_count;
   char  version[4];
   uint32_t  rsv[10];
} __attribute__((packed));
 
// logo image header,32 byte
struct grayscale_header {
   char magic[4]; /* must be "GR04" */
   uint16_t x;
   uint16_t y;
   uint16_t w;
   uint16_t h;
   uint32_t logo_type;
   uint32_t data_offset; /* image offset in byte */
   uint32_t data_size; /* image size in byte */
   uint32_t rsv[2];
} __attribute__((packed));
 
/*
 * The start address of logo image in logo.img must be aligned
 * in 512 bytes,so the header size must be times of 512 bytes.
 * Here we fix the size to 512 bytes, so the count of logo image
 * can only support up to 14.
 */
struct logo_info {
   struct logo_part_header part_hdr;
   struct grayscale_header img_hdr[14];
} __attribute__((packed));
 
struct input_img_info {
   char path[256];
   int logo_type;
};
 
/*
 * Every part of logo.img must be aligned in RK_BLK_SIZE,
 * use macro aligned_in_blk to calculate the the real size
 */
#define RK_BLK_SIZE 512
#define ALIGN(x, y)    (((x) + (y) - 1) & ~((y) - 1))
 
struct input_img_info in_img_info[16];
uint32_t screen_w;
uint32_t screen_h;
static const char version[4] = "1.00";
static const char *PROG;
 
static const char *fix_path(const char *path)
{
   if (!memcmp(path, "./", 2))
       return path + 2;
   return path;
}
 
static void print_version(void)
{
   printf("Version %s (zwp@rock-chips.com)\n", version);
}
 
void usage(void)
{
   printf("Usage: %s [options] [arguments]\n\n", PROG);
   print_version();
   printf("\t --uboot-logo path");
   printf("\t\t\t Pack uboot logo to logo.img from given path\n");
   printf("\t --charge-logo path");
   printf("\t\t\t Pack charge logo to logo.img from given path\n");
   printf("\t --lowpower-logo path");
   printf("\t\t\t Pack low power logo to logo.img from given path\n");
   printf("\t --kernel-logo path");
   printf("\t\t\t Pack low power logo to logo.img from given path\n");
   printf("\t --poweroff-logo path");
   printf("\t\t\t Pack power off logo to logo.img from given path\n");
   printf("\t --output path");
   printf("\t\t\t Output the grayscale image to path\n");
}
 
static inline int size_of_header(void)
{
   return ALIGN(sizeof(struct logo_info), RK_BLK_SIZE);
}
 
static inline int size_of_one_image(void)
{
   return ALIGN((screen_w * screen_h) >> 1, RK_BLK_SIZE);
}
 
int get_logo_resolution(char *in_path, uint32_t *logo_width,
           uint32_t *logo_height)
{
   struct bmp_header bmp_hdr;
   FILE *file;
   int size;
   int ret = 0;
 
   if (!in_path)
       fprintf(stderr, "Invalid bmp file path.\n");
 
   file = fopen(in_path, "rb");
   if (!file) {
       fprintf(stderr, "File %s open failed.\n", in_path);
       return -1;
   }
   size = sizeof(struct bmp_header);
   if (size != fread(&bmp_hdr, 1, size, file)) {
       fprintf(stderr, "Incomplete read of file %s.\n", in_path);
       ret = -1;
       goto out;
   }
   if (!(bmp_hdr.signature[0] == 'B' &&
         bmp_hdr.signature[1] == 'M')) {
       printf("cat not find bmp file\n");
       ret = -1;
       goto out;
   }
   *logo_width = bmp_hdr.width;
   *logo_height = bmp_hdr.height;
   fprintf(stderr, "logo resolution is %d x %d.\n",
       *logo_width, *logo_height);
out:
   fclose(file);
   return ret;
}
 
/*
 * The bmp pixel is scan from left-bottom to right-top
 */
int convert_bmp_idx_to_gray_idx(int idx, uint32_t w, uint32_t h)
{
   int row = h - (idx / w) - 1;
 
   return (row * w + idx % w) / 2;
}
 
int convert_one_image(char *in_path, void *out_buf, uint32_t offset,
             struct grayscale_header *gr_hdr, int type)
{
   struct bmp_image *bmp;
   struct bmp_header *bmp_hdr;
   FILE *file;
   void *bmp_buf;
   int size;
   int ret = -1;
   uint8_t *gr16_data = (uint8_t *)out_buf;
 
   if (!out_buf || !in_path) {
       fprintf(stderr, "in_path or out_buf is NULL.\n");
       return -1;
   }
 
   file = fopen(in_path, "rb");
   if (!file) {
       fprintf(stderr, "File %s open failed.\n", in_path);
       return -1;
   }
 
   fseek(file, 0, SEEK_END);
   size = ftell(file);
   fseek(file, 0, SEEK_SET);
 
   bmp_buf = calloc(1, size);
   if (!bmp_buf) {
       fprintf(stderr, "Allocate memory of %d bytes failed.\n", size);
       fclose(file);
       return -1;
   }
   if (size != fread(bmp_buf, 1, size, file)) {
       fprintf(stderr, "Incomplete read of file %s.\n", in_path);
       goto out;
   }
 
   bmp = (struct bmp_image *)bmp_buf;
   bmp_hdr = &bmp->hdr;
   if (!(bmp_hdr->signature[0] == 'B' &&
         bmp_hdr->signature[1] == 'M')) {
       printf("cat not find bmp file\n");
       goto out;
   }
 
   if (size != le32_to_cpu(bmp_hdr->file_size)) {
       fprintf(stderr, "Invalid BMP file size %d.\n",
           le32_to_cpu(bmp_hdr->file_size));
       goto out;
   }
   printf("bmp_hdr->width=%d, bmp_hdr->height=%d\n",
          bmp_hdr->width, bmp_hdr->height);
   printf("screen_w=%d, screen_h=%d\n", screen_w, screen_h);
   if (le32_to_cpu(bmp_hdr->width) != screen_w ||
       le32_to_cpu(bmp_hdr->height) != screen_h) {
       fprintf(stderr, "The image size must same with others.\n");
       goto out;
   }
   //write header
   gr_hdr->magic[0] = 'G';
   gr_hdr->magic[1] = 'R';
   gr_hdr->magic[2] = '0';
   gr_hdr->magic[3] = '4';
   gr_hdr->x = 0;
   gr_hdr->y = 0;
   gr_hdr->w = screen_w;
   gr_hdr->h = screen_h;
   gr_hdr->logo_type = type;
   gr_hdr->data_offset = offset;
   gr_hdr->data_size = screen_w * screen_h / 2;
 
   printf("bmp depth is %d\n", bmp_hdr->bit_count);
   //convert rgb to gray data, and write to output buffer
   // the used algorithm please refer to below url:
   // https://www.cnblogs.com/zhangjiansheng/p/6925722.html
   // we use below algorithm:
   // Gray = (R*19595 + G*38469 + B*7472) >> 16
   switch (bmp_hdr->bit_count) {
   case 16:{
       struct pixel_u16 *color_u16;
       int i;
 
       color_u16 = (struct pixel_u16 *)bmp->color_table;
       for (i = 0; i < screen_w * screen_h / 2; i++) {
           struct pixel_u16 *pix1 = &color_u16[2 * i];
           struct pixel_u16 *pix2 = &color_u16[2 * i + 1];
           int j = convert_bmp_idx_to_gray_idx(2 * i, screen_w,
                               screen_h);
           /*
            * the rgb value of pixel_u16 is 4 bits,
            * so the counted grayscale value is 4bit
            */
           uint32_t gray_px1 = (pix1->red * 19595 +
                        pix1->green * 38469 +
                        pix1->blue * 7472) >> 16;
           uint32_t gray_px2 = (pix2->red * 19595 +
                        pix2->green * 38469 +
                        pix2->blue * 7472) >> 16;
           gr16_data[j] = gray_px1 | (gray_px2 << 4);
       }
   }
   break;
   case 24: {
       struct pixel_u24 *color_u24;
       int i;
 
       color_u24 = (struct pixel_u24 *)bmp->color_table;
       for (i = 0; i < screen_w * screen_h / 2; i++) {
           struct pixel_u24 *pix1 = &color_u24[2 * i];
           struct pixel_u24 *pix2 = &color_u24[2 * i + 1];
           int j = convert_bmp_idx_to_gray_idx(2 * i, screen_w,
                               screen_h);
           /*
            * The rgb value of pixel_u24 is 8 bits,
            * so the counted grayscale
            * value need to divide into 16
            */
           uint32_t gray_px1 = ((pix1->red * 19595 +
                         pix1->green * 38469 +
                         pix1->blue * 7472) >> 16) >> 4;
           uint32_t gray_px2 = ((pix2->red * 19595 +
                         pix2->green * 38469 +
                         pix2->blue * 7472) >> 16) >> 4;
 
           gr16_data[j] = gray_px1 | (gray_px2 << 4);
       }
   }
   break;
   case 32: {
       struct pixel_u32 *color_u32;
       int i;
 
       color_u32 = (struct pixel_u32 *)bmp->color_table;
       for (i = 0; i < screen_w * screen_h / 2; i++) {
           struct pixel_u32 *pix1 = &color_u32[2 * i];
           struct pixel_u32 *pix2 = &color_u32[2 * i + 1];
           int j = convert_bmp_idx_to_gray_idx(2 * i, screen_w,
                               screen_h);
           /*
            * The rgb value of pixel_u32 is 8 bits,
            * so the counted grayscale
            * value need to divide into 16
            */
           uint32_t gray_px1 = ((pix1->red * 19595 +
                         pix1->green * 38469 +
                         pix1->blue * 7472) >> 16) >> 4;
           uint32_t gray_px2 = ((pix2->red * 19595 +
                         pix2->green * 38469 +
                         pix2->blue * 7472) >> 16) >> 4;
           gr16_data[j] = gray_px1 | (gray_px2 << 4);
       }
   }
   break;
   default:
       ret = -1;
       printf("Invalid bit count[%d],only support 16/24/32 bpp bmp\n",
              bmp_hdr->bit_count);
   break;
   }
 
   fprintf(stderr, "Convert image success\n");
   ret = 0;
out:
   free(bmp_buf);
   fclose(file);
   return ret;
}
 
void *init_grayscale_logo_buf(int logo_count, uint32_t size_one_image)
{
   int size;
   void *out_buf;
 
   if (!logo_count) {
       fprintf(stderr, "No input logo!\n");
       return NULL;
   }
   size = size_of_header();
   fprintf(stderr, "size of header in logo.img is %d\n", size);
   //every pixel of the grayscale image is 4 bits
   size += logo_count * size_one_image;
   out_buf = calloc(1, size);
 
   return out_buf;
}
 
void deinit_grayscale_logo_buf(void *buf)
{
   if (buf) {
       free(buf);
       buf = NULL;
   }
}
 
int main(int argc, char *argv[])
{
   char out_path[256] = {0};
   void *out_buf;
   int logo_count = 0;
   int i;
   int hdr_size, one_img_size, total_size;
   int ret = -1;
   struct logo_info *logo_hdr;
   FILE *file;
 
   PROG = fix_path(argv[0]);
 
   argc--, argv++;
   while (argc > 0 && argv[0][0] == '-') {
       /* it's a opt arg. */
       const char *arg = argv[0];
 
       argc--, argv++;
       if (!strcmp("-h", arg)) {
           usage();
           return 0;
       }  else if (!strcmp("--charge-logo", arg)) {
           int len, i;
           /*
            * Charge logo are located in directory
            * u-boot/tools/images/eink/, there are 7
            * pictures to tell user the battery capacity
            * during charging
            */
           for (i = 0; i < 7; i++) {
               int logo_type = EINK_LOGO_CHARGING_0 << i;
 
               len = strlen(argv[0]);
               if (len > 256) {
                   fprintf(stderr,
                       "input charging logo path %s is too long.\n",
                       argv[0]);
                   return -1;
               }
               printf("charge logo path %s\n", argv[0]);
               memcpy(in_img_info[logo_count].path,
                      argv[0], len);
               in_img_info[logo_count].logo_type = logo_type;
               logo_count++;
               argc--, argv++;
           }
       }  else if (!strcmp("--uboot-logo", arg)) {
           int len = strlen(argv[0]);
 
           if (len > 256) {
               printf("Uboot logo path %s is too long.\n",
                      argv[0]);
               return -1;
           }
           memcpy(in_img_info[logo_count].path, argv[0], len);
           in_img_info[logo_count].logo_type = EINK_LOGO_UBOOT;
           logo_count++;
           argc--, argv++;
       } else if (!strcmp("--kernel-logo", arg)) {
           int len = strlen(argv[0]);
 
           if (len > 256) {
               printf("Kernel logo path %s is too long\n",
                      argv[0]);
               return -1;
           }
           memcpy(in_img_info[logo_count].path, argv[0], len);
           in_img_info[logo_count].logo_type = EINK_LOGO_KERNEL;
           logo_count++;
           argc--, argv++;
       } else if (!strcmp("--poweroff-logo", arg)) {
           int len = strlen(argv[0]);
 
           if (len > 256) {
               printf("Poweroff logo path %s is too long\n",
                      argv[0]);
               return -1;
           }
           memcpy(in_img_info[logo_count].path, argv[0], len);
           in_img_info[logo_count].logo_type = EINK_LOGO_POWEROFF;
           logo_count++;
           argc--, argv++;
       }  else if (!strcmp("--screen-width", arg)) {
           screen_w = strtoul(argv[0], NULL, 10);
           argc--, argv++;
       }  else if (!strcmp("--screen-height", arg)) {
           screen_h = strtoul(argv[0], NULL, 10);
           argc--, argv++;
       }  else if (!strcmp("--output", arg)) {
           int len = strlen(argv[0]);
 
           if (len > 256) {
               printf("input output path %s is too long.\n",
                      argv[0]);
               return -1;
           }
           memcpy(out_path, argv[0], len);
           argc--, argv++;
       } else {
           fprintf(stderr, "Unknown opt:%s", arg);
           usage();
           return -1;
       }
   }
 
   ret = get_logo_resolution(in_img_info[0].path, &screen_w, &screen_h);
   if (ret < 0) {
       fprintf(stderr,
           "Get height and width from logo image failed.\n");
       usage();
       return -1;
   }
 
   if (screen_w == 0 || screen_h == 0) {
       fprintf(stderr,
           "The screen weight and screen height must be set.\n");
       usage();
       return -1;
   }
 
   file = fopen(out_path, "wb+");
   if (!file) {
       fprintf(stderr, "File %s open failed.\n", out_path);
       usage();
       return -1;
   }
   hdr_size = size_of_header();
   one_img_size = size_of_one_image();
 
   out_buf = init_grayscale_logo_buf(logo_count, one_img_size);
   if (!out_buf) {
       fprintf(stderr, "Can't malloc buffer for grayscale image.\n");
       fclose(file);
       return -1;
   }
 
   logo_hdr = (struct logo_info *)out_buf;
   fprintf(stderr, "logo count is %d,one_img_size=%d,size=%d.\n",
       logo_count, one_img_size, screen_w * screen_h / 2);
   for (i = 0; i < logo_count; i++) {
       char *in_path = in_img_info[i].path;
       int type = in_img_info[i].logo_type;
       void *img_buf;
       int offset = hdr_size + i *  one_img_size;
 
       img_buf = out_buf + offset;
       printf("image[%d] start addr=0x%p\n", i, img_buf);
       ret = convert_one_image(in_path, img_buf, offset,
                   &logo_hdr->img_hdr[i], type);
       if (ret < 0) {
           printf("Convert image[%d] failed, type is %d\n",
                  i, type);
           break;
       }
   }
 
   if (ret == 0) {
       struct logo_part_header *part_hdr = &logo_hdr->part_hdr;
 
       total_size = hdr_size + (i - 1) * one_img_size +
               screen_h * screen_w / 2;
 
       //convert success, write header data.
       part_hdr->magic[0] = 'R';
       part_hdr->magic[1] = 'K';
       part_hdr->magic[2] = 'E';
       part_hdr->magic[3] = 'L';
       part_hdr->totoal_size = total_size;
       part_hdr->screen_width = screen_w;
       part_hdr->screen_height = screen_h;
       part_hdr->logo_count = i;
       printf("screen w=%d, h=%d, total_size=%d\n",
              screen_w, screen_h, total_size);
       memcpy(part_hdr->version, version, 4);
 
       // write to output file
       ret = fwrite(out_buf, total_size, 1, file);
       if (ret != 1)
           fprintf(stderr, "write image to file %s failed\n",
               out_path);
   }
 
   deinit_grayscale_logo_buf(out_buf);
   ret = fclose(file);
   if (ret != 0)
       printf("Close file[%s] failed, err=%d\n", out_path, ret);
   file = NULL;
   return ret;
}