Exemplo n.º 1
0
float ColorCalibrator::findCorrectExposure()
{
    Log::get() << Log::MESSAGE << "ColorCalibrator::" << __FUNCTION__ << " - Finding correct exposure time" << Log::endl;

    Values res;
    while (true)
    {
        _gcamera->getAttribute("shutterspeed", res);
        int status = _gcamera->capture();
        if (false == status)
        {
            Log::get() << Log::WARNING << "ColorCalibrator::" << __FUNCTION__ << " - There was an issue during capture." << Log::endl;
            return 0.f;
        }

        _gcamera->update();
        ImageBuffer img = _gcamera->get();
        ImageBufferSpec spec = _gcamera->getSpec();

        // Exposure is found from a centered area, covering 4% of the frame
        int roiSize = spec.width / 5;
        unsigned long total = roiSize * roiSize;
        unsigned long sum = 0;
        
        uint8_t* pixel = reinterpret_cast<uint8_t*>(img.data());
        for (int y = spec.height / 2 - roiSize / 2; y < spec.height / 2 + roiSize / 2; ++y)
            for (int x = spec.width / 2 - roiSize / 2; x < spec.width / 2 + roiSize / 2; ++x)
            {
                sum += (unsigned long)(255.f * (0.2126 * pixel[(x + y * spec.width) * 3]
                                              + 0.7152 * pixel[(x + y * spec.width) * 3 + 1]
                                              + 0.0722 * pixel[(x + y * spec.width) * 3 + 2]));
            }

        float meanValue = (float)sum / (float)total;
        Log::get() << Log::MESSAGE << "ColorCalibrator::" << __FUNCTION__ << " - Mean value over all channels: " << meanValue << Log::endl;

        if (meanValue < 100.f)
        {
            float speed = res[0].asFloat() * std::max(1.5f, 100.f / meanValue);
            _gcamera->setAttribute("shutterspeed", {speed});
        }
        else if (meanValue > 160.f)
        {
            float speed = res[0].asFloat() / std::max(1.5f, 160.f / meanValue);
            _gcamera->setAttribute("shutterspeed", {speed});
        }
        else
        {
            break;
        }
    }
    if (res.size() == 0)
        return 0.f;
    else
        return res[0].asFloat();
}
Exemplo n.º 2
0
ImageTGA::ImageTGA (const ImageBuffer &ibuff)
  : _header (NULL)
{
  const GLubyte *colormap = NULL;
  const GLubyte *data_ptr;

  try
    {
      _name = ibuff.filename ();
      data_ptr = ibuff.data ();
      _standardCoordSystem = true;

      // Read TGA header
      _header = reinterpret_cast<const TGA_Header *>(data_ptr);
      data_ptr += sizeof (TGA_Header) + _header->id_lenght;

      // Get image information
      getTextureInfo ();

      // Memory allocation for pixel data
      _pixels = new GLubyte[_width * _height * _components];

      // Read color map, if present
      if (_header->colormap_type)
	{
	  // NOTE: color map is stored in BGR
	  colormap = data_ptr;
	  data_ptr += _header->cm_length * (_header->cm_size >> 3);
	}

      // Decode image data
      switch (_header->image_type)
	{
	case 0:
	  // No data
	  break;

	case 1:
	  // Uncompressed 8 bits color index
	  readTGA8bits (data_ptr, colormap);
	  break;

	case 2:
	  // Uncompressed 16-24-32 bits
	  switch (_header->pixel_depth)
	    {
	    case 16:
	      readTGA16bits (data_ptr);
	      break;

	    case 24:
	      readTGA24bits (data_ptr);
	      break;

	    case 32:
	      readTGA32bits (data_ptr);
	      break;
	    }

	  break;

	case 3:
	  // Uncompressed 8 or 16 bits grayscale
	  if (_header->pixel_depth == 8)
	    readTGAgray8bits (data_ptr);
	  else // 16 bits
	    readTGAgray16bits (data_ptr);

	  break;

	case 9:
	  // RLE compressed 8 bits color index
	  readTGA8bitsRLE (data_ptr, colormap);
	  break;

	case 10:
	  // RLE compressed 16-24-32 bits
	  switch (_header->pixel_depth)
	    {
	    case 16:
	      readTGA16bitsRLE (data_ptr);
	      break;

	    case 24:
	      readTGA24bitsRLE (data_ptr);
	      break;

	    case 32:
	      readTGA32bitsRLE (data_ptr);
	      break;
	    }

	  break;

	case 11:
	  // RLE compressed 8 or 16 bits grayscale
	  if (_header->pixel_depth == 8)
	    readTGAgray8bitsRLE (data_ptr);
	  else // 16 bits
	    readTGAgray16bitsRLE (data_ptr);

	  break;

	default:
	  // Image type is not correct, free memory and quit
	  throw ImageException ("Unknown TGA image type", _name);
	}
    }