9

問題があります。以下に示すように、画像をラップする循環ラップ関数を作成したいと考えています。

画像ラップ

これは OSX で利用できますが、iOS では利用できません。

これまでの私の論理は次のとおりです。

画像をセクションに分割し、xセクションごとに次のようにします。

  1. alpha度回転
  2. x 軸で画像をスケーリングして、画像のひし形の「歪んだ」効果を作成します
  3. 後ろに回転90 - atan((h / 2) / (w / 2))
  4. オフセットを変換する

私の問題は、これが不正確に見えることであり、これを正しく行う方法を数学的に理解することができませんでした.どんな助けでも大歓迎です.

の OSX ドキュメントへのリンクCICircularWrap:

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/filter/ci/CICircularWrap

4

2 に答える 2

12

CICircularWrapは iOS ではサポートされていないため(編集: 現在はサポートされています - 以下の回答を確認してください)、今のところ独自の効果をコーディングする必要があります。おそらく最も簡単な方法は、極座標系からデカルト座標系への変換を計算し、ソース イメージから補間することです。私はこの単純な (そして率直に言って非常に遅い - かなり最適化できる) アルゴリズムを思いつきました:

    #import <QuartzCore/QuartzCore.h>

    CGContextRef CreateARGBBitmapContext (size_t pixelsWide, size_t pixelsHigh)
    {
        CGContextRef    context = NULL;
        CGColorSpaceRef colorSpace;
        void *          bitmapData;
        int             bitmapByteCount;
        int             bitmapBytesPerRow;

        // Declare the number of bytes per row. Each pixel in the bitmap in this
        // example is represented by 4 bytes; 8 bits each of red, green, blue, and
        // alpha.
        bitmapBytesPerRow   = (int)(pixelsWide * 4);
        bitmapByteCount     = (int)(bitmapBytesPerRow * pixelsHigh);

        // Use the generic RGB color space.
        colorSpace = CGColorSpaceCreateDeviceRGB();
        if (colorSpace == NULL)
        {
            fprintf(stderr, "Error allocating color space\n");
            return NULL;
        }

        // Allocate memory for image data. This is the destination in memory
        // where any drawing to the bitmap context will be rendered.
        bitmapData = malloc( bitmapByteCount );
        if (bitmapData == NULL)
        {
            fprintf (stderr, "Memory not allocated!");
            CGColorSpaceRelease( colorSpace );
            return NULL;
        }

        // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
        // per component. Regardless of what the source image format is
        // (CMYK, Grayscale, and so on) it will be converted over to the format
        // specified here by CGBitmapContextCreate.
        context = CGBitmapContextCreate (bitmapData,
                                         pixelsWide,
                                         pixelsHigh,
                                         8,      // bits per component
                                         bitmapBytesPerRow,
                                         colorSpace,
                                         kCGImageAlphaPremultipliedFirst);
        if (context == NULL)
        {
            free (bitmapData);
            fprintf (stderr, "Context not created!");
        }

        // Make sure and release colorspace before returning
        CGColorSpaceRelease( colorSpace );

        return context;
    }

    CGImageRef circularWrap(CGImageRef inImage,CGFloat bottomRadius, CGFloat topRadius, CGFloat startAngle, BOOL clockWise, BOOL interpolate)
    {
        if(topRadius < 0 || bottomRadius < 0) return NULL;

        // Create the bitmap context
        int w = (int)CGImageGetWidth(inImage);
        int h = (int)CGImageGetHeight(inImage);

        //result image side size (always a square image)
        int resultSide = 2*MAX(topRadius, bottomRadius);
        CGContextRef cgctx1 = CreateARGBBitmapContext(w,h);
        CGContextRef cgctx2 = CreateARGBBitmapContext(resultSide,resultSide);

        if (cgctx1 == NULL || cgctx2 == NULL)
        {
            return NULL;
        }

        // Get image width, height. We'll use the entire image.
        CGRect rect = {{0,0},{w,h}};

        // Draw the image to the bitmap context. Once we draw, the memory
        // allocated for the context for rendering will then contain the
        // raw image data in the specified color space.
        CGContextDrawImage(cgctx1, rect, inImage);

        // Now we can get a pointer to the image data associated with the bitmap
        // context.
        int *data1 = CGBitmapContextGetData (cgctx1);
        int *data2 = CGBitmapContextGetData (cgctx2);

        int resultImageSize = resultSide*resultSide;
        double temp;
        for(int *p = data2, pos = 0;pos<resultImageSize;p++,pos++)
        {
            *p = 0;
            int x = pos%resultSide-resultSide/2;
            int y = -pos/resultSide+resultSide/2;
            CGFloat phi = modf(((atan2(x, y)+startAngle)/2.0/M_PI+0.5),&temp);
            if(!clockWise) phi = 1-phi;
            phi*=w;
            CGFloat r = ((sqrtf(x*x+y*y))-topRadius)*h/(bottomRadius-topRadius);
            if(phi>=0 && phi<w && r>=0 && r<h)
            {
                if(!interpolate || phi >= w-1 || r>=h-1)
                {
                    //pick the closest pixel
                    *p = data1[(int)r*w+(int)phi];
                }
                else
                {
                    double dphi = modf(phi, &temp);
                    double dr = modf(r, &temp);

                    int8_t* c00 = (int8_t*)(data1+(int)r*w+(int)phi);
                    int8_t* c01 = (int8_t*)(data1+(int)r*w+(int)phi+1);
                    int8_t* c10 = (int8_t*)(data1+(int)r*w+w+(int)phi);
                    int8_t* c11 = (int8_t*)(data1+(int)r*w+w+(int)phi+1);

                    //interpolate components separately
                    for(int component = 0; component < 4; component++)
                    {
                        double avg = ((*c00 & 0xFF)*(1-dphi)+(*c01 & 0xFF)*dphi)*(1-dr)+((*c10 & 0xFF)*(1-dphi)+(*c11 & 0xFF)*dphi)*dr;
                        *p += (((int)(avg))<<(component*8));
                        c00++; c10++; c01++; c11++;
                    }
                }
            }
        }

        CGImageRef result = CGBitmapContextCreateImage(cgctx2);

        // When finished, release the context
        CGContextRelease(cgctx1);
        CGContextRelease(cgctx2);
        // Free image data memory for the context
        if (data1) free(data1);
        if (data2) free(data2);

        return result;
    }

circularWrapパラメータを指定して関数を使用します。

  • CGImageRef inImageソース画像
  • CGFloat bottomRadiusソース画像の下側は、この半径の円に変換されます
  • CGFloat topRadiusソース画像の上側も同様で、これは下側の半径より大きくても小さくてもかまいません。(画像の上下を折り返すことになります)
  • CGFloat startAngleソース画像の左右が変形する角度。BOOL clockWiseレンダリングの方向
  • BOOL interpolateシンプルなアンチエイリアシング アルゴリズム。画像の内側のみ補間

いくつかのサンプル (左上がソース画像):
いくつかのサンプル (左上が元の画像) コード付き:

    image1 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,0,300,0,YES,NO)];
    image2 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,100,300,M_PI_2,NO,YES)];
    image3 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,300,200,M_PI_4,NO,YES)];
    image4 = [UIImage imageWithCGImage:circularWrap(sourceImage.CGImage,250,300,0,NO,NO)];

楽しい!:)

于 2013-10-17T17:17:24.777 に答える
3

Apple は CICircularWrap を iOS 9 に追加しました

https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CICircularWrap

画像を透明な円で囲みます。

ローカライズされた表示名

サーキュラー ラップ ディストーション

可用性

OS X v10.5 以降および iOS 9 以降で利用できます。

于 2015-09-21T09:19:17.763 に答える