1 | // Copyright (C) 2011 Kornel Kisielewicz
|
---|
2 | // This file is part of NV Libraries.
|
---|
3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
4 |
|
---|
5 | #include "nv/gfx/image.hh"
|
---|
6 |
|
---|
7 | #include <algorithm>
|
---|
8 |
|
---|
9 | using namespace nv;
|
---|
10 |
|
---|
11 | image::image( glm::ivec2 size, size_t depth )
|
---|
12 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
13 | {
|
---|
14 | m_data = new uint8[ static_cast<uint32>( m_size.x * m_size.y ) * m_depth ];
|
---|
15 | }
|
---|
16 |
|
---|
17 | image::image( image_data* data )
|
---|
18 | : m_size( data->get_size() ), m_depth( data->get_depth() ), m_data( data->release_data() )
|
---|
19 | {
|
---|
20 | NV_ASSERT( m_data, "image created from empty image_data!" );
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | image::image( glm::ivec2 size, size_t depth, const uint8 * data, bool reversed )
|
---|
25 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
26 | {
|
---|
27 | sint32 bsize = m_size.x * m_size.y * static_cast<sint32>( m_depth );
|
---|
28 | m_data = new uint8[ bsize ];
|
---|
29 |
|
---|
30 | if ( reversed )
|
---|
31 | {
|
---|
32 | sint32 bline = m_size.x * static_cast<sint32>( m_depth );
|
---|
33 | for( int i = 0; i < m_size.y; ++i )
|
---|
34 | {
|
---|
35 | std::copy( data + i * bline, data + (i + 1) * bline, m_data + bsize - ( i + 1 ) * bline );
|
---|
36 | }
|
---|
37 |
|
---|
38 | }
|
---|
39 | else
|
---|
40 | {
|
---|
41 | std::copy( data, data + bsize, m_data );
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | void image::fill( uint8 value )
|
---|
46 | {
|
---|
47 | std::fill( m_data, m_data + m_size.x * m_size.y * (int)m_depth, value );
|
---|
48 | }
|
---|
49 |
|
---|
50 | void image::fill( region r, uint8 value, int stride )
|
---|
51 | {
|
---|
52 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
53 |
|
---|
54 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
55 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
56 |
|
---|
57 | for( int i = 0; i < r.size.y; ++i )
|
---|
58 | {
|
---|
59 | // TODO: test
|
---|
60 | std::fill( m_data + bpos + bline * i, m_data + bpos + bline * i + stride, value );
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | void image::set_region( region r, const uint8 * data, int stride )
|
---|
66 | {
|
---|
67 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
68 |
|
---|
69 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
70 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
71 |
|
---|
72 | for( int i = 0; i < r.size.y; ++i )
|
---|
73 | {
|
---|
74 | // TODO: test if same as old:
|
---|
75 | // memcpy( m_data+((r.pos.y+i)*m_size.x + r.pos.x ) * m_depth,
|
---|
76 | // data + (i*stride), r.size.x * m_depth );
|
---|
77 | std::copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | void image::set_region( region r, const image_data* idata )
|
---|
82 | {
|
---|
83 | if ( idata->get_depth() == m_depth )
|
---|
84 | {
|
---|
85 | set_region( r, idata->get_data() );
|
---|
86 | return;
|
---|
87 | }
|
---|
88 |
|
---|
89 | fill( r, 255 );
|
---|
90 |
|
---|
91 | sint32 bpos = (r.pos.y*m_size.x + r.pos.x ) * static_cast<sint32>( m_depth );
|
---|
92 | sint32 bline = m_size.x*static_cast<sint32>( m_depth );
|
---|
93 |
|
---|
94 | const uint8* data = idata->get_data();
|
---|
95 | sint32 depth = idata->get_depth();
|
---|
96 | sint32 dstride = r.size.x * static_cast<sint32>( depth );
|
---|
97 |
|
---|
98 | for( int y = 0; y < r.size.y; ++y )
|
---|
99 | {
|
---|
100 | sint32 pos = bpos + bline * y;
|
---|
101 | for( int x = 0; x < r.size.x; ++x )
|
---|
102 | {
|
---|
103 | sint32 xy = pos + x * m_depth;
|
---|
104 | for( int e = 0; e < depth; ++e )
|
---|
105 | {
|
---|
106 | m_data[ xy + e ] = data[ y*dstride + x * depth + e ];
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | image::~image()
|
---|
114 | {
|
---|
115 | delete[] m_data;
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|