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 <stdio.h>
26
27#include "pipe/p_compiler.h"
28#include "pipe/p_format.h"
29#include "pipe/p_state.h"
30#include "util/u_format.h"
31#include "util/u_memory.h"
32#include "util/u_debug.h"
33#include "util/u_format.h"
34#include "util/u_network.h"
35#include "util/u_string.h"
36#include "util/u_tile.h"
37
38static uint8_t* rbug_read(const char *filename, unsigned size);
39static void dump(unsigned src_width, unsigned src_height,
40                 unsigned src_stride, enum pipe_format src_format,
41                 uint8_t *data, unsigned src_size);
42
43int main(int argc, char** argv)
44{
45   /* change these */
46   unsigned width = 64;
47   unsigned height = 64;
48   unsigned stride = width * 4;
49   unsigned size = stride * height;
50   const char *filename = "mybin.bin";
51   enum pipe_format format = PIPE_FORMAT_B8G8R8A8_UNORM;
52
53   dump(width, height, stride, format, rbug_read(filename, size), size);
54
55   return 0;
56}
57
58static void dump(unsigned width, unsigned height,
59                 unsigned src_stride, enum pipe_format src_format,
60                 uint8_t *data, unsigned src_size)
61{
62   enum pipe_format dst_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
63   unsigned dst_stride;
64   unsigned dst_size;
65   float *rgba;
66   int i;
67   char filename[512];
68
69   {
70      assert(src_stride >= util_format_get_stride(src_format, width));
71   }
72   {
73      dst_stride = util_format_get_stride(dst_format, width);
74      dst_size = util_format_get_2d_size(dst_format, dst_stride, width);
75      rgba = MALLOC(dst_size);
76   }
77
78   util_snprintf(filename, 512, "%s.bmp", util_format_name(src_format));
79
80   if (util_format_is_compressed(src_format)) {
81      debug_printf("skipping: %s\n", filename);
82      return;
83   }
84
85   debug_printf("saving: %s\n", filename);
86
87   for (i = 0; i < height; i++) {
88      pipe_tile_raw_to_rgba(src_format, data + src_stride * i,
89                            width, 1,
90                            &rgba[width*4*i], dst_stride);
91   }
92
93   debug_dump_float_rgba_bmp(filename, width, height, rgba, width);
94
95   FREE(rgba);
96}
97
98static uint8_t* rbug_read(const char *filename, unsigned size)
99{
100   uint8_t *data;
101   FILE *file = fopen(filename, "rb");
102
103   data = MALLOC(size);
104
105   fread(data, 1, size, file);
106   fclose(file);
107
108   return data;
109}
110