1 //------------------------------------------------------------
2 #include "tfxparam.h"
3 #include "stdfx.h"
4 
5 #include "ino_common.h"
6 /* tnzbase --> Source Files --> tfx --> binaryFx.cppを参照 */
7 class ino_blend_hard_light final : public TBlendForeBackRasterFx {
8   FX_PLUGIN_DECLARATION(ino_blend_hard_light)
9   TRasterFxPort m_up;
10   TRasterFxPort m_down;
11   TDoubleParamP m_opacity;
12   TBoolParamP m_clipping_mask;
13 
14 public:
ino_blend_hard_light()15   ino_blend_hard_light()
16       : m_opacity(1.0 * ino::param_range()), m_clipping_mask(true) {
17     addInputPort("Fore", this->m_up);
18     addInputPort("Back", this->m_down);
19     bindParam(this, "opacity", this->m_opacity);
20     bindParam(this, "clipping_mask", this->m_clipping_mask);
21     this->m_opacity->setValueRange(0, 1.0 * ino::param_range());
22   }
~ino_blend_hard_light()23   ~ino_blend_hard_light() {}
canHandle(const TRenderSettings & rs,double frame)24   bool canHandle(const TRenderSettings &rs, double frame) override {
25     return true;
26   }
doGetBBox(double frame,TRectD & bBox,const TRenderSettings & rs)27   bool doGetBBox(double frame, TRectD &bBox,
28                  const TRenderSettings &rs) override {
29     TRectD up_bx;
30     const bool up_sw =
31         (m_up.isConnected() ? m_up->doGetBBox(frame, up_bx, rs) : false);
32     TRectD dn_bx;
33     const bool dn_sw =
34         (m_down.isConnected() ? m_down->doGetBBox(frame, dn_bx, rs) : false);
35     if (up_sw && dn_sw) {
36       bBox = up_bx + dn_bx;
37       return !bBox.isEmpty();
38     } else if (up_sw) {
39       bBox = up_bx;
40       return true;
41     } else if (dn_sw) {
42       bBox = dn_bx;
43       return true;
44     } else {
45       bBox = TRectD();
46       return false;
47     }
48   }
49   // TRect getInvalidRect(const TRect &max) {return max;}
50   // void doSetParam(const std::string &name, const TParamP &param) {}
getMemoryRequirement(const TRectD & rect,double frame,const TRenderSettings & rs)51   int getMemoryRequirement(const TRectD &rect, double frame,
52                            const TRenderSettings &rs) override {
53     return TRasterFx::memorySize(rect, rs.m_bpp);
54   }
55 
doDryCompute(TRectD & rect,double frame,const TRenderSettings & rs)56   void doDryCompute(TRectD &rect, double frame,
57                     const TRenderSettings &rs) override {
58     this->dryComputeUpAndDown(rect, frame, rs, false);
59   }
60   void doCompute(TTile &tile, double frame, const TRenderSettings &rs) override;
61   void computeUpAndDown(TTile &tile, double frame, const TRenderSettings &rs,
62                         TRasterP &dn_ras, TRasterP &up_ras,
63                         bool upComputesWholeTile = false);
64   void dryComputeUpAndDown(TRectD &rect, double frame,
65                            const TRenderSettings &rs,
66                            bool upComputesWholeTile = false
67                            /*
68            upComputesWholeTile は Screen, Min, Blendでtrueにして使用している。
69            */
70                            );
71 };
72 FX_PLUGIN_IDENTIFIER(ino_blend_hard_light, "inoHardLightFx");
73 //------------------------------------------------------------
74 namespace {
75 /* より大きな四角エリアにPixel整数値で密着する */
makeRectCoherent(TRectD & rect,const TPointD & pos)76 void makeRectCoherent(TRectD &rect, const TPointD &pos) {
77   rect -= pos;
78   rect.x0 = tfloor(rect.x0); /* ((x)<(int)(x)? (int)(x)-1: (int)(x))*/
79   rect.y0 = tfloor(rect.y0);
80   rect.x1 = tceil(rect.x1); /* ((int)(x)<(x)? (int)(x)+1: (int)(x))*/
81   rect.y1 = tceil(rect.y1);
82   rect += pos;
83 }
84 }
computeUpAndDown(TTile & tile,double frame,const TRenderSettings & rs,TRasterP & dn_ras,TRasterP & up_ras,bool upComputesWholeTile)85 void ino_blend_hard_light::computeUpAndDown(TTile &tile, double frame,
86                                             const TRenderSettings &rs,
87                                             TRasterP &dn_ras, TRasterP &up_ras,
88                                             bool upComputesWholeTile) {
89   /* ------ サポートしていないPixelタイプはエラーを投げる --- */
90   if (!((TRaster32P)tile.getRaster()) && !((TRaster64P)tile.getRaster())) {
91     throw TRopException("unsupported input pixel type");
92   }
93   /*
94 m_down,m_upは繋がっている方があればそれを表示する
95 両方とも接続していれば合成処理する
96 表示スイッチを切ってあるならm_upを表示する
97 fxをreplaceすると、
98   m_source   --> m_up  (=port0)
99   m_refernce --> m_down(=port1)
100 となる
101 */
102   const bool up_is = (this->m_up.isConnected() &&
103                       this->m_up.getFx()->getTimeRegion().contains(frame));
104   const bool down_is = (this->m_down.isConnected() &&
105                         this->m_down.getFx()->getTimeRegion().contains(frame));
106   /* ------ 両方とも切断の時処理しない ---------------------- */
107   if (!up_is && !down_is) {
108     tile.getRaster()->clear();
109     return;
110   }
111   /* ------ up接続かつdown切断の時 -------------------------- */
112   if (up_is && !down_is) {
113     this->m_up->compute(tile, frame, rs);
114     return;
115   }
116   /* ------ down接続時 downのみ描画して... ------------------ */
117   if (down_is) {
118     this->m_down->compute(tile, frame, rs);
119   }
120   /* ------ up切断時 ---------------------------------------- */
121   if (!up_is) {
122     return;
123   }
124 
125   /* upと重なる部分を描画する */
126 
127   /* ------ tileの範囲 -------------------------------------- */
128   const TDimension tsz(tile.getRaster()->getSize()); /* 整数 */
129   const TRectD tileRect(tile.m_pos, TDimensionD(tsz.lx, tsz.ly));
130   TRectD upBBox;
131   if (upComputesWholeTile) {
132     upBBox = tileRect;
133   }      /* tile全体を得る */
134   else { /* 厳密なエリア... */
135     this->m_up->getBBox(frame, upBBox, rs);
136     upBBox *= tileRect; /* upとtileの交差エリア */
137 
138     /* より大きな四角エリアにPixel整数値で密着する */
139     makeRectCoherent(upBBox, tile.m_pos);  // double-->int grid
140   }
141 
142   TDimensionI upSize(                        /* TRectDをTDimensionIに変換 */
143                      tround(upBBox.getLx())  // getLx() = "x1>=x0?x1-x0:0"
144                      ,
145                      tround(upBBox.getLy())  // getLy() = "y1>=y0?y1-y0:0"
146                      );
147   if ((upSize.lx <= 0) || (upSize.ly <= 0)) {
148     return;
149   }
150 
151   /* ------ upのメモリ確保と描画 ---------------------------- */
152   TTile upTile;
153   this->m_up->allocateAndCompute(upTile, upBBox.getP00(), upSize,
154                                  tile.getRaster() /* 32/64bitsの判定に使う */
155                                  ,
156                                  frame, rs);
157   /* ------ upとdownのTRasterを得る ------------------------- */
158   TRectI dnRect(upTile.getRaster()->getSize());  // TDimensionI(-)
159 
160   dnRect += convert(upTile.m_pos - tile.m_pos); /* uptile->tile原点 */
161   /*
162   ここで問題はdoubleの位置を、四捨五入して整数値にしていること
163   移動してから四捨五入ではないの???
164   dnRectの元位置が整数位置なので、問題ないか...
165   */
166 
167   dn_ras = upComputesWholeTile ? tile.getRaster()
168                                : tile.getRaster()->extract(dnRect);
169   up_ras = upTile.getRaster();
170   assert(dn_ras->getSize() == up_ras->getSize());
171 }
dryComputeUpAndDown(TRectD & rect,double frame,const TRenderSettings & rs,bool upComputesWholeTile)172 void ino_blend_hard_light::dryComputeUpAndDown(TRectD &rect, double frame,
173                                                const TRenderSettings &rs,
174                                                bool upComputesWholeTile) {
175   const bool up_is = (this->m_up.isConnected() &&
176                       this->m_up.getFx()->getTimeRegion().contains(frame));
177   const bool down_is = (this->m_down.isConnected() &&
178                         this->m_down.getFx()->getTimeRegion().contains(frame));
179   /* ------ 両方とも切断の時処理しない ---------------------- */
180   if (!up_is && !down_is) {
181     return;
182   }
183   /* ------ up接続かつdown切断の時 -------------------------- */
184   if (up_is && !down_is) {
185     this->m_up->dryCompute(rect, frame, rs);
186     return;
187   }
188   /* ------ down接続時 -------------------------------------- */
189   if (down_is) {
190     this->m_down->dryCompute(rect, frame, rs);
191   }
192   /* ------ up切断時 ---------------------------------------- */
193   if (!up_is) {
194     return;
195   }
196 
197   /* ------ tileのgeometryを計算する ------------------------ */
198   TRectD upBBox;
199 
200   if (upComputesWholeTile) {
201     upBBox = rect;
202   } else {
203     this->m_up->getBBox(frame, upBBox, rs);
204     upBBox *= rect;
205     makeRectCoherent(upBBox, rect.getP00());
206   }
207   if ((upBBox.getLx() > 0.5) && (upBBox.getLy() > 0.5)) {
208     this->m_up->dryCompute(upBBox, frame, rs);
209   }
210 }
211 //------------------------------------------------------------
212 #include <sstream> /* std::ostringstream */
213 #include "igs_color_blend.h"
214 namespace {
215 template <class T, class Q>
tmpl_(TRasterPT<T> dn_ras_out,const TRasterPT<T> & up_ras,const double up_opacity,const bool clipping_mask_sw)216 void tmpl_(TRasterPT<T> dn_ras_out, const TRasterPT<T> &up_ras,
217            const double up_opacity, const bool clipping_mask_sw) {
218   double maxi = static_cast<double>(T::maxChannelValue);  // 255or65535
219 
220   assert(dn_ras_out->getSize() == up_ras->getSize());
221 
222   for (int yy = 0; yy < dn_ras_out->getLy(); ++yy) {
223     T *out_pix             = dn_ras_out->pixels(yy);
224     const T *const out_end = out_pix + dn_ras_out->getLx();
225     const T *up_pix        = up_ras->pixels(yy);
226     for (; out_pix < out_end; ++out_pix, ++up_pix) {
227       double upr = static_cast<double>(up_pix->r) / maxi;
228       double upg = static_cast<double>(up_pix->g) / maxi;
229       double upb = static_cast<double>(up_pix->b) / maxi;
230       double upa = static_cast<double>(up_pix->m) / maxi;
231       double dnr = static_cast<double>(out_pix->r) / maxi;
232       double dng = static_cast<double>(out_pix->g) / maxi;
233       double dnb = static_cast<double>(out_pix->b) / maxi;
234       double dna = static_cast<double>(out_pix->m) / maxi;
235       igs::color::hard_light(dnr, dng, dnb, dna, upr, upg, upb, upa,
236                              clipping_mask_sw ? up_opacity * dna : up_opacity);
237       out_pix->r = static_cast<Q>(dnr * (maxi + 0.999999));
238       out_pix->g = static_cast<Q>(dng * (maxi + 0.999999));
239       out_pix->b = static_cast<Q>(dnb * (maxi + 0.999999));
240       out_pix->m = static_cast<Q>(dna * (maxi + 0.999999));
241     }
242   }
243 }
fx_(TRasterP & dn_ras_out,const TRasterP & up_ras,const TPoint & pos,const double up_opacity,const bool clipping_mask_sw)244 void fx_(TRasterP &dn_ras_out, const TRasterP &up_ras, const TPoint &pos,
245          const double up_opacity, const bool clipping_mask_sw) {
246   /* 交差したエリアを処理するようにする、いるのか??? */
247   TRect outRect(dn_ras_out->getBounds());
248   TRect upRect(up_ras->getBounds() + pos);
249   TRect intersection = outRect * upRect;
250   if (intersection.isEmpty()) return;
251 
252   TRasterP cRout = dn_ras_out->extract(intersection);
253   TRect rr       = intersection - pos;
254   TRasterP cRup  = up_ras->extract(rr);
255 
256   TRaster32P rout32 = cRout, rup32 = cRup;
257   TRaster64P rout64 = cRout, rup64 = cRup;
258 
259   if (rout32 && rup32) {
260     tmpl_<TPixel32, UCHAR>(rout32, rup32, up_opacity, clipping_mask_sw);
261   } else if (rout64 && rup64) {
262     tmpl_<TPixel64, USHORT>(rout64, rup64, up_opacity, clipping_mask_sw);
263   } else {
264     throw TRopException("unsupported pixel type");
265   }
266 }
267 }
doCompute(TTile & tile,double frame,const TRenderSettings & rs)268 void ino_blend_hard_light::doCompute(TTile &tile, double frame,
269                                      const TRenderSettings &rs) {
270   /* ------ 画像生成 ---------------------------------------- */
271   TRasterP dn_ras, up_ras;
272   this->computeUpAndDown(tile, frame, rs, dn_ras, up_ras);
273   if (!dn_ras || !up_ras) {
274     return;
275   }
276   /* ------ 動作パラメータを得る ---------------------------- */
277   const double up_opacity =
278       this->m_opacity->getValue(frame) / ino::param_range();
279   /* ------ (app_begin)log記憶 ------------------------------ */
280   const bool log_sw = ino::log_enable_sw();
281 
282   if (log_sw) {
283     std::ostringstream os;
284     os << "params"
285        << "  up_opacity " << up_opacity << "   dn_tile w " << dn_ras->getLx()
286        << "  wrap " << dn_ras->getWrap() << "  h " << dn_ras->getLy()
287        << "  pixbits " << ino::pixel_bits(dn_ras) << "   up_tile w "
288        << up_ras->getLx() << "  wrap " << up_ras->getWrap() << "  h "
289        << up_ras->getLy() << "  pixbits " << ino::pixel_bits(up_ras)
290        << "   frame " << frame;
291   }
292   /* ------ fx処理 ------------------------------------------ */
293   try {
294     if (dn_ras) {
295       dn_ras->lock();
296     }
297     if (up_ras) {
298       up_ras->lock();
299     }
300     fx_(dn_ras, up_ras, TPoint(), up_opacity,
301         this->m_clipping_mask->getValue());
302     if (up_ras) {
303       up_ras->unlock();
304     }
305     if (dn_ras) {
306       dn_ras->unlock();
307     }
308   }
309   /* ------ error処理 --------------------------------------- */
310   catch (std::exception &e) {
311     if (up_ras) {
312       up_ras->unlock();
313     }
314     if (dn_ras) {
315       dn_ras->unlock();
316     }
317     if (log_sw) {
318       std::string str("exception <");
319       str += e.what();
320       str += '>';
321     }
322     throw;
323   } catch (...) {
324     if (up_ras) {
325       up_ras->unlock();
326     }
327     if (dn_ras) {
328       dn_ras->unlock();
329     }
330     if (log_sw) {
331       std::string str("other exception");
332     }
333     throw;
334   }
335 }
336