Lumiverse  2.5
A framework for creating lighting control applications
imageio.h
1 /* LIBRARY: ImageIO Library v0.13
2 *
3 * FILE: imageio.hpp
4 *
5 * COPYRIGHT: Copyright (c) 2003-2004 Adrien Treuille
6 *
7 * AUTHORS: Adrien Treuille (treuille [AT] cs [DOT] washington [DOT] edu)
8 * Kristin Siu
9 *
10 * LICENSE: The ImageIO Library is supplied "AS IS". The Authors
11 * disclaim all warranties, expressed or implied,
12 * including, without limitation, the warranties of
13 * merchantability and of fitness for any purpose.
14 * The Authors assume no liability for direct, indirect,
15 * incidental, special, exemplary, or consequential
16 * damages, which may result from the use of the ImageIO
17 * Library, even if advised of the possibility of such
18 * damage.
19 *
20 * Permission is hereby granted to use, copy, modify,
21 * and distribute this source code, or portions hereof,
22 * for any purpose, without fee, for non-commercial
23 * purposes. All rights reserved for commercial uses of
24 * this source code.
25 */
26 
27 #ifndef IMAGEIO_HPP_
28 #define IMAGEIO_HPP_
29 
30 #include <cstdlib>
31 #include <string.h>
32 
33 namespace Lumiverse {
34 
35  // Sets tbe width and height to the appropriate values and mallocs
36  // a char *buffer loading up the values in row-major, RGBA format.
37  // The memory associated with the buffer can be deallocated with free().
38  // If there was an error reading file, then 0 is returned, and
39  // width = height are set to -1.
40  unsigned char* imageio_load_image(const char* filename, int *width, int *height);
41 
42  // Saves image given by buffer with specicified width and height
43  // to the given file name, returns true on success, false otherwise.
44  // The image format is RGBA.
45  bool imageio_save_image(const char* filename, unsigned char* buffer, int width, int height);
46 
47  // puts a default filename in name, up to len characters
48  void imageio_gen_name(char* filename, size_t len);
49 
50  // Converts from float image to byte image.
51  inline void floats_to_bytes(unsigned char *arr, float *rgba, int width, int height)
52  {
53  for (int j = 0; j < height; j++) {
54  for (int i = 0; i < width; i++) {
55  int offset = (j * width + i) * 4;
56  int inv_offset = ((height - 1 - j) * width + i) * 4;
57 
58  // convert to bytes
59  arr[offset] = static_cast<unsigned char>(rgba[inv_offset] * 0xff);
60  arr[offset + 1] = static_cast<unsigned char>(rgba[inv_offset + 1] * 0xff);
61  arr[offset + 2] = static_cast<unsigned char>(rgba[inv_offset + 2] * 0xff);
62  arr[offset + 3] = static_cast<unsigned char>(rgba[inv_offset + 3] * 0xff);
63  }
64  }
65  }
66 
67  // Converts from byte image to float image.
68  inline void bytes_to_floats(float *rgba, unsigned char *arr, int width, int height)
69  {
70  for (int j = 0; j < height; j++) {
71  for (int i = 0; i < width; i++) {
72  int offset = (j * width + i) * 4;
73  int inv_offset = ((height - 1 - j) * width + i) * 4;
74 
75  // convert to float
76  rgba[offset] = static_cast<float>(arr[inv_offset]) / 0xff;
77  rgba[offset + 1] = static_cast<float>(arr[inv_offset + 1]) / 0xff;
78  rgba[offset + 2] = static_cast<float>(arr[inv_offset + 2]) / 0xff;
79  rgba[offset + 3] = static_cast<float>(arr[inv_offset + 3]) / 0xff;
80  }
81  }
82  }
83 }
84 
85 #endif /* IMAGEIO_HPP_ */
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2