| 1 | /*========================================================================= |
| 2 | |
| 3 | Program: librle, a minimal RLE library for DICOM |
| 4 | |
| 5 | Copyright (c) 2014 Mathieu Malaterre |
| 6 | All rights reserved. |
| 7 | See Copyright.txt for details. |
| 8 | |
| 9 | This software is distributed WITHOUT ANY WARRANTY; without even |
| 10 | the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 | PURPOSE. See the above copyright notice for more information. |
| 12 | |
| 13 | =========================================================================*/ |
| 14 | #include "info.h"cxx . . $ `<file>/Modules/ThirdParty/GDCM/src/gdcm/Utilities/gdcmrle/info.h`/ |
| 15 | |
| 16 | #include <stdexcept> |
| 17 | #include <cassert> |
| 18 | |
| 19 | namespace rlecxx . . $ rle/ |
| 20 | { |
| 21 | |
| 22 | // The byte of a multibyte number with the greatest importance: that is, the |
| 23 | // byte stored first on a big-endian system or last on a little-endian |
| 24 | // system. |
| 25 | |
| 26 | pixel_infocxx . . $ rle/pixel_info#::pixel_infocxx . . $ rle/pixel_info#pixel_info(8fbe6856f5410d23).( |
| 27 | unsigned char nc, |
| 28 | unsigned char bpp |
| 29 | ): |
| 30 | number_componentscxx . . $ rle/pixel_info#number_components.(nc), |
| 31 | bits_per_pixelcxx . . $ rle/pixel_info#bits_per_pixel.(bpp) |
| 32 | { |
| 33 | if ( nc != 1 && nc != 3 ) |
| 34 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( "invalid samples per pixel" ); |
| 35 | if( bpp != 8 && bpp != 16 && bpp != 32 ) |
| 36 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( "invalid bits per pixel" ); |
| 37 | } |
| 38 | |
| 39 | static inline int compute_num_segments_implcxx . . $ rle/compute_num_segments_impl(9b79fb6aee4c0440).( int nc, int bpp ) |
| 40 | { |
| 41 | // pre |
| 42 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!( bpp % 8 == 0 && (nc == 1 || nc == 3) ); |
| 43 | const int mult = bpp / 8; |
| 44 | const int res = nc * mult; |
| 45 | // post |
| 46 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!( res <= 12 && res > 0 ); |
| 47 | return res; |
| 48 | } |
| 49 | |
| 50 | bool pixel_infocxx . . $ rle/pixel_info#::check_num_segmentscxx . . $ rle/pixel_info#check_num_segments(83c267b20bac841a).( const int num_segments ) |
| 51 | { |
| 52 | bool ok = false; |
| 53 | // DICOM restrict number of possible values: |
| 54 | switch( num_segments ) |
| 55 | { |
| 56 | case 1: // 1 -> Grayscale / 8bits |
| 57 | case 2: // 2 -> 16bits |
| 58 | case 3: // 3 -> RGB / 8bits |
| 59 | case 4: // 4 -> 32bits |
| 60 | case 6: // 6 -> RGB / 16bits |
| 61 | case 12: // 12 -> RGB / 32 bits |
| 62 | ok = true; |
| 63 | break; |
| 64 | } |
| 65 | return ok; |
| 66 | } |
| 67 | |
| 68 | static inline void compute_nc_bpp_from_num_segmentscxx . . $ rle/compute_nc_bpp_from_num_segments(4b55c833e9dbf0bc).( const int num_segments, int & nc, int & bpp ) |
| 69 | { |
| 70 | // pre condition: |
| 71 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!( pixel_info::check_num_segments( num_segments ) ); |
| 72 | |
| 73 | if( num_segments % 3 == 0 ) |
| 74 | { |
| 75 | nc = 3; |
| 76 | bpp = ( num_segments / 3 ) * 8; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | nc = 1; |
| 81 | bpp = num_segments; |
| 82 | } |
| 83 | |
| 84 | // post condition |
| 85 | assertcxx . . $ `<external>/4398592474888995393/assert.h:50:10`!( compute_num_segments_impl( nc, bpp ) == num_segments ); |
| 86 | } |
| 87 | |
| 88 | pixel_infocxx . . $ rle/pixel_info#::pixel_infocxx . . $ rle/pixel_info#pixel_info(d4f767463ce0a6b3).( int num_segments ) |
| 89 | { |
| 90 | int nc, bpp; |
| 91 | compute_nc_bpp_from_num_segmentscxx . . $ rle/compute_nc_bpp_from_num_segments(4b55c833e9dbf0bc).( num_segments, nc, bpp ); |
| 92 | number_componentscxx . . $ rle/pixel_info#number_components. = nc; |
| 93 | bits_per_pixelcxx . . $ rle/pixel_info#bits_per_pixel. = bpp; |
| 94 | } |
| 95 | |
| 96 | int pixel_infocxx . . $ rle/pixel_info#::get_number_of_bits_per_pixelcxx . . $ rle/pixel_info#get_number_of_bits_per_pixel(455f465bc33b4cdf).() const |
| 97 | { |
| 98 | return bits_per_pixelcxx . . $ rle/pixel_info#bits_per_pixel.; |
| 99 | } |
| 100 | |
| 101 | int pixel_infocxx . . $ rle/pixel_info#::get_number_of_componentscxx . . $ rle/pixel_info#get_number_of_components(455f465bc33b4cdf).() const |
| 102 | { |
| 103 | return number_componentscxx . . $ rle/pixel_info#number_components.; |
| 104 | } |
| 105 | |
| 106 | // Segments are organised as follow, eg 16bits RGB: |
| 107 | /* |
| 108 | NumSeg: 6 |
| 109 | Offset: 64 // Most Significant / Red |
| 110 | Offset: 12752 // Least Significant / Red |
| 111 | Offset: 62208 // Most Significant / Green |
| 112 | Offset: 74896 // Least Significant / Green |
| 113 | Offset: 124352 // Most Significant / Blue |
| 114 | Offset: 137040 // Least Significant / Blue |
| 115 | Offset: 0 |
| 116 | Offset: 0 |
| 117 | Offset: 0 |
| 118 | Offset: 0 |
| 119 | Offset: 0 |
| 120 | Offset: 0 |
| 121 | Offset: 0 |
| 122 | Offset: 0 |
| 123 | Offset: 0 |
| 124 | */ |
| 125 | |
| 126 | int pixel_infocxx . . $ rle/pixel_info#::compute_num_segmentscxx . . $ rle/pixel_info#compute_num_segments(455f465bc33b4cdf).() const |
| 127 | { |
| 128 | return compute_num_segments_implcxx . . $ rle/compute_num_segments_impl(9b79fb6aee4c0440).(number_componentscxx . . $ rle/pixel_info#number_components., bits_per_pixelcxx . . $ rle/pixel_info#bits_per_pixel.); |
| 129 | } |
| 130 | |
| 131 | image_infocxx . . $ rle/image_info#::image_infocxx . . $ rle/image_info#image_info(2f540234122711ac).(int w, int h, pixel_infocxx . . $ rle/pixel_info# const & pi, bool pc, bool le): |
| 132 | widthcxx . . $ rle/image_info#width.(w), |
| 133 | heightcxx . . $ rle/image_info#height.(h), |
| 134 | pixcxx . . $ rle/image_info#pix.(pi), |
| 135 | planarconfigurationcxx . . $ rle/image_info#planarconfiguration.(pc), |
| 136 | littleendiancxx . . $ rle/image_info#littleendian.(le) |
| 137 | { |
| 138 | if( widthcxx . . $ rle/image_info#width. < 0 || heightcxx . . $ rle/image_info#height. < 0 ) |
| 139 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( "invalid dimensions" ); |
| 140 | if( pc && pixcxx . . $ rle/image_info#pix..get_number_of_componentscxx . . $ rle/pixel_info#get_number_of_components(455f465bc33b4cdf).() != 3 ) |
| 141 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( "invalid planar configuration" ); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | } // end namespace rle |