[opengtl-commits] [700] implement pixel/setPixel

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 700
Author:   cyrille
Date:     2009-03-26 14:39:36 +0100 (Thu, 26 Mar 2009)

Log Message:
-----------
implement pixel/setPixel

Modified Paths:
--------------
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
    trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h
    trunk/OpenGTL/OpenShiva/OpenShiva/wrappers/PixelWrap_p.h


Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2009-03-26 12:41:20 UTC (rev 699)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.cpp	2009-03-26 13:39:36 UTC (rev 700)
@@ -80,6 +80,15 @@
   return func_llvm_memcpy_i32;
 }
 
+llvm::Value* CodeGenerator::accessPixelAlphaPos( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel)
+{
+  SHIVA_DEBUG( *_pixel );
+  std::vector<llvm::Value*> indexes;
+  indexes.push_back( _gc.codeGenerator()->integerToConstant(0));
+  indexes.push_back( _gc.codeGenerator()->integerToConstant(PixelWrap::POS_ALPHAPOS ));
+  return llvm::GetElementPtrInst::Create( _pixel, indexes.begin(), indexes.end(), "", _currentBlock );
+}
+
 llvm::Value* CodeGenerator::accessPixelDataPtr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel)
 {
   SHIVA_DEBUG( *_pixel );
@@ -619,3 +628,110 @@
     llvm::ReturnInst::Create( px_var, currentBlock);
   return func;
 }
+
+llvm::Function* CodeGenerator::generatePixelSetAlpha( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _pixelType, int channelsNb)
+{
+  // void setAlpha(PixelWrap* self, float alpha)
+  // {
+  //   int alphaPos = self->alphaPos;
+  //   if(alphaPos != -1 ) self->data[alphaPos] = alpha;
+  //   return ;
+  // }
+  GTLCore::CodeGenerator codeGenerator( _moduleData );
+    
+  llvm::Function* func = codeGenerator.createFunction(
+      Wrapper::pixel_wrap_set_alpha_type(_moduleData->typesManager(), _pixelType),
+      "pixel_wrap_set_alpha" + GTLCore::String::number( ++imageSampleNearestId) );
+  GTLCore::GenerationContext generationContext( &codeGenerator, func, 0, _moduleData );
+  // Get the args.
+    llvm::Function::arg_iterator arg_it = func->arg_begin();
+    //   PixelWrap* self = first arg;
+    llvm::Value* arg_self = arg_it;
+    //   float alpha = second arg;
+    ++arg_it;
+    llvm::Value* arg_alpha = arg_it;
+  //  {
+    llvm::BasicBlock* firstBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( firstBlock );
+    
+  //   int alphaPos = self->alphaPos;
+    llvm::Value* alphaPos = new llvm::LoadInst( accessPixelAlphaPos( generationContext, firstBlock, arg_self), "", firstBlock);
+
+  // alphaPos != -1
+    llvm::Value* compar = GTLCore::CodeGenerator::createDifferentExpression(firstBlock, alphaPos, GTLCore::Type::Integer32, GTLCore::CodeGenerator::integerToConstant(-1), GTLCore::Type::Integer32 );
+  
+  // self->data[alphaPos] = alpha;
+    llvm::BasicBlock* alphaPosBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( alphaPosBlock );
+    // self->data
+    llvm::Value* pixelDataPtr = accessPixelDataPtr( generationContext, alphaPosBlock, arg_self );
+    // self->data[alphaPos]
+    if( channelsNb == 1 ) {
+      new llvm::StoreInst( arg_alpha, pixelDataPtr, "", alphaPosBlock );
+    } else {
+      llvm::Value* floatVec = new llvm::LoadInst( pixelDataPtr, "", alphaPosBlock );
+      floatVec = llvm::InsertElementInst::Create( floatVec, arg_alpha, alphaPos, "", alphaPosBlock);
+      new llvm::StoreInst( floatVec, pixelDataPtr, "", alphaPosBlock );
+    }
+  
+  // return;
+    llvm::BasicBlock* returnBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( returnBlock );
+    llvm::ReturnInst::Create(returnBlock);
+    
+    GTLCore::CodeGenerator::createIfStatement(firstBlock, compar, GTLCore::Type::Boolean, alphaPosBlock, alphaPosBlock, returnBlock);
+    
+  return func;
+}
+llvm::Function* CodeGenerator::generatePixelAlpha( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _pixelType, int channelsNb)
+{
+  // float alpha(PixelWrap* self)
+  // {
+  //   int alphaPos = self->alphaPos;
+  //   if(alphaPos == -1 ) return 1.0;
+  //   return self->data[alphaPos];
+  // }
+  
+  GTLCore::CodeGenerator codeGenerator( _moduleData );
+    
+  llvm::Function* func = codeGenerator.createFunction(
+      Wrapper::pixel_wrap_alpha_type(_moduleData->typesManager(), _pixelType),
+      "pixel_wrap_alpha" + GTLCore::String::number( ++imageSampleNearestId) );
+  GTLCore::GenerationContext generationContext( &codeGenerator, func, 0, _moduleData );
+
+  // Get the args.
+    llvm::Function::arg_iterator arg_it = func->arg_begin();
+    //   PixelWrap* self = first arg;
+    llvm::Value* arg_self = arg_it;
+  
+  //  {
+    llvm::BasicBlock* firstBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( firstBlock );
+    
+  //   int alphaPos = self->alphaPos;
+    llvm::Value* alphaPos = new llvm::LoadInst( accessPixelAlphaPos( generationContext, firstBlock, arg_self), "read alpha pos", firstBlock);
+
+  // alphaPos == -1
+    llvm::Value* compar = GTLCore::CodeGenerator::createEqualExpression(firstBlock, alphaPos, GTLCore::Type::Integer32, GTLCore::CodeGenerator::integerToConstant(-1), GTLCore::Type::Integer32 );
+  // return 1.0
+    llvm::BasicBlock* noAlphaPosBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( noAlphaPosBlock );
+    llvm::ReturnInst::Create( GTLCore::CodeGenerator::floatToConstant(1.0), noAlphaPosBlock );
+  // return self->data[alphaPos];
+    llvm::BasicBlock* alphaPosBlock = llvm::BasicBlock::Create();
+    func->getBasicBlockList().push_back( alphaPosBlock );
+    // self->data
+    llvm::Value* pixelDataPtr = accessPixelDataPtr( generationContext, alphaPosBlock, arg_self );
+    // self->data[alphaPos]
+    llvm::Value* floatVec = new llvm::LoadInst( pixelDataPtr, "", alphaPosBlock );
+    llvm::Value* floatValue = 0;
+    if( channelsNb == 1 ) {
+      floatValue = floatVec;
+    } else {
+      floatValue = new llvm::ExtractElementInst( floatVec, alphaPos, "", alphaPosBlock );
+    }
+    llvm::ReturnInst::Create( floatValue, alphaPosBlock );
+    
+    GTLCore::CodeGenerator::createIfStatement(firstBlock, compar, GTLCore::Type::Boolean, noAlphaPosBlock, noAlphaPosBlock, alphaPosBlock);
+  return func;
+}

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2009-03-26 12:41:20 UTC (rev 699)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/CodeGenerator_p.h	2009-03-26 13:39:36 UTC (rev 700)
@@ -60,6 +60,14 @@
        */
       static llvm::Function* generateImageSampleNearest( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _imageType, const GTLCore::Type* _type );
       /**
+       * Generate a setAlpha function.
+       */
+      static llvm::Function* generatePixelSetAlpha( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _pixelType, int channelsNb);
+      /**
+       * Generate a alpha function.
+       */
+      static llvm::Function* generatePixelAlpha( GTLCore::ModuleData* _moduleData, const GTLCore::Type* _pixelType, int channelsNb);
+      /**
        * @return a pointer to the data pointer of a pixel (as a pointer to a vector of float)
        */
       static llvm::Value* accessPixelDataPtr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
@@ -68,6 +76,7 @@
        */
       static llvm::Value* accessPixelDataAsF32Ptr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
     private:
+      static llvm::Value* accessPixelAlphaPos( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
       static llvm::Function* createMemCpyFunction( llvm::Module* _module );
       static llvm::Value* accessPixelDataAsU8Ptr( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _pixel);
       static llvm::Value* callMemcpy( GTLCore::GenerationContext& _gc, llvm::BasicBlock* _currentBlock, llvm::Value* _dst, llvm::Value* _src, llvm::Value* _n );

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2009-03-26 12:41:20 UTC (rev 699)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.cpp	2009-03-26 13:39:36 UTC (rev 700)
@@ -117,6 +117,22 @@
   createRegionType( _module, _typesManager );
 }
 
+
+llvm::FunctionType* Wrapper::pixel_wrap_set_alpha_type( GTLCore::TypesManager* _typesManager, const GTLCore::Type* _pixelType)
+{
+  std::vector<const llvm::Type*> functionArgs;
+  functionArgs.push_back( _pixelType->d->pointerType() );
+  functionArgs.push_back( llvm::Type::FloatTy );
+  return llvm::FunctionType::get( llvm::Type::VoidTy, functionArgs, false);
+}
+
+llvm::FunctionType* Wrapper::pixel_wrap_alpha_type( GTLCore::TypesManager* _typesManager, const GTLCore::Type* _pixelType)
+{
+  std::vector<const llvm::Type*> functionArgs;
+  functionArgs.push_back( _pixelType->d->pointerType() );
+  return llvm::FunctionType::get( llvm::Type::FloatTy, functionArgs, false);
+}
+
 const GTLCore::Type* Wrapper::createPixelType( GTLCore::ModuleData* _module, GTLCore::TypesManager* _typesManager, GTLCore::ConvertCenter* _convertCenter, const GTLCore::String& _suffix, int _channels )
 {
   //---------------------- WARNING ----------------------//
@@ -130,10 +146,26 @@
   pixelDataMembers.push_back( GTLCore::Type::StructDataMember( "data", vecType ) );
   
   pixelDataMembers.push_back( GTLCore::Type::StructDataMember( "coord", _typesManager->getVector( GTLCore::Type::Float, 2 ) ) );
+  pixelDataMembers.push_back( GTLCore::Type::StructDataMember( "", GTLCore::Type::Integer32 ) );
   
+  
+  
   const GTLCore::Type* type = _typesManager->d->createStructure( "pixel" + _suffix, pixelDataMembers );
   type->d->setVisitor( PixelVisitor::instance() );
   
+  type->d->addFunctionMember( GTLCore::Type::StructFunctionMember(
+          GTLCore::Function::Private::createInternalFunction(
+                _module, "setAlpha",
+                CodeGenerator::generatePixelSetAlpha( _module, type, _channels ),
+                GTLCore::Type::Void, 2,
+                type, GTLCore::Type::Float ) ) );
+  type->d->addFunctionMember( GTLCore::Type::StructFunctionMember(
+          GTLCore::Function::Private::createInternalFunction(
+                _module, "alpha",
+                CodeGenerator::generatePixelAlpha( _module, type, _channels ),
+                GTLCore::Type::Float, 1,
+                type ) ) );
+
   _convertCenter->addAutoConversion( type, vecType );
   
   return type;

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h	2009-03-26 12:41:20 UTC (rev 699)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/Wrapper_p.h	2009-03-26 13:39:36 UTC (rev 700)
@@ -60,6 +60,8 @@
       static llvm::FunctionType* image_wrap_sample_nearest_type( GTLCore::TypesManager* _typesManager, const GTLCore::Type* _imageType, const GTLCore::Type* _pixelType );
       static llvm::FunctionType* image_wrap_mem_to_vec_float_type( GTLCore::TypesManager* _typesManager, int _channels );
       static llvm::FunctionType* image_wrap_vec_float_to_mem_type( GTLCore::TypesManager* _typesManager, int _channels );
+      static llvm::FunctionType* pixel_wrap_set_alpha_type( GTLCore::TypesManager* _typesManager, const GTLCore::Type* _pixelType);
+      static llvm::FunctionType* pixel_wrap_alpha_type( GTLCore::TypesManager* _typesManager, const GTLCore::Type* _pixelType);
       /**
        * @return the vectory type associated to that number of _channels
        */

Modified: trunk/OpenGTL/OpenShiva/OpenShiva/wrappers/PixelWrap_p.h
===================================================================
--- trunk/OpenGTL/OpenShiva/OpenShiva/wrappers/PixelWrap_p.h	2009-03-26 12:41:20 UTC (rev 699)
+++ trunk/OpenGTL/OpenShiva/OpenShiva/wrappers/PixelWrap_p.h	2009-03-26 13:39:36 UTC (rev 700)
@@ -31,13 +31,16 @@
   STRUCT_HEADER
   void* data;
   void* coord;
+  int alphapos;
   enum PixelIndexes {
     INDEX_DATA = 0,
-    INDEX_COORD = 1
+    INDEX_COORD = 1,
+    INDEX_ALPHAPOS = 2
   };
   enum PixelPoses {
     POS_DATA = STRUCT_FIRST_ELEMENT + INDEX_DATA,
-    POS_COORD = STRUCT_FIRST_ELEMENT + INDEX_COORD
+    POS_COORD = STRUCT_FIRST_ELEMENT + INDEX_COORD,
+    POS_ALPHAPOS = STRUCT_FIRST_ELEMENT + INDEX_ALPHAPOS
   };
 };
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/