File: corpus_gen.cpp

package info (click to toggle)
crunch-dxtc 0.55.5-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,624 kB
  • sloc: cpp: 64,979; ansic: 633; python: 321; makefile: 116
file content (319 lines) | stat: -rw-r--r-- 10,192 bytes parent folder | download | duplicates (2)
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
// File: corpus_gen.cpp - Block compression corpus generator.
// See Copyright Notice and license at the end of inc/crnlib.h
//
// Example command line:
// -gentest [-deep] [-blockpercentage .035] [-width 4096] [-height 4096] -in c:\temp\*.jpg [-in c:\temp\*.jpeg] [-in @blah.txt]
#include "crn_core.h"
#include "corpus_gen.h"
#include "crn_console.h"

#include "crn_find_files.h"
#include "crn_file_utils.h"
#include "crn_command_line_params.h"

#include "crn_dxt.h"
#include "crn_cfile_stream.h"
#include "crn_texture_conversion.h"
#include "crn_radix_sort.h"

#include "crn_defs.h"

namespace crnlib {
struct block {
  color_quad_u8 m_c[4 * 4];

  inline operator size_t() const { return fast_hash(this, sizeof(*this)); }

  inline bool operator==(const block& rhs) const {
    return memcmp(this, &rhs, sizeof(*this)) == 0;
  }
};

typedef crnlib::hash_map<block, empty_type> block_hash_map;

corpus_gen::corpus_gen() {
}

void corpus_gen::sort_blocks(image_u8& img) {
  const uint num_blocks_x = img.get_width() / 4;
  const uint num_blocks_y = img.get_height() / 4;
  const uint total_blocks = num_blocks_x * num_blocks_y;

  console::printf("Sorting %u blocks...", total_blocks);

  crnlib::vector<float> block_std_dev(total_blocks);

  for (uint by = 0; by < num_blocks_y; by++) {
    for (uint bx = 0; bx < num_blocks_x; bx++) {
      color_quad_u8 c[4 * 4];

      for (uint y = 0; y < 4; y++)
        for (uint x = 0; x < 4; x++)
          c[x + y * 4] = img(bx * 4 + x, by * 4 + y);

      double std_dev = 0.0f;
      for (uint i = 0; i < 3; i++)
        std_dev += image_utils::compute_std_dev(16, c, i, 1);

      block_std_dev[bx + by * num_blocks_x] = (float)std_dev;
    }
  }

  crnlib::vector<uint> block_indices0(total_blocks);
  crnlib::vector<uint> block_indices1(total_blocks);

  const uint* pIndices = indirect_radix_sort(total_blocks, &block_indices0[0], &block_indices1[0], &block_std_dev[0], 0, sizeof(float), true);

  image_u8 new_img(img.get_width(), img.get_height());

  uint dst_block_index = 0;

  //float prev_std_dev = -999;
  for (uint i = 0; i < total_blocks; i++) {
    uint src_block_index = pIndices[i];

    //float std_dev = block_std_dev[src_block_index];
    //crnlib_ASSERT(std_dev >= prev_std_dev);
    //prev_std_dev = std_dev;

    uint src_block_x = src_block_index % num_blocks_x;
    uint src_block_y = src_block_index / num_blocks_x;

    uint dst_block_x = dst_block_index % num_blocks_x;
    uint dst_block_y = dst_block_index / num_blocks_x;

    new_img.unclipped_blit(src_block_x * 4, src_block_y * 4, 4, 4, dst_block_x * 4, dst_block_y * 4, img);

    dst_block_index++;
  }

#if 0      
      //new_img.swap(img);
#else
  crnlib::vector<uint> remaining_blocks(num_blocks_x);

  console::printf("Arranging %u blocks...", total_blocks);

  for (uint by = 0; by < num_blocks_y; by++) {
    console::printf("%u of %u", by, num_blocks_y);

    remaining_blocks.resize(num_blocks_x);
    for (uint i = 0; i < num_blocks_x; i++)
      remaining_blocks[i] = i;

    color_quad_u8 match_block[16];
    utils::zero_object(match_block);
    for (uint bx = 0; bx < num_blocks_x; bx++) {
      uint best_index = 0;

      uint64 best_error = cUINT64_MAX;

      for (uint i = 0; i < remaining_blocks.size(); i++) {
        uint src_block_index = remaining_blocks[i];

        uint64 error = 0;
        for (uint y = 0; y < 4; y++) {
          for (uint x = 0; x < 4; x++) {
            const color_quad_u8& c = new_img(src_block_index * 4 + x, by * 4 + y);
            error += color::elucidian_distance(c, match_block[x + y * 4], false);
          }
        }

        if (error < best_error) {
          best_error = error;
          best_index = i;
        }
      }

      uint src_block_index = remaining_blocks[best_index];

      for (uint y = 0; y < 4; y++) {
        for (uint x = 0; x < 4; x++) {
          const color_quad_u8& c = new_img(src_block_index * 4 + x, by * 4 + y);
          match_block[x + y * 4] = c;

          img(bx * 4 + x, by * 4 + y) = c;
        }
      }

      remaining_blocks.erase_unordered(best_index);
    }
  }
#endif
}

bool corpus_gen::generate(const char* pCmd_line) {
  static const command_line_params::param_desc param_desc_array[] =
      {
          {"corpus_gen", 0, false},
          {"in", 1, true},
          {"deep", 0, false},
          {"blockpercentage", 1, false},
          {"width", 1, false},
          {"height", 1, false},
          {"alpha", 0, false},
      };

  command_line_params params;
  if (!params.parse(pCmd_line, CRNLIB_ARRAY_SIZE(param_desc_array), param_desc_array, true))
    return false;

  if (!params.has_key("in")) {
    console::error("Must specify one or more input files using the /in option!");
    return false;
  }

  uint num_dst_blocks_x = params.get_value_as_int("width", 0, 4096, 128, 4096);
  num_dst_blocks_x = (num_dst_blocks_x + 3) / 4;
  uint num_dst_blocks_y = params.get_value_as_int("height", 0, 4096, 128, 4096);
  num_dst_blocks_y = (num_dst_blocks_y + 3) / 4;

  const uint total_dst_blocks = num_dst_blocks_x * num_dst_blocks_y;
  image_u8 dst_img(num_dst_blocks_x * 4, num_dst_blocks_y * 4);
  uint next_dst_block = 0;
  uint total_dst_images = 0;

  random rm;

  block_hash_map block_hash;
  block_hash.reserve(total_dst_blocks);

  uint total_images_loaded = 0;
  uint total_blocks_written = 0;

  command_line_params::param_map_const_iterator it = params.begin();
  for (; it != params.end(); ++it) {
    if (it->first != "in")
      continue;
    if (it->second.m_values.empty()) {
      console::error("Must follow /in parameter with a filename!\n");
      return false;
    }

    for (uint in_value_index = 0; in_value_index < it->second.m_values.size(); in_value_index++) {
      const dynamic_string& filespec = it->second.m_values[in_value_index];

      find_files file_finder;
      if (!file_finder.find(filespec.get_ptr(), find_files::cFlagAllowFiles | (params.has_key("deep") ? find_files::cFlagRecursive : 0))) {
        console::warning("Failed finding files: %s", filespec.get_ptr());
        continue;
      }

      if (file_finder.get_files().empty()) {
        console::warning("No files found: %s", filespec.get_ptr());
        return false;
      }

      const find_files::file_desc_vec& files = file_finder.get_files();

      for (uint file_index = 0; file_index < files.size(); file_index++) {
        const find_files::file_desc& file_desc = files[file_index];

        console::printf("Loading image: %s", file_desc.m_fullname.get_ptr());

        image_u8 img;
        if (!image_utils::read_from_file(img, file_desc.m_fullname.get_ptr(), 0)) {
          console::warning("Failed loading image file: %s", file_desc.m_fullname.get_ptr());
          continue;
        }

        if (!params.has_key("alpha")) {
          for (uint y = 0; y < img.get_height(); y++)
            for (uint x = 0; x < img.get_width(); x++)
              img(x, y).a = 255;
        }

        total_images_loaded++;

        uint width = img.get_width();
        uint height = img.get_height();

        uint num_blocks_x = (width + 3) / 4;
        uint num_blocks_y = (height + 3) / 4;
        uint total_blocks = num_blocks_x * num_blocks_y;

        float percentage = params.get_value_as_float("blockpercentage", 0, .1f, .001f, 1.0f);
        uint total_rand_blocks = math::maximum<uint>(1U, (uint)(total_blocks * percentage));

        crnlib::vector<uint> remaining_blocks(total_blocks);
        for (uint i = 0; i < total_blocks; i++)
          remaining_blocks[i] = i;

        uint num_blocks_remaining = total_rand_blocks;
        while (num_blocks_remaining) {
          if (remaining_blocks.empty())
            break;

          uint rand_block_index = rm.irand(0, remaining_blocks.size());
          uint block_index = remaining_blocks[rand_block_index];
          remaining_blocks.erase_unordered(rand_block_index);

          uint block_y = block_index / num_blocks_x;
          uint block_x = block_index % num_blocks_x;

          block b;

          for (uint y = 0; y < 4; y++) {
            for (uint x = 0; x < 4; x++) {
              b.m_c[x + y * 4] = img.get_clamped(block_x * 4 + x, block_y * 4 + y);
            }
          }

          if (!block_hash.insert(b).second)
            continue;
          if (block_hash.size() == total_dst_blocks) {
            block_hash.clear();
            block_hash.reserve(total_dst_blocks);
          }

          uint dst_block_x = next_dst_block % num_dst_blocks_x;
          uint dst_block_y = next_dst_block / num_dst_blocks_x;
          for (uint y = 0; y < 4; y++) {
            for (uint x = 0; x < 4; x++) {
              dst_img(dst_block_x * 4 + x, dst_block_y * 4 + y) = b.m_c[x + y * 4];
            }
          }

          next_dst_block++;
          if (total_dst_blocks == next_dst_block) {
            sort_blocks(dst_img);

            dynamic_string dst_filename(cVarArg, "test_%u.tga", total_dst_images);
            console::printf("Writing image: %s", dst_filename.get_ptr());
            image_utils::write_to_file(dst_filename.get_ptr(), dst_img, 0);

            dst_img.set_all(color_quad_u8::make_black());

            next_dst_block = 0;

            total_dst_images++;
          }

          total_blocks_written++;

          num_blocks_remaining--;
        }

      }  // file_index

    }  // in_value_index
  }

  if (next_dst_block) {
    sort_blocks(dst_img);

    dynamic_string dst_filename(cVarArg, "test_%u.tga", total_dst_images);
    console::printf("Writing image: %s", dst_filename.get_ptr());
    image_utils::write_to_file(dst_filename.get_ptr(), dst_img, 0);

    next_dst_block = 0;

    total_dst_images++;
  }

  console::printf("Found %u input images, %u output images, %u total blocks", total_images_loaded, total_dst_images, total_blocks_written);

  return true;
}

}  // namespace crnlib