1 /* This file is part of the Pangolin Project. 2 * http://github.com/stevenlovegrove/Pangolin 3 * 4 * Copyright (c) 2011 Steven Lovegrove 5 * 6 * Permission is hereby granted, free of charge, to any person 7 * obtaining a copy of this software and associated documentation 8 * files (the "Software"), to deal in the Software without 9 * restriction, including without limitation the rights to use, 10 * copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following 13 * conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 * OTHER DEALINGS IN THE SOFTWARE. 26 */ 27 28 #pragma once 29 30 #include <pangolin/image/image.h> 31 #include <pangolin/image/pixel_format.h> 32 33 namespace pangolin { 34 35 class PANGOLIN_EXPORT StreamInfo 36 { 37 public: StreamInfo()38 inline StreamInfo() 39 : fmt(PixelFormatFromString("GRAY8")) {} 40 StreamInfo(PixelFormat fmt,const Image<unsigned char> img_offset)41 inline StreamInfo(PixelFormat fmt, const Image<unsigned char> img_offset ) 42 : fmt(fmt), img_offset(img_offset) {} 43 44 inline StreamInfo(PixelFormat fmt, size_t w, size_t h, size_t pitch, unsigned char* offset = 0) fmt(fmt)45 : fmt(fmt), img_offset(offset,w,h,pitch) {} 46 47 //! Format representing how image is laid out in memory PixFormat()48 inline const PixelFormat &PixFormat() const { return fmt; } 49 50 //! Image width in pixels Width()51 inline size_t Width() const { return img_offset.w; } 52 53 //! Image height in pixels Height()54 inline size_t Height() const { return img_offset.h; } 55 Aspect()56 inline double Aspect() const { return (double)Width() / (double)Height(); } 57 58 //! Pitch: Number of bytes between one image row and the next Pitch()59 inline size_t Pitch() const { return img_offset.pitch; } 60 61 //! Number of contiguous bytes in memory that the image occupies RowBytes()62 inline size_t RowBytes() const { 63 // Row size without padding 64 return (fmt.bpp*img_offset.w)/8; 65 } 66 67 //! Returns true iff image contains padding or stridded access 68 //! This implies that the image data is not contiguous in memory. IsPitched()69 inline bool IsPitched() const { 70 return Pitch() != RowBytes(); 71 } 72 73 //! Number of contiguous bytes in memory that the image occupies SizeBytes()74 inline size_t SizeBytes() const { 75 return (img_offset.h-1) * img_offset.pitch + RowBytes(); 76 } 77 78 //! Offset in bytes relative to start of frame buffer Offset()79 inline unsigned char* Offset() const { return img_offset.ptr; } 80 81 //! Return Image wrapper around raw base pointer StreamImage(unsigned char * base_ptr)82 inline Image<unsigned char> StreamImage(unsigned char* base_ptr) const { 83 Image<unsigned char> img = img_offset; 84 img.ptr += (size_t)base_ptr; 85 return img; 86 } 87 88 //! Return Image wrapper around raw base pointer StreamImage(const unsigned char * base_ptr)89 inline const Image<unsigned char> StreamImage(const unsigned char* base_ptr) const { 90 Image<unsigned char> img = img_offset; 91 img.ptr += (size_t)base_ptr; 92 return img; 93 } 94 95 protected: 96 PixelFormat fmt; 97 Image<unsigned char> img_offset; 98 }; 99 100 } 101