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