Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Interacting with C APIs

Interacting with C APIs

shibuyaswift #4

nakajijapan

June 08, 2016
Tweet

More Decks by nakajijapan

Other Decks in Technology

Transcript

  1. Interacting
    with C APIs
    shibuya.swift #4
    @nakajijapan
    Use GIFLIB in Swift

    View full-size slide

  2. @nakajijapan
    Software Engineer
    GMO PEPABO inc.
    iOS / Web / OS X

    View full-size slide

  3. NKJMultiMovieCaptureView
    NKJMovieComposer
    NKJPagerViewController
    PhotoSlider
    Teiten

    View full-size slide

  4. ϋϯυϝΠυ࡞඼Λ
    ചΕΔɺങ͑Δɻ
    ࠃ಺࠷େڃͷ
    ϋϯυϝΠυϚʔέοτ

    View full-size slide

  5. Teiten
    Teiten is an app that fixed-point
    observation a lot earnestly yourself
    by using a PC camera.
    http://teiten.nakajijapan.net/

    View full-size slide

  6. Sengiri
    " Sengiri " from the screen recording
    until the animated GIF file , you can
    create as soon as a simple operation .
    Sengiri, in English, is the shred.
    http://sengiri.nakajijapan.net/

    View full-size slide

  7. Stop
    SFDPSE
    Crop
    Record

    View full-size slide

  8. Sengiri is
    • OS X Application
    • ࠷খݶͷૢ࡞ͰGIFಈըΛ࡞੒͢Δ
    • ࢓ࣄޮ཰Խπʔϧ
    • OSS (coming soon…)

    View full-size slide

  9. ΋͏গ͠ؤுΖ͏

    View full-size slide

  10. ࣌ʑɺը࣭͕
    ྼԽ
    ͯ͠͠·͏

    View full-size slide

  11. ΋͏গ͠
    ਂງ͍͖͍ͯͨ͠

    View full-size slide

  12. ௐࠪ
    • FFmpeg
    • GIFLIB

    View full-size slide

  13. FFmpeg
    • ༷ʑͳencode/decodeʹରԠ
    • ࢓༷Λ೺Ѳ͢Δ͜ͱ͔Β
    • ๲େ
    • ίʔυྔ͕ଟ͍


    View full-size slide

  14. GIFLIB
    • GIFͷencode/decodeʹಛԽ
    • ࢓༷Λ೺Ѳ͠΍͍͢
    • ίʔυྔ͕গͳ͍
    • FFmpegΑΓ͸গͳ͍

    View full-size slide

  15. Use C API
    in Swift

    View full-size slide

  16. Interacting
    with C APIs
    Use giflib in Swift

    View full-size slide

  17. C
    int number = 10;
    int *p;
    *p = 100;
    printf("%d %x \n", *p, p);
    p = &number;
    printf("%d %x \n", *p, p);

    View full-size slide

  18. C
    516918f8
    p number
    10
    516918dc
    p = &number;

    View full-size slide

  19. C
    char c[] = “abcd";
    char *p;
    p = &c;
    printf("*p = %c\n",*p);
    printf("p = %s\n", p);
    printf("*(p + 1) = %c\n", *(c + 1));
    printf("*p + 1 = %c\n", *c + 1);
    printf("c[1] = %c\n", c[1]);
    // *p = a
    // p = abcd
    // *(p + 1) = b
    // *p + 1 = b
    // c[1] = b

    View full-size slide

  20. C
    516918f8
    p
    c[]
    516918dc
    p = &c;
    a b c d
    *(c + 1)
    *(c + 2)
    *(c + 3)

    View full-size slide

  21. C API -> Swift

    View full-size slide

  22. Primitive Types

    View full-size slide

  23. Primitive Types
    or Int8
    or Int
    or Int32

    View full-size slide

  24. $ 4XJGU
    1PJOUFS
    DPOTU5ZQF 6OTBGF1PJOUFS5ZQF
    5ZQF 6OTBGF.VUBCMF1PJOUFS5ZQF
    WPJE 6OTBGF.VUBCMF1PJOUFS7PJE
    Pointer

    View full-size slide

  25. C
    int number = 10;
    int *p;
    *p = 100;
    printf("%d %x \n", *p, p);
    p = &number;
    printf("%d %x \n", *p, p);

    View full-size slide

  26. Swift
    // int *p;
    var p:UnsafeMutablePointer

    View full-size slide

  27. Swift
    // int *p;
    var p = UnsafeMutablePointer.alloc(1)
    // *p = 100;
    p.memory = 100

    View full-size slide

  28. Swift
    // int *p;
    var p = UnsafeMutablePointer.alloc(1)
    // *p = 100;
    p.memory = 100

    View full-size slide

  29. Swift
    // int *p;
    var p = UnsafeMutablePointer.alloc(1)
    // *p = 100;
    p.memory = 100

    View full-size slide

  30. Swift
    // int *p;
    var p = UnsafeMutablePointer.alloc(1)
    // *p = 100;
    p.memory = 100
    WBSNFNPSZ.FNPSZ\HFUOPONVUBUJOHTFU^
    6OTBGF.VUBCMF1PJOUFS

    View full-size slide

  31. C
    unsigned char c[] = “abcd";
    unsigned char *p;
    p = &c;
    printf("*p = %c\n",*p);
    printf("p = %s\n", p);
    printf("*(p + 1) = %c\n", *(c + 1));
    printf("*p + 1 = %c\n", *c + 1);
    printf("c[1] = %c\n", c[1]);
    // *p = a
    // p = abcd
    // *(p + 1) = b
    // *p + 1 = b
    // c[1] = b

    View full-size slide

  32. Swift
    let p = UnsafeMutablePointer.alloc(4)
    or
    let p = UnsafeMutablePointer.alloc(4)

    View full-size slide

  33. C
    unsigned char c[] = “abcd";
    unsigned char *p;
    p = &c;
    printf("*p = %c\n",*p);
    printf("p = %s\n", p);
    printf("*(p + 1) = %c\n", *(c + 1));
    printf("*p + 1 = %c\n", *c + 1);
    printf("c[1] = %c\n", c[1]);
    // *p = a
    // p = abcd
    // *(p + 1) = b
    // *p + 1 = b
    // c[1] = b

    View full-size slide

  34. Swift
    let p = UnsafeMutablePointer.alloc(4)
    p.advancedBy(1).memory

    View full-size slide

  35. Swift
    let p = UnsafeMutablePointer.alloc(4)
    p.advancedBy(1).memory
    TVCTDSJQU @J*OU
    .FNPSZ\HFUOPONVUBUJOHTFU^

    View full-size slide

  36. Swift
    let p = UnsafeMutablePointer.alloc(4)
    p[1]

    View full-size slide

  37. Swift
    for i in 0..<256 {
    colorTypes.advancedBy(i).memory.Red =
    pixelColorMaps.advancedBy(i).memory.red
    colorTypes.advancedBy(i).memory.Green =
    pixelColorMaps.advancedBy(i).memory.green
    colorTypes.advancedBy(i).memory.Blue =
    pixelColorMaps.advancedBy(i).memory.blue
    }

    View full-size slide

  38. Swift
    for i in 0..<256 {
    colorTypes[i].Red = pixelColorMaps[i].red
    colorTypes[i].Green = pixelColorMaps[i].green
    colorTypes[i].Blue = pixelColorMaps[i].blue
    }

    View full-size slide

  39. Swift
    unsigned char controlExtension[3] =
    {0x04, 0x00, 0x00, 0xff};

    View full-size slide

  40. Swift

    let controlExtension =
    UnsafeMutablePointer.alloc(4)
    controlExtension[0] = 0x04
    controlExtension[1] = 0x00
    controlExtension[2] = 0x00
    controlExtension[3] = 0xff

    View full-size slide

  41. Type Casting

    View full-size slide

  42. Type Casting
    UnsafeMutablePointer.self
    UnsafeMutablePointer.self

    View full-size slide

  43. Type Casting
    let address = baseAddress as UnsafeMutablePointer

    View full-size slide

  44. Type Casting
    let address = baseAddress as UnsafeMutablePointer

    View full-size slide

  45. Type Casting
    /// Returns the bits of `x`, interpreted as having type `U`.
    ///
    /// - Warning: Breaks the guarantees of Swift's type system; use
    /// with extreme care. There's almost always a better way to do
    /// anything.
    ///
    @warn_unused_result
    public func unsafeBitCast(x: T, _: U.Type) -> U

    View full-size slide

  46. Swift
    let address = CVPixelBufferGetBaseAddress(imageBuffer!)
    let address2 = unsafeBitCast(
    baseAddress,
    UnsafeMutablePointer.self
    )

    View full-size slide

  47. Type Casting
    /// Returns the bits of `x`, interpreted as having type `U`.
    ///
    /// - Warning: Breaks the guarantees of Swift's type system; use
    /// with extreme care. There's almost always a better way to do
    /// anything.
    ///
    @warn_unused_result
    public func unsafeBitCast(x: T, _: U.Type) -> U

    View full-size slide

  48. Memory
    Management

    View full-size slide

  49. Memory Management
    • SwiftͰCͷؔ਺͕ར༻Ͱ͖Δ
    • ࢖͍ํ͸ಉ͡
    • UnsafeMutablePointerͰಉ༷ͷϝιου͕༻
    ҙ͞Ε͍ͯΔ

    View full-size slide

  50. Memory Management
    /* ANSI-C */
    public func memchr(_: UnsafePointer, _: Int32, _: Int) -> UnsafeMutablePointer
    public func memcmp(_: UnsafePointer, _: UnsafePointer, _: Int) -> Int32
    public func memcpy(_: UnsafeMutablePointer, _: UnsafePointer, _: Int) ->
    UnsafeMutablePointer
    public func memmove(_: UnsafeMutablePointer, _: UnsafePointer, _: Int) ->
    UnsafeMutablePointer
    public func memset(_: UnsafeMutablePointer, _: Int32, _: Int) ->
    UnsafeMutablePointer

    View full-size slide

  51. Swift
    let buffer = UnsafeMutablePointer.alloc(
    width * height * 4
    )
    memset(buffer, 0, width * height * 4)
    (…snip…)
    free(buffer)

    View full-size slide

  52. Swift
    let buffer = UnsafeMutablePointer.alloc(
    width * height * 4
    )
    memset(buffer, 0, width * height * 4)
    (…snip…)
    free(buffer)
    buffer.destroy()

    View full-size slide

  53. C APIs
    • Swift͸C APIʹΞΫηεग़དྷΔे෼ͳ
    ػೳΛ࣋ͪ߹Θ͍ͤͯΔ௿ϨΠϠʔ
    ͳϞδϡʔϧʹ௚઀Ͱ͖Δͷศར
    • ϙΠϯλɺϝϞϦ؅ཧͷ஌ࣝ͸ඞਢ

    View full-size slide

  54. UnsafeMutablePointer
    IUUQTEFWFMPQFSBQQMFDPNMJCSBSZXBUDIPTEPDVNFOUBUJPO4XJGU
    [email protected]@4USVDUVSFJOEFYIUNM

    View full-size slide

  55. GIFLIBΛ࢖͍͍ͨ

    View full-size slide

  56. • ࢓༷೺Ѳෆ଍…
    • ΧϥʔϚοϓ
    • ؔ਺

    View full-size slide