summaryrefslogtreecommitdiff
path: root/utility/include/bmpblk_utility.h
blob: 9c708ba3cfc615ad11470bc929cdf53a5bed0564 (plain)
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
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.


#ifndef VBOOT_REFERENCE_BMPBLK_UTILITY_H_
#define VBOOT_REFERENCE_BMPBLK_UTILITY_H_

#include "bmpblk_header.h"

#include <yaml.h>

#include <map>
#include <string>
#include <vector>

using std::map;
using std::string;
using std::vector;

namespace vboot_reference {

/* Internal struct for contructing ImageInfo. */
typedef struct ImageConfig {
  ImageInfo data;
  string filename;
  string raw_content;
  string compressed_content;
  uint32_t offset;
} ImageConfig;

/* Internal struct for contructing ScreenLayout. */
typedef struct ScreenConfig {
  ScreenLayout data;
  string image_names[MAX_IMAGE_IN_LAYOUT];
} ScreenConfig;

typedef map<string, ImageConfig> StrImageConfigMap;
typedef map<string, ScreenConfig> StrScreenConfigMap;

/* Internal struct for contructing the whole BmpBlock. */
typedef struct BmpBlockConfig {
  string config_filename;
  BmpBlockHeader header;
  vector<string> image_names;
  StrImageConfigMap images_map;
  StrScreenConfigMap screens_map;
  vector<vector<string> > localizations;
} BmpBlockConfig;

class BmpBlockUtil {
 public:
  BmpBlockUtil();
  ~BmpBlockUtil();

  /* Load all the images and related infomations according to a config file. */
  void load_from_config(const char *filename);

  /* Compress all the images using a given comression method. */
  void compress_all_images(const Compression compress);

  /* Contruct the bmpblock. */
  void pack_bmpblock();

  /* Write the bmpblock to a file */
  void write_to_bmpblock(const char *filename);

  /* What compression to use for the images */
  void force_compression(uint32_t compression);

 private:
  /* Clear all internal data. */
  void initialize();

  /* Elemental function called from load_from_config.
   * Load the config file (yaml format) and parse it. */
  void load_yaml_config(const char *filename);

  /* Elemental function called from load_from_config.
   * Load all image files into the internal variables. */
  void load_all_image_files();

  /* Elemental function called from load_from_config.
   * Contruct all ImageInfo structs. */
  void fill_all_image_infos();

  /* Elemental function called from load_from_config.
   * Contruct the BmpBlockHeader struct. */
  void fill_bmpblock_header();

  /* Helper functions for parsing a YAML config file. */
  void expect_event(yaml_parser_t *parser, const yaml_event_type_e type);
  void parse_config(yaml_parser_t *parser);
  void parse_first_layer(yaml_parser_t *parser);
  void parse_bmpblock(yaml_parser_t *parser);
  void parse_compression(yaml_parser_t *parser);
  void parse_images(yaml_parser_t *parser);
  void parse_layout(yaml_parser_t *parser, ScreenConfig &screen);
  void parse_screens(yaml_parser_t *parser);
  void parse_localizations(yaml_parser_t *parser);

  /* Useful functions */
  const string read_image_file(const char *filename);
  ImageFormat get_image_format(const string content);
  uint32_t get_bmp_image_width(const string content);
  uint32_t get_bmp_image_height(const string content);

  /* Internal variable for storing the config of BmpBlock. */
  BmpBlockConfig config_;

  /* Internal variable for storing the content of BmpBlock. */
  string bmpblock_;

  /* Internal variables to determine whether or not to specify compression */
  bool set_compression_;                // true if we force it
  uint32_t compression_;                // what we force it to
};

}  // namespace vboot_reference

#endif  // VBOOT_REFERENCE_BMPBLK_UTILITY_H_