Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
acgl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ACGL
acgl
Commits
7e8af607
Commit
7e8af607
authored
11 years ago
by
Janis Born
Browse files
Options
Downloads
Patches
Plain Diff
ensure correct endianness for decoding and encoding 16 bit raw data using LodePNG
parent
c021ba6b
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/ACGL/OpenGL/Data/TextureDataLoadStore.cc
+87
-53
87 additions, 53 deletions
src/ACGL/OpenGL/Data/TextureDataLoadStore.cc
with
87 additions
and
53 deletions
src/ACGL/OpenGL/Data/TextureDataLoadStore.cc
+
87
−
53
View file @
7e8af607
...
...
@@ -15,6 +15,7 @@
#include
<cstdio>
#include
<ctime>
#include
<stdexcept>
#include
<fstream>
#include
"lodepng/lodepng.h"
#include
"rgbe/rgbe.hh"
...
...
@@ -163,34 +164,52 @@ bool saveScreenshotWithDate(const std::string& _prefix, const std::string& _file
// library specific load
///////////////////////////////////////////////////////////////////////////////////////////////////
struct
LodepngFile
{
public:
LodepngFile
(
const
std
::
string
&
_filename
)
{
unsigned
int
errorCode
=
lodepng_load_file
(
&
mData
,
&
mSize
,
_filename
.
c_str
());
if
(
errorCode
)
{
std
::
stringstream
errorMsg
;
errorMsg
<<
"LodePNG error while loading file "
<<
_filename
<<
" - "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
);
mData
=
nullptr
;
throw
std
::
runtime_error
(
errorMsg
.
str
());
}
}
~
LodepngFile
()
{
free
(
mData
);
}
unsigned
char
*
mData
;
size_t
mSize
;
};
SharedTextureData
loadTextureDataFromLodepng
(
const
std
::
string
&
_filename
)
{
SharedTextureData
data
;
unsigned
int
errorCode
;
unsigned
int
width
,
height
;
unsigned
char
*
png
;
size_t
pngsize
;
LodePNGState
state
;
// Load the PNG file from disk
lodepng_state_init
(
&
state
);
errorCode
=
lodepng_load_file
(
&
png
,
&
pngsize
,
_filename
.
c_str
());
if
(
errorCode
)
try
{
ACGL
::
Utils
::
error
()
<<
"LodePNG error while loading file "
<<
_filename
<<
" - "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
)
<<
std
::
endl
;
return
data
;
}
LodepngFile
lodepngFile
(
_filename
);
// Extract metadata (bit depth, color type)
errorCode
=
lodepng_inspect
(
&
width
,
&
height
,
&
state
,
png
,
pngsize
);
errorCode
=
lodepng_inspect
(
&
width
,
&
height
,
&
state
,
lodepngFile
.
mData
,
lodepngFile
.
mSize
);
if
(
errorCode
)
{
ACGL
::
Utils
::
error
()
<<
"LodePNG error "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
)
<<
std
::
endl
;
return
data
;
std
::
stringstream
errorMsg
;
errorMsg
<<
"LodePNG error "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
);
throw
std
::
runtime_error
(
errorMsg
.
str
());
}
unsigned
int
bitdepth
=
state
.
info_png
.
color
.
bitdepth
;
...
...
@@ -208,28 +227,32 @@ SharedTextureData loadTextureDataFromLodepng(const std::string &_filename)
GLenum
glType
=
0
;
if
(
bitdepth
==
8
)
glType
=
GL_UNSIGNED_BYTE
;
if
(
bitdepth
==
16
)
glType
=
GL_UNSIGNED_SHORT
;
if
(
bitdepth
==
32
)
glType
=
GL_UNSIGNED_INT
;
if
(
channels
==
0
||
glFormat
==
0
||
glType
==
0
)
{
ACGL
::
Utils
::
error
()
<<
"Could not load "
<<
_filename
<<
": "
<<
"unsupported bit depth or format"
<<
std
::
endl
;
return
data
;
std
::
stringstream
errorMsg
;
errorMsg
<<
"Could not load "
<<
_filename
<<
": "
<<
"unsupported bit depth or format"
;
throw
std
::
runtime_error
(
errorMsg
.
str
());
}
// Decode the image
unsigned
char
*
image
;
errorCode
=
lodepng_decode_memory
(
&
image
,
&
width
,
&
height
,
png
,
pngs
ize
,
colorType
,
bitdepth
);
errorCode
=
lodepng_decode_memory
(
&
image
,
&
width
,
&
height
,
lodepngFile
.
mData
,
lodepngFile
.
mS
ize
,
colorType
,
bitdepth
);
if
(
errorCode
)
{
ACGL
::
Utils
::
error
()
<<
"LodePNG error while loading file "
<<
_filename
<<
" - "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
)
<<
std
::
endl
;
return
data
;
std
::
stringstream
errorMsg
;
errorMsg
<<
"LodePNG error while decoding file "
<<
_filename
<<
" - "
<<
errorCode
<<
": "
<<
lodepng_error_text
(
errorCode
);
throw
std
::
runtime_error
(
errorMsg
.
str
());
}
free
(
png
);
// LodePNG decodes 16 bit PNGs in big endian format ==> Convert
#ifndef ACGL_BIG_ENDIAN
if
(
bitdepth
==
16
)
for
(
int
i
=
0
;
i
<
(
width
*
height
*
channels
*
2
);
i
+=
2
)
std
::
swap
(
image
[
i
],
image
[
i
+
1
]);
#endif
// Wrap the data in a TextureData object
data
=
SharedTextureData
(
new
TextureData
());
data
->
setData
((
GLubyte
*
)
image
);
data
->
setWidth
(
width
);
...
...
@@ -239,6 +262,11 @@ SharedTextureData loadTextureDataFromLodepng(const std::string &_filename)
// Flip
data
->
flipVertically
();
}
catch
(
std
::
runtime_error
&
error
)
{
ACGL
::
Utils
::
error
()
<<
error
.
what
()
<<
std
::
endl
;
}
return
data
;
}
...
...
@@ -626,6 +654,12 @@ bool saveTextureDataToLodepng( const SharedTextureData &_data, const std::string
return
false
;
}
// LodePNG expects 16 bit image data in big endian format ==> convert
#ifndef ACGL_BIG_ENDIAN
if
(
channelBitCount
==
16
)
for
(
int
i
=
0
;
i
<
_data
->
getWidth
()
*
_data
->
getHeight
()
*
_data
->
getNumberOfChannels
()
*
2
;
i
+=
2
)
std
::
swap
(
processedData
[
i
],
processedData
[
i
+
1
]);
#endif
LodePNGColorType
colorType
;
if
(
channelCount
==
1
)
colorType
=
LCT_GREY
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment