Home | History | Annotate | Download | only in mosaic
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 ///////////////////////////////////////////////////
     18 // Blend.h
     19 // $Id: Blend.h,v 1.23 2011/06/24 04:22:14 mbansal Exp $
     20 
     21 #ifndef BLEND_H
     22 #define BLEND_H
     23 
     24 #include "MosaicTypes.h"
     25 #include "Pyramid.h"
     26 #include "Delaunay.h"
     27 
     28 #define BLEND_RANGE_DEFAULT 6
     29 #define BORDER 8
     30 
     31 // Percent of total mosaicing time spent on each of the following operations
     32 const float TIME_PERCENT_ALIGN = 20.0;
     33 const float TIME_PERCENT_BLEND = 75.0;
     34 const float TIME_PERCENT_FINAL = 5.0;
     35 
     36 // This threshold determines the minimum separation between the image centers
     37 // of the input image frames for them to be accepted for blending in the
     38 // STRIP_TYPE_WIDE mode.
     39 const float STRIP_SEPARATION_THRESHOLD_PXLS = 10;
     40 
     41 // This threshold determines the number of pixels on either side of the strip
     42 // to cross-fade using the images contributing to each seam.
     43 const float STRIP_CROSS_FADE_WIDTH_PXLS = 2;
     44 // This specifies the maximum pyramid level to which cross-fading is applied.
     45 // The original image resolution is Level-0, half of that size is Level-1 and
     46 // so on. BLEND_RANGE_DEFAULT specifies the number of pyramid levels used by
     47 // the blending algorithm.
     48 const int STRIP_CROSS_FADE_MAX_PYR_LEVEL = 2;
     49 
     50 /**
     51  *  Class for pyramid blending a mosaic.
     52  */
     53 class Blend {
     54 
     55 public:
     56 
     57   static const int BLEND_TYPE_NONE    = -1;
     58   static const int BLEND_TYPE_FULL    = 0;
     59   static const int BLEND_TYPE_PAN     = 1;
     60   static const int BLEND_TYPE_CYLPAN  = 2;
     61   static const int BLEND_TYPE_HORZ   = 3;
     62 
     63   static const int STRIP_TYPE_THIN      = 0;
     64   static const int STRIP_TYPE_WIDE      = 1;
     65 
     66   static const int BLEND_RET_ERROR        = -1;
     67   static const int BLEND_RET_OK           = 0;
     68   static const int BLEND_RET_ERROR_MEMORY = 1;
     69   static const int BLEND_RET_CANCELLED    = -2;
     70 
     71   Blend();
     72   ~Blend();
     73 
     74   int initialize(int blendingType, int stripType, int frame_width, int frame_height);
     75 
     76   int runBlend(MosaicFrame **frames, MosaicFrame **rframes, int frames_size, ImageType &imageMosaicYVU,
     77         int &mosaicWidth, int &mosaicHeight, float &progress, bool &cancelComputation);
     78 
     79 protected:
     80 
     81   PyramidShort *m_pFrameYPyr;
     82   PyramidShort *m_pFrameUPyr;
     83   PyramidShort *m_pFrameVPyr;
     84 
     85   PyramidShort *m_pMosaicYPyr;
     86   PyramidShort *m_pMosaicUPyr;
     87   PyramidShort *m_pMosaicVPyr;
     88 
     89   CDelaunay m_Triangulator;
     90   CSite *m_AllSites;
     91 
     92   BlendParams m_wb;
     93 
     94   // Height and width of individual frames
     95   int width, height;
     96 
     97    // Height and width of mosaic
     98   unsigned short Mwidth, Mheight;
     99 
    100   // Helper functions
    101   void FrameToMosaic(double trs[3][3], double x, double y, double &wx, double &wy);
    102   void MosaicToFrame(double trs[3][3], double x, double y, double &wx, double &wy);
    103   void FrameToMosaicRect(int width, int height, double trs[3][3], BlendRect &brect);
    104   void ClipBlendRect(CSite *csite, BlendRect &brect);
    105   void AlignToMiddleFrame(MosaicFrame **frames, int frames_size);
    106 
    107   int  DoMergeAndBlend(MosaicFrame **frames, int nsite,  int width, int height, YUVinfo &imgMos, MosaicRect &rect, MosaicRect &cropping_rect, float &progress, bool &cancelComputation);
    108   void ComputeMask(CSite *csite, BlendRect &vcrect, BlendRect &brect, MosaicRect &rect, YUVinfo &imgMos, int site_idx);
    109   void ProcessPyramidForThisFrame(CSite *csite, BlendRect &vcrect, BlendRect &brect, MosaicRect &rect, YUVinfo &imgMos, double trs[3][3], int site_idx);
    110 
    111   int  FillFramePyramid(MosaicFrame *mb);
    112 
    113   // TODO: need to add documentation about the parameters
    114   void ComputeBlendParameters(MosaicFrame **frames, int frames_size, int is360);
    115   void SelectRelevantFrames(MosaicFrame **frames, int frames_size,
    116         MosaicFrame **relevant_frames, int &relevant_frames_size);
    117 
    118   int  PerformFinalBlending(YUVinfo &imgMos, MosaicRect &cropping_rect);
    119   void CropFinalMosaic(YUVinfo &imgMos, MosaicRect &cropping_rect);
    120 
    121 private:
    122    static const float LIMIT_SIZE_MULTIPLIER = 5.0f * 2.0f;
    123    static const float LIMIT_HEIGHT_MULTIPLIER = 2.5f;
    124    int MosaicSizeCheck(float sizeMultiplier, float heightMultiplier);
    125 };
    126 
    127 #endif
    128