XIL Device Porting and Extensibility Guide
この本のみを検索
PDF 文書ファイルをダウンロードする

Sample Molecule

A

This example illustrates a molecule for performing 16-to-8 bit remapping of memory images. It implements the combined atomics convert16_8(rescale16()). The source image must be a 1-banded, XIL_SHORT image. The destination must be a 1-banded, XIL_BYTE image. This example contains a single file, Rescale16Convert16to8.cc, which implements the molecule.
Code Example A-1 Rescale16Convert16yo8.cc (1 of 6)

  //This line lets emacs recognize this as -*- C++ -*- Code  
  //------------------------------------------------------------------------  
  //  
  //  Description:  
  //      Contains the convert(rescale()) molecule for 16 bit to 8 bit conversion  
  //  (Memory to memory 16-to-8 bit remapping)  
  //  
  //  Parameters:  
  //  Source must be a 1-banded, XIL_SHORT image.  
  //  Destination must be a 1-banded, XIL_BYTE image.  
  //  
  //  Returns:  
  //  
  //  XIL_SUCCESS or XIL_FAILURE  
  //  
  //  Side Effects:  
  //  
  //  Notes:  

Code Example A-1 Rescale16Convert16yo8.cc (2 of 6)

  //  
  //  Deficiencies/ToDo:  
  //  Should be able to handle multiple bands.  
  //  
  //  
  //------------------------------------------------------------------------  
  #pragma ident "@(#)Rescale16Convert16to8.cc1.2\t94/03/23 "  
  
  #include <xil/XilDefines.h>  
  #include <xil/XilError.h>  
  #include <xil/XilImage.h>  
  #include <xil/XilOp.h>  
  #include <xil/XilRoi.h>  
  #include <xil/XilRoiList.h>  
  
  //  
  // Class definition for this molecule  
  //  
  class XilDeviceComputeTypeMemory : public XilDeviceComputeType {  
   public:  
      int Rescale16Convert16to8(XilOp* op, int count);  
      ~XilDeviceComputeTypeMemory();  
  
  };  
  
  //  
  // Declaration of molecule name and the atomic functions it  
  // implements for describeMembers routine  
  //  
  /* XILCONFIG: Rescale16Convert16to8 = convert16to8(rescale16()) */  
  
  //  
  // define for 16-bit rounding  
  //  
  #define ROUND_16(_round16_input_,_round16_output_)              \  
  {                                              \  
      float _round16_tmp_;                                           \  
                                                  \  
      if ((_round16_input_) >=0) {                                   \  

Code Example A-1 Rescale16Convert16yo8.cc (3 of 6)

          _round16_tmp_ = (_round16_input_) + 0.5;                \  
      }                                          \  
      else {                                     \  
          _round16_tmp_ = (_round16_input_) + -0.5;               \  
      }                                          \  
      if (_round16_tmp_ >= (float)MAXSHORT) {              \  
          (_round16_output_) = (MAXSHORT);                           \  
      }                                          \  
      else if (_round16_tmp_ <= (float)MINSHORT) {                   \  
          (_round16_output_) = (MINSHORT);                           \  
      }                                          \  
      else {                                     \  
          (_round16_output_) = ((Xil_signed16) _round16_tmp_);\  
      }                                          \  
  }  
  
  //  
  // the molecule  
  //  
  int  
  XilDeviceComputeTypeMemory::Rescale16Convert16to8(  
      XilOp*     op,           // a pointer into the DAG  
      int        )             // unused--the number of combined ops to be done  
  {  
      //  
      // Get the destination image from the convert16to8 op  
      //  
      XilImage*  dst = op->getDst();  
  
      //  
      // Go to the next operation on the DAG (the rescale16 op)  
      // and get the source image and the rescale values  
      //  
      op = op->getOp1();  
      XilImage*  src = op->getSrc1();  
      float *scale_value = (float *)(op->getPtrParam(1));  
      float *offset_value = (float *)(op->getPtrParam(2));  
  
      //  
      // ensure that molecule requirements are met (1 banded images, 16 to 8)  
      //  

Code Example A-1 Rescale16Convert16yo8.cc (4 of 6)

      if((src->getBands() != 1) ||  
         (dst->getBands() != 1) ||  
         (src->getDataType() != XIL_SHORT) ||  
         (dst->getDataType() != XIL_BYTE))  
          return XIL_FAILURE;  
  
      //  
      // get information about the source  
      //  
      long src_x_origin, src_y_origin;  
      src->getOrigin(&src_x_origin, &src_y_origin);  
  
      //  
      // get source's memory storage  
      XilMemoryStorageShort *short_storage;  
      short_storage = (XilMemoryStorageShort *)src->getMemoryStorage();  
      if (short_storage==NULL) {  
       // we could flag an error here, but the core will re-try with  
       // atomic operators  
          return XIL_FAILURE;  
      }  
      Xil_signed16 *src_base_addr = (Xil_signed16 *)short_storage->data;  
      unsigned long src_next_pixel = short_storage->pixel_stride;  
      unsigned long src_next_scan  = short_storage->scanline_stride;  
  
      //  
      // get information about the destination  
      //  
      long dst_x_origin, dst_y_origin;  
      dst->getOrigin(&dst_x_origin, &dst_y_origin);  
  
      //  
      // get destination's memory storage  
      XilMemoryStorageByte *byte_storage;  
      byte_storage = (XilMemoryStorageByte *)dst->getMemoryStorage();  
      if (byte_storage==NULL) {  
       // we could flag an error here, but the core will re-try with  
       // atomic operators  
          return XIL_FAILURE;  
      }  
      Xil_unsigned8 *dst_base_addr = (Xil_unsigned8 *)byte_storage->data;  
      unsigned long dst_next_pixel = byte_storage->pixel_stride;  

Code Example A-1 Rescale16Convert16yo8.cc (5 of 6)

      unsigned long dst_next_scan  = byte_storage->scanline_stride;  
  
      //  
      // get the list of intersected ROIs between source and destination  
      //  
      XilRoi* roi;  
      XilRoiList* roi_list= XiliGetRoiList(&roi,dst,src);  
      if (roi_list==NULL) {  
         // we could flag an error here, but the core will re-try with  
         // atomic operators  
         return XIL_FAILURE;  
      }  
  
      //  
      //  Now that we've intersected to determine the pixels that will  
      //  be touched in the destination, set the pixelsTouchedRoi on  
      //  the image.  
      //  
      dst->setPixelsTouchedRoi(roi);  
      dst->setPixelsTouchedRoi_flag(TRUE);  
  
      //  
      // operate on each ROI, all ROI's are guaranteed not to go outside images  
      //  
      long       x, y;  
      unsigned int x_size, y_size;  
      float scale = scale_value[0];  
      float offset = offset_value[0];  
      while (roi_list->next(&x,&y,&x_size,&y_size)) {  
          Xil_signed16    *src_scanline = src_base_addr +  
                                  ((y + src_y_origin) * src_next_scan) +  
                                  ((x + src_x_origin) * src_next_pixel);  
          Xil_signed16    *src_pixel;  
          Xil_unsigned8   *dst_scanline = dst_base_addr +  
                                  ((y + dst_y_origin) * dst_next_scan) +  
                                  ((x + dst_x_origin) * dst_next_pixel);  
          Xil_unsigned8   *dst_pixel;  
  
       //  
       // loop over each scanline in the ROI  
       //  
          do {  

Code Example A-1 Rescale16Convert16yo8.cc (6 of 6)

              // point to the first pixel of the scanline  
              src_pixel = src_scanline;  
              dst_pixel = dst_scanline;  
  
           // do the rescale-cast for each pixel in the scanline  
              int pixel_count = x_size;  
           Xil_signed16 result;  
              do {  
                  float tmp = ((float)(*src_pixel) * scale) + offset;  
       ROUND_16(tmp, result);  
                  *dst_pixel = (Xil_unsigned8) result;  
                  src_pixel += src_next_pixel;  
                  dst_pixel += dst_next_pixel;  
              } while (--pixel_count);  
  
              // move to next scanline  
              src_scanline += src_next_scan;  
              dst_scanline += dst_next_scan;  
  
          } while (--y_size);  
      }  
  
      // delete the intersected roilist  
      // (the roi stored in dest "pixelsTouchedRoi"  
      // will be destroyed by the xil core)  
      roi_list->destroy();  
  
      // molecule successfully completed  
      return XIL_SUCCESS;  
  }