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 Slide

  2. About Me

    View Slide

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

    View Slide

  4. NKJMultiMovieCaptureView
    NKJMovieComposer
    NKJPagerViewController
    PhotoSlider
    Teiten

    View Slide

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

    View Slide

  6. ࠷ۙ

    View Slide

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

    View Slide

  8. 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 Slide

  9. Stop
    SFDPSE
    Crop
    Record

    View Slide

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

    View Slide

  11. View Slide

  12. ΋͏গ͠ؤுΖ͏

    View Slide

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

    View Slide

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

    View Slide

  15. ௐࠪ
    • FFmpeg
    • GIFLIB

    View Slide

  16. FFmpeg

    View Slide

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


    View Slide

  18. GIFLIB

    View Slide

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

    View Slide

  20. But

    View Slide

  21. C

    View Slide

  22. Use C API
    in Swift

    View Slide

  23. Talk about

    View Slide

  24. Interacting
    with C APIs
    Use giflib in Swift

    View Slide

  25. Premise

    View Slide

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

    View Slide

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

    View Slide

  28. 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 Slide

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

    View Slide

  30. C API -> Swift

    View Slide

  31. Primitive Types

    View Slide

  32. Primitive Types
    or Int8
    or Int
    or Int32

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. 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 Slide

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

    View Slide

  42. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  46. 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 Slide

  47. 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 Slide

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

    View Slide

  49. Swift

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

    View Slide

  50. Type Casting

    View Slide

  51. Type Casting
    UnsafeMutablePointer.self
    UnsafeMutablePointer.self

    View Slide

  52. Type Casting
    let address = baseAddress as UnsafeMutablePointer

    View Slide

  53. Type Casting
    let address = baseAddress as UnsafeMutablePointer

    View Slide

  54. 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 Slide

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

    View Slide

  56. 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 Slide

  57. Memory
    Management

    View Slide

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

    View Slide

  59. 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 Slide

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

    View Slide

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

    View Slide

  62. C APIs

    View Slide

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

    View Slide

  64. UnsafeMutablePointer
    IUUQTEFWFMPQFSBQQMFDPNMJCSBSZXBUDIPTEPDVNFOUBUJPO4XJGU
    [email protected]@4USVDUVSFJOEFYIUNM

    View Slide

  65. GIFLIBΛ࢖͍͍ͨ

    View Slide

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

    View Slide

  67. ࠂ஌

    View Slide

  68. iOSDC

    View Slide

  69. Thanks.

    View Slide