Pixels.cpp revision 3ed852eea50f9d4cd633efb8c2b054b8e33c253
13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// This may look like C code, but it is really -*- C++ -*-
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Copyright Bob Friesenhahn, 1999, 2000, 2001, 2002, 2003
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Pixels Implementation
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy//
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#define MAGICKCORE_IMPLEMENTATION  1
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#define MAGICK_PLUSPLUS_IMPLEMENTATION 1
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Magick++/Include.h"
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include <string> // This is here to compile with Visual C++
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Magick++/Thread.h"
143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Magick++/Exception.h"
153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#include "Magick++/Pixels.h"
163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
173ed852eea50f9d4cd633efb8c2b054b8e33c253cristynamespace Magick
183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Construct pixel view using specified image.
243ed852eea50f9d4cd633efb8c2b054b8e33c253cristyMagick::Pixels::Pixels( Magick::Image &image_ )
253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  : _image(image_),
263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _view(AcquireCacheView(_image.image())),
273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _x(0),
283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _y(0),
293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _columns(0),
303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _rows(0)
313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  GetExceptionInfo( &_exception );
333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (!_view)
353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _image.throwImageException();
363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) DestroyExceptionInfo( &_exception );
373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Destroy pixel view
403ed852eea50f9d4cd633efb8c2b054b8e33c253cristyMagick::Pixels::~Pixels( void )
413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ( _view )
433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _view = DestroyCacheView( _view );
443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Transfer pixels from the image to the pixel view as defined by
483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// the specified region. Modified pixels may be subsequently
493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// transferred back to the image via sync.
503ed852eea50f9d4cd633efb8c2b054b8e33c253cristyMagick::PixelPacket* Magick::Pixels::get ( const int x_,
513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const int y_,
523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const unsigned int columns_,
533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const unsigned int rows_ )
543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _x = x_;
563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _y = y_;
573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _columns = columns_;
583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _rows = rows_;
593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  PixelPacket* pixels = GetCacheViewAuthenticPixels( _view, x_, y_, columns_, rows_,  &_exception);
613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ( !pixels )
633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    throwException( *GetCacheViewException(_view) );
643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return pixels;
663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Transfer read-only pixels from the image to the pixel view as
693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// defined by the specified region.
703ed852eea50f9d4cd633efb8c2b054b8e33c253cristyconst Magick::PixelPacket* Magick::Pixels::getConst ( const int x_, const int y_,
713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                                      const unsigned int columns_,
723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                                      const unsigned int rows_ )
733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _x = x_;
753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _y = y_;
763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _columns = columns_;
773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _rows = rows_;
783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  const PixelPacket* pixels =
803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    GetCacheViewVirtualPixels(_view, x_, y_, columns_, rows_, &_exception );
813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ( !pixels )
833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    throwException( _exception );
843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return pixels;
863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Transfers the image view pixels to the image.
893ed852eea50f9d4cd633efb8c2b054b8e33c253cristyvoid Magick::Pixels::sync ( void )
903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if( !SyncCacheViewAuthenticPixels( _view, &_exception ) )
923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    throwException(  _exception );
933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Allocate a pixel view region to store image pixels as defined
963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// by the region rectangle.  This area is subsequently transferred
973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// from the pixel view to the image via 'sync'.
983ed852eea50f9d4cd633efb8c2b054b8e33c253cristyMagick::PixelPacket* Magick::Pixels::set ( const int x_,
993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const int y_,
1003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const unsigned int columns_,
1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy					   const unsigned int rows_ )
1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
1033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _x = x_;
1043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _y = y_;
1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _columns = columns_;
1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  _rows = rows_;
1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  PixelPacket* pixels = QueueCacheViewAuthenticPixels( _view, static_cast<long>(x_), static_cast<long>(y_),
1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                                      columns_, rows_,  &_exception );
1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ( !pixels )
1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    throwException( _exception );
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return pixels;
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
1153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy// Return pixel colormap index array
1173ed852eea50f9d4cd633efb8c2b054b8e33c253cristyMagick::IndexPacket* Magick::Pixels::indexes ( void )
1183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  IndexPacket* pixel_indexes = GetCacheViewAuthenticIndexQueue( _view );
1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ( !pixel_indexes )
1223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    _image.throwImageException();
1233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return pixel_indexes;
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
126