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