Class: Rixmap::Deformer::AffineDeformer

Inherits:
BaseDeformer show all
Defined in:
src/rixmapdeformation.cxx,
src/rixmapdeformation.cxx

Overview

アフィン変換による変形処理実装.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDeformer

#inspect, #interpolator, #interpolator=

Constructor Details

#initialize(matrix = nil, ipo = nil) (private)

アフィン変換による変形処理実装オブジェクトを初期化します.

Parameters:

  • matrix (Rixmap::Deformer::AffineMatrix) (defaults to: nil)

    アフィン変換パラメータ

  • ipo (Symbol, String, Integer, nil) (defaults to: nil)

    補間処理方法

See Also:

  • Rixmap::Deformer::AffineDeformer.{BaseDeformer{BaseDeformer#initialize}


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'src/rixmapdeformation.cxx', line 136

static VALUE AffineDeformer_initialize(int argc, VALUE* argv, VALUE self) {
    VALUE argMatrix = Qnil, argIPO = Qnil;
    rb_scan_args(argc, argv, "02", &argMatrix, &argIPO);

    // 自分を初期化
    if (NIL_P(argMatrix)) {
        rb_iv_set(self, "@matrix", rb_class_new_instance(0, NULL, cRixmapAffineMatrix));
    } else {
        rb_iv_set(self, "@matrix", argMatrix);
    }

    // 親を初期化
    {
        VALUE sargs[1] = {argIPO};
        rb_call_super(1, sargs);
    }

    return self;
}

Instance Attribute Details

#matrixObject

Instance Method Details

#deform(image, fit = false) ⇒ Rixmap::Image

アフィン変換による変形処理を実行します.

Parameters:

  • image (Rixmap::Image)

    対象画像

  • fit (Boolean) (defaults to: false)

    trueの場合は変形後の画像サイズに合うように画像自体を拡大します.

Returns:



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'src/rixmapdeformation.cxx', line 164

static VALUE AffineDeformer_deform(int argc, VALUE* argv, VALUE self) {
    // パラメータ処理
    VALUE argImage = Qnil, argFit = Qnil;
    rb_scan_args(argc, argv, "11", &argImage, &argFit);

    // 自分ポインタ
    Rixmap::AffineDeformerData* _this = rixmap_unwrap<Rixmap::AffineDeformerData>(self);

    // 対象画像
    Rixmap::ImageData* srcImage = rixmap_unwrap<Rixmap::ImageData>(argImage);

    // 補間処理方法
    Rixmap::Interpolator* ipo = NULL;
    switch (_this->getInterpolation()) {
    case Rixmap::Interpolation::BICUBIC:
        ipo = new Rixmap::BicubicInterpolator(srcImage);
        break;

    default:
        ipo = new Rixmap::NOPInterpolator(srcImage);
        break;
    }

    // 画像サイズ調整フラグをC++型に展開
    bool fit = false;
    if (RTEST(argFit)) {
        fit = true;
    }

    try {
        // アフィン変換パラメータ
        VALUE matrixObject = rb_iv_get(self, "@matrix");
        Rixmap::AffineMatrix* matrix = rixmap_unwrap<Rixmap::AffineMatrix>(matrixObject);

        // 画像サイズ調整
        int32_t newWidth   = srcImage->getWidth();
        int32_t newHeight  = srcImage->getHeight();
        int32_t newXOffset = 0;
        int32_t newYOffset = 0;

        if (fit) {  // サイズ調整有
            matrix->matrixize();

            // 座標点を取得
            Rixmap::Point srcPoints[4] = {
                {0.0, 0.0},
                {0.0, static_cast<double>(srcImage->getHeight())},
                {static_cast<double>(srcImage->getWidth()), 0.0},
                {static_cast<double>(srcImage->getWidth()), static_cast<double>(srcImage->getHeight())}
            };
            Rixmap::Point dstPoint;

            int32_t left   = INT32_MAX;
            int32_t right  = INT32_MIN;
            int32_t top    = INT32_MAX;
            int32_t bottom = INT32_MIN;
            for (int i = 0; i < 4; i++) {
                matrix->deform(srcPoints[i], dstPoint);
                if (dstPoint.x < left) {
                    left = lround(std::floor(dstPoint.x));
                }
                if (dstPoint.x > right) {
                    right = lround(std::ceil(dstPoint.x));
                }
                if (dstPoint.y < top) {
                    top = lround(std::floor(dstPoint.y));
                }
                if (dstPoint.y > bottom) {
                    bottom = lround(std::ceil(dstPoint.y));
                }
            }

            // サイズへ
            newWidth   = (right - left);
            newHeight  = (bottom - top);
            newXOffset = left;
            newYOffset = top;
        } else {    // サイズ調整なし
            // DO NOTHING
        }

        // 保存先画像を作成
        VALUE srcMetadata    = Rixmap::Helper::GetImageMetadata(*srcImage);
        VALUE dstImageObject = RixmapImage_NewInstance(ipo->getPreferredMode(), newWidth, newHeight, srcMetadata);
        Rixmap::ImageData* dstImage = rixmap_unwrap<Rixmap::ImageData>(dstImageObject);

        // 移動先チャンネルリスト
        const Rixmap::ChannelArray& channels = dstImage->getModeInfo().getChannels();

        // 変換処理
        matrix->matrixize();
        Rixmap::Point src;      // 変形前 (元画像) 座標格納先
        Rixmap::Point dst;      // 変形後 (返却用画像) 座標格納先
        for (int32_t dstY = 0; dstY < dstImage->getHeight(); dstY++) {
            for (int32_t dstX = 0; dstX < dstImage->getWidth(); dstX++) {
                // 変換前座標を取得
                dst.x = static_cast<double>(dstX + newXOffset);
                dst.y = static_cast<double>(dstY + newYOffset);
                matrix->inverse(dst, src);

                // 補間処理
                ipo->calculate(src.x, src.y);

                // ピクセルデータを取り込む
                for (auto it = channels.begin(); it != channels.end(); it++) {
                    uint8_t byte = ipo->get(*it);
                    dstImage->set(*it, dstX, dstY, byte);
                }
            }
        }

        return dstImageObject;
    } catch (const std::exception& e) {
        if (ipo != NULL) {
            delete ipo;
        }
        rb_raise(rb_eRuntimeError, e.what());
    }
}