summaryrefslogtreecommitdiff
path: root/progs/rbug/bin_to_bmp.c
blob: 40ae62909095a91db40b4046b7d05b0850126f08 (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
/*
 * Copyright 2009 VMware, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>

#include "pipe/p_compiler.h"
#include "pipe/p_format.h"
#include "pipe/p_state.h"
#include "util/u_format.h"
#include "util/u_memory.h"
#include "util/u_debug.h"
#include "util/u_format.h"
#include "util/u_network.h"
#include "util/u_string.h"
#include "util/u_tile.h"

static uint8_t* rbug_read(const char *filename, unsigned size);
static void dump(unsigned src_width, unsigned src_height,
                 unsigned src_stride, enum pipe_format src_format,
                 uint8_t *data, unsigned src_size);

int main(int argc, char** argv)
{
   /* change these */
   unsigned width = 64;
   unsigned height = 64;
   unsigned stride = width * 4;
   unsigned size = stride * height;
   const char *filename = "mybin.bin";
   enum pipe_format format = PIPE_FORMAT_A8R8G8B8_UNORM;

   dump(width, height, stride, format, rbug_read(filename, size), size);

   return 0;
}

static void dump(unsigned width, unsigned height,
                 unsigned src_stride, enum pipe_format src_format,
                 uint8_t *data, unsigned src_size)
{
   enum pipe_format dst_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
   unsigned dst_stride;
   unsigned dst_size;
   float *rgba;
   int i;
   char filename[512];

   {
      assert(src_stride >= util_format_get_stride(src_format, width));
   }
   {
      dst_stride = util_format_get_stride(dst_format, width);
      dst_size = util_format_get_2d_size(dst_format, dst_stride, width);
      rgba = MALLOC(dst_size);
   }

   util_snprintf(filename, 512, "%s.bmp", util_format_name(src_format));

   if (util_format_is_compressed(src_format)) {
      debug_printf("skipping: %s\n", filename);
      return;
   }

   debug_printf("saving: %s\n", filename);

   for (i = 0; i < height; i++) {
      pipe_tile_raw_to_rgba(src_format, data + src_stride * i,
                            width, 1,
                            &rgba[width*4*i], dst_stride);
   }

   debug_dump_float_rgba_bmp(filename, width, height, rgba, width);

   FREE(rgba);
}

static uint8_t* rbug_read(const char *filename, unsigned size)
{
   uint8_t *data;
   FILE *file = fopen(filename, "rb");

   data = MALLOC(size);

   fread(data, 1, size, file);
   fclose(file);

   return data;
}