cdae3486ce008e80cc2d9bb277673872ab0a16e0
[mesa.git] / progs / rbug / bin_to_bmp.c
1 /*
2 * Copyright 2009 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VMWARE AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include "pipe/p_compiler.h"
26 #include "pipe/p_format.h"
27 #include "pipe/p_state.h"
28 #include "util/u_memory.h"
29 #include "util/u_debug.h"
30 #include "util/u_network.h"
31 #include "util/u_tile.h"
32
33 static uint8_t* read(const char *filename, unsigned size);
34 static void dump(unsigned src_width, unsigned src_height,
35 unsigned src_stride, enum pipe_format src_format,
36 uint8_t *data, unsigned src_size);
37
38 int main(int argc, char** argv)
39 {
40 /* change these */
41 unsigned width = 64;
42 unsigned height = 64;
43 unsigned stride = width * 4;
44 unsigned size = stride * height;
45 const char *filename = "mybin.bin";
46 enum pipe_format format = PIPE_FORMAT_A8R8G8B8_UNORM;
47
48 dump(width, height, stride, format, read(filename, size), size);
49
50 return 0;
51 }
52
53 static void dump(unsigned width, unsigned height,
54 unsigned src_stride, enum pipe_format src_format,
55 uint8_t *data, unsigned src_size)
56 {
57 struct pipe_format_block src_block;
58
59 enum pipe_format dst_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
60 struct pipe_format_block dst_block;
61 unsigned dst_stride;
62 unsigned dst_size;
63 float *rgba;
64 int i;
65 char filename[512];
66
67 {
68 pf_get_block(src_format, &src_block);
69 assert(src_stride >= pf_get_stride(&src_block, width));
70 assert(src_size >= pf_get_2d_size(&src_block, src_stride, width));
71 }
72 {
73 pf_get_block(dst_format, &dst_block);
74 dst_stride = pf_get_stride(&dst_block, width);
75 dst_size = pf_get_2d_size(&dst_block, dst_stride, width);
76 rgba = MALLOC(dst_size);
77 }
78
79 util_snprintf(filename, 512, "%s.bmp", pf_name(src_format));
80
81 if (pf_is_compressed(src_format)) {
82 debug_printf("skipping: %s\n", filename);
83 return;
84 }
85
86 debug_printf("saving: %s\n", filename);
87
88 for (i = 0; i < height; i++) {
89 pipe_tile_raw_to_rgba(src_format, data + src_stride * i,
90 width, 1,
91 &rgba[width*4*i], dst_stride);
92 }
93
94 debug_dump_float_rgba_bmp(filename, width, height, rgba, width);
95
96 FREE(rgba);
97 }
98
99 static uint8_t* read(const char *filename, unsigned size)
100 {
101 uint8_t *data;
102 FILE *file = fopen(filename, "rb");
103
104 data = MALLOC(size);
105
106 fread(data, 1, size, file);
107 fclose(file);
108
109 return data;
110 }