NTrace
GPU ray tracing framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ImageLodePngIO.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009-2011, NVIDIA Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA Corporation nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "io/ImageLodePngIO.hpp"
29 #include "io/Stream.hpp"
30 #include "gui/Image.hpp"
31 
33 
34 using namespace FW;
35 
36 //------------------------------------------------------------------------
37 
39 {
40  // Read the entire input stream.
41 
42  Array<U8> dataBuffer;
43  int blockSize = 4096;
44  for (;;)
45  {
46  int pos = dataBuffer.getSize();
47  dataBuffer.resize(pos + blockSize);
48  int num = stream.read(dataBuffer.getPtr(pos), blockSize);
49  if (num < blockSize)
50  {
51  dataBuffer.resize(pos + num);
52  break;
53  }
54  }
55 
56  if (hasError())
57  return NULL;
58 
59  // Decode image info.
60 
61  LodePNG::Decoder decoder;
62  decoder.inspect(dataBuffer.getPtr(), dataBuffer.getSize());
63  Vec2i size(decoder.getWidth(), decoder.getHeight());
64  bool hasAlpha = (LodePNG_InfoColor_canHaveAlpha(&decoder.getInfoPng().color) != 0);
65 
66  if (decoder.hasError())
67  setError("importLodePngImage(): LodePNG error %d!", decoder.getError());
68  if (min(size) <= 0)
69  setError("importLodePngImage(): Invalid image size!");
70  if (hasError())
71  return NULL;
72 
73  // Decode image data.
74 
75  int numBytes = size.x * size.y * ((hasAlpha) ? 4 : 3);
76  std::vector<U8> pixelBuffer;
77  pixelBuffer.reserve(numBytes);
78  decoder.getInfoRaw().color.colorType = (hasAlpha) ? 6 : 2;
79  decoder.decode(pixelBuffer, dataBuffer.getPtr(), dataBuffer.getSize());
80 
81  if (decoder.hasError())
82  setError("importLodePngImage(): LodePNG error %d!", decoder.getError());
83  if ((int)pixelBuffer.size() != numBytes)
84  setError("importLodePngImage(): Incorrect amount of pixel data!");
85  if (hasError())
86  return NULL;
87 
88  // Create image.
89 
91  image->getBuffer().set(&pixelBuffer[0], numBytes);
92  return image;
93 }
94 
95 //------------------------------------------------------------------------
96 
98 {
99  // Select format and convert.
100 
101  FW_ASSERT(image);
102  const Vec2i& size = image->getSize();
103  bool hasAlpha = image->getFormat().hasChannel(ImageFormat::ChannelType_A);
105 
106  Image* converted = NULL;
107  if (image->getFormat().getID() != format)
108  {
109  converted = new Image(size, format);
110  *converted = *image;
111  }
112 
113  // Encode image.
114 
115  LodePNG::Encoder encoder;
116  int colorType = (hasAlpha) ? 6 : 2;
117  encoder.getSettings().autoLeaveOutAlphaChannel = false;
118  encoder.getInfoRaw().color.colorType = colorType;
119  encoder.getInfoPng().color.colorType = colorType;
120 
121  std::vector<U8> dataBuffer;
122  encoder.encode(dataBuffer, (U8*)((converted) ? converted : image)->getPtr(), size.x, size.y);
123 
124  if (encoder.hasError())
125  setError("exportLodePngImage(): LodePNG error %d!", encoder.getError());
126 
127  // Write to the output stream.
128 
129  if (!hasError() && !dataBuffer.empty())
130  stream.write(&dataBuffer[0], (int)dataBuffer.size());
131 
132  // Clean up.
133 
134  delete converted;
135 }
136 
137 //------------------------------------------------------------------------
#define NULL
Definition: Defs.hpp:39
void set(const void *ptr)
Definition: Buffer.hpp:92
void setError(const char *fmt,...)
Definition: Defs.cpp:253
ID getID(void) const
Definition: Image.cpp:78
virtual void write(const void *ptr, int size)=0
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
Definition: Math.hpp:461
#define FW_ASSERT(X)
Definition: Defs.hpp:67
Image * importLodePngImage(InputStream &stream)
Buffer & getBuffer(void) const
Definition: Image.hpp:148
bool hasError(void)
Definition: Defs.cpp:289
const Vec2i & getSize(void) const
Definition: Image.hpp:143
const ImageFormat & getFormat(void) const
Definition: Image.hpp:144
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: DLLImports.inl:349
unsigned char U8
Definition: Defs.hpp:83
void exportLodePngImage(OutputStream &stream, const Image *image)
unsigned LodePNG_InfoColor_canHaveAlpha(const LodePNG_InfoColor *info)
Definition: lodepng.cpp:2530
const T * getPtr(S idx=0) const
Definition: Array.hpp:202
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Definition: DLLImports.inl:60
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
Definition: DLLImports.inl:319
void resize(S size)
Definition: Array.hpp:366
S getSize(void) const
Definition: Array.hpp:188
virtual int read(void *ptr, int size)=0
bool hasChannel(ChannelType type) const
Definition: Image.hpp:110