[319] | 1 | // Copyright (C) 2011-2014 ChaosForge Ltd
|
---|
[13] | 2 | // This file is part of NV Libraries.
|
---|
| 3 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
| 4 |
|
---|
[89] | 5 | #include "nv/gfx/image.hh"
|
---|
[383] | 6 | #include "nv/stl/algorithm.hh"
|
---|
[13] | 7 |
|
---|
| 8 | using namespace nv;
|
---|
| 9 |
|
---|
[376] | 10 | image::image( glm::ivec2 size, nv::size_t depth )
|
---|
[13] | 11 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 12 | {
|
---|
[128] | 13 | m_data = new uint8[ static_cast<uint32>( m_size.x * m_size.y ) * m_depth ];
|
---|
[13] | 14 | }
|
---|
| 15 |
|
---|
[90] | 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 |
|
---|
[376] | 23 | image::image( glm::ivec2 size, nv::size_t depth, const uint8 * data, bool reversed )
|
---|
[21] | 24 | : m_size( size ), m_depth( depth ), m_data( nullptr )
|
---|
| 25 | {
|
---|
[121] | 26 | sint32 bsize = m_size.x * m_size.y * static_cast<sint32>( m_depth );
|
---|
| 27 | m_data = new uint8[ bsize ];
|
---|
[21] | 28 |
|
---|
| 29 | if ( reversed )
|
---|
| 30 | {
|
---|
[121] | 31 | sint32 bline = m_size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 32 | for( int i = 0; i < m_size.y; ++i )
|
---|
[21] | 33 | {
|
---|
[383] | 34 | raw_copy( data + i * bline, data + (i + 1) * bline, m_data + bsize - ( i + 1 ) * bline );
|
---|
[21] | 35 | }
|
---|
| 36 |
|
---|
| 37 | }
|
---|
| 38 | else
|
---|
| 39 | {
|
---|
[383] | 40 | raw_copy( data, data + bsize, m_data );
|
---|
[21] | 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[13] | 44 | void image::fill( uint8 value )
|
---|
| 45 | {
|
---|
[383] | 46 | raw_fill( m_data, m_data + m_size.x * m_size.y * (int)m_depth, value );
|
---|
[13] | 47 | }
|
---|
| 48 |
|
---|
[228] | 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
|
---|
[383] | 59 | raw_fill( m_data + bpos + bline * i, m_data + bpos + bline * i + stride, value );
|
---|
[228] | 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[121] | 64 | void image::set_region( region r, const uint8 * data, int stride )
|
---|
[13] | 65 | {
|
---|
[121] | 66 | if ( stride == 0 ) stride = r.size.x * static_cast<sint32>( m_depth );
|
---|
[90] | 67 |
|
---|
[121] | 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 );
|
---|
[13] | 70 |
|
---|
[26] | 71 | for( int i = 0; i < r.size.y; ++i )
|
---|
| 72 | {
|
---|
[90] | 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 );
|
---|
[383] | 76 | raw_copy( data + i*stride, data + (i+1)*stride, m_data + bpos + bline * i );
|
---|
[26] | 77 | }
|
---|
[13] | 78 | }
|
---|
| 79 |
|
---|
[228] | 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 |
|
---|
[323] | 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;
|
---|
[228] | 92 |
|
---|
[323] | 93 | uint32 rsizex = static_cast< uint32 >( r.size.x );
|
---|
| 94 | uint32 rsizey = static_cast< uint32 >( r.size.y );
|
---|
[228] | 95 | const uint8* data = idata->get_data();
|
---|
[323] | 96 | size_t depth = idata->get_depth();
|
---|
| 97 | uint32 dstride = rsizex * depth;
|
---|
[228] | 98 |
|
---|
[323] | 99 | for( uint32 y = 0; y < rsizey; ++y )
|
---|
[228] | 100 | {
|
---|
[323] | 101 | uint32 pos = bpos + bline * y;
|
---|
| 102 | for( uint32 x = 0; x < rsizex; ++x )
|
---|
[228] | 103 | {
|
---|
[323] | 104 | uint32 xy = pos + x * m_depth;
|
---|
| 105 | for( size_t e = 0; e < depth; ++e )
|
---|
[228] | 106 | {
|
---|
| 107 | m_data[ xy + e ] = data[ y*dstride + x * depth + e ];
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
[13] | 114 | image::~image()
|
---|
| 115 | {
|
---|
| 116 | delete[] m_data;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[21] | 119 |
|
---|