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