rectinfo_lut_block.cpp

00001
00002 /***************************************************************************
00003  *  rectinfo_lut_block.cpp - Rectification info block for 16x16 LUT
00004  *
00005  *  Created: Wed Oct 31 15:16:50 2007
00006  *  Copyright  2007  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023
00024 #include <fvutils/rectification/rectinfo_lut_block.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 using namespace fawkes;
00029 
00030 /** @class RectificationLutInfoBlock <fvutils/rectification/rectinfo_lut_block.h>
00031  * Recitification Lookup Table Block.
00032  * This class defines a rectification lookup table info block that can be used
00033  * to define a LUT that maps rectified to unrectified pixels.
00034  * @author Tim Niemueller
00035  */
00036 
00037 /** Constructor.
00038  * @param width width of the image
00039  * @param height height of the image
00040  * @param camera camera identifier, see rectinfo_camera_t
00041  */
00042 RectificationLutInfoBlock::RectificationLutInfoBlock(uint16_t width,
00043                                                      uint16_t height,
00044                                                      uint8_t camera)
00045   : RectificationInfoBlock(FIREVISION_RECTINFO_TYPE_LUT_16x16,
00046                            camera,
00047                            sizeof(rectinfo_lut_16x16_block_header_t) +
00048                            (width * height * sizeof(rectinfo_lut_16x16_entry_t)))
00049 {
00050   _lut_block_header = (rectinfo_lut_16x16_block_header_t *)_data;
00051   _lut_data         = (rectinfo_lut_16x16_entry_t *)((char *)_data +
00052                                                      sizeof(rectinfo_lut_16x16_block_header_t));
00053
00054   _lut_block_header->width  = width;
00055   _lut_block_header->height = height;
00056 }
00057
00058 
00059 /** Copy Constructor.
00060  * It is assumed that the block actually is a rectification LUT info block. Check that
00061  * before calling this method.
00062  * @param block block to copy
00063  */
00064 RectificationLutInfoBlock::RectificationLutInfoBlock(FireVisionDataFileBlock *block)
00065   : RectificationInfoBlock(block)
00066 {
00067   _lut_block_header = (rectinfo_lut_16x16_block_header_t *)_data;
00068   _lut_data         = (rectinfo_lut_16x16_entry_t *)((char *)_data +
00069                                                      sizeof(rectinfo_lut_16x16_block_header_t));
00070 }
00071
00072
00073 void
00074 RectificationLutInfoBlock::mapping(uint16_t x, uint16_t y,
00075                                    uint16_t *to_x, uint16_t *to_y)
00076 {
00077   if ( x > _lut_block_header->width ) {
00078     throw OutOfBoundsException("RectLUT X (from)", x, 0, _lut_block_header->width);
00079   }
00080   if ( y > _lut_block_header->height ) {
00081     throw OutOfBoundsException("RectLUT Y (from)", y, 0, _lut_block_header->height);
00082   }
00083
00084   *to_x = _lut_data[y * _lut_block_header->width + x].x;
00085   *to_y = _lut_data[y * _lut_block_header->width + x].y;
00086 }
00087
00088 
00089 /** Set mapping.
00090  * @param x X pixel coordinate to get mapping for
00091  * @param y Y pixel coordinate to get mapping for
00092  * @param to_x X pixel coordinate of the unrectified image
00093  * @param to_y Y pixel coordinate of the unrectified image
00094  */
00095 void
00096 RectificationLutInfoBlock::set_mapping(uint16_t x, uint16_t y,
00097                                        uint16_t to_x, uint16_t to_y)
00098 {
00099   if ( x > _lut_block_header->width ) {
00100     throw OutOfBoundsException("RectLUT X (from)", x, 0, _lut_block_header->width);
00101   }
00102   if ( y > _lut_block_header->height ) {
00103     throw OutOfBoundsException("RectLUT Y (from)", y, 0, _lut_block_header->height);
00104   }
00105   if ( to_x > _lut_block_header->width ) {
00106     throw OutOfBoundsException("RectLUT X (to)", to_x, 0, _lut_block_header->width);
00107   }
00108   if ( to_y > _lut_block_header->height ) {
00109     throw OutOfBoundsException("RectLUT Y (to)", to_y, 0, _lut_block_header->height);
00110   }
00111
00112   _lut_data[y * _lut_block_header->width + x].x = to_x;
00113   _lut_data[y * _lut_block_header->width + x].y = to_y;
00114 }
00115
00116 
00117 /** Get width of the LUT.
00118  * @return width of LUT.
00119  */
00120 uint16_t
00121 RectificationLutInfoBlock::pixel_width()
00122 {
00123   return _lut_block_header->width;
00124 }
00125
00126 
00127 /** Get height the LUT.
00128  * @return height of LUT.
00129  */
00130 uint16_t
00131 RectificationLutInfoBlock::pixel_height()
00132 {
00133   return _lut_block_header->height;
00134 }
00135
00136 
00137 /** Get raw LUT data.
00138  * Use this to access the LUT.
00139  * @return pointer to raw LUT data
00140  */
00141 rectinfo_lut_16x16_entry_t *
00142 RectificationLutInfoBlock::lut_data()
00143 {
00144   return _lut_data;
00145 }