Interacting with C APIs

Interacting with C APIs

shibuyaswift #4

A77456b262557e22986345f6d0555c58?s=128

nakajijapan

June 08, 2016
Tweet

Transcript

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

    Swift
  2. About Me

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

    OS X
  4. NKJMultiMovieCaptureView NKJMovieComposer NKJPagerViewController PhotoSlider Teiten

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

  6. ࠷ۙ

  7. Teiten Teiten is an app that fixed-point observation a lot

    earnestly yourself by using a PC camera. http://teiten.nakajijapan.net/
  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/
  9. Stop SFDPSE Crop Record

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

    • OSS (coming soon…)
  11. None
  12. ΋͏গ͠ؤுΖ͏

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

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

  15. ௐࠪ • FFmpeg • GIFLIB

  16. FFmpeg

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

  18. GIFLIB

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

  20. But

  21. C

  22. Use C API in Swift

  23. Talk about

  24. Interacting with C APIs Use giflib in Swift

  25. Premise

  26. C int number = 10; int *p; *p = 100;

    printf("%d %x \n", *p, p); p = &number; printf("%d %x \n", *p, p);
  27. C 516918f8 p number 10 516918dc p = &number;

  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
  29. C 516918f8 p c[] 516918dc p = &c; a b

    c d *(c + 1) *(c + 2) *(c + 3)
  30. C API -> Swift

  31. Primitive Types

  32. Primitive Types or Int8 or Int or Int32

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

  34. C int number = 10; int *p; *p = 100;

    printf("%d %x \n", *p, p); p = &number; printf("%d %x \n", *p, p);
  35. Swift // int *p; var p:UnsafeMutablePointer<CInt>

  36. Swift // int *p; var p = UnsafeMutablePointer<CInt>.alloc(1) // *p

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

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

    = 100; p.memory = 100
  39. Swift // int *p; var p = UnsafeMutablePointer<CInt>.alloc(1) // *p

    = 100; p.memory = 100 WBSNFNPSZ.FNPSZ\HFUOPONVUBUJOHTFU^ 6OTBGF.VUBCMF1PJOUFS
  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
  41. Swift let p = UnsafeMutablePointer<CUnsignedChar>.alloc(4) or let p = UnsafeMutablePointer<UInt8>.alloc(4)

  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
  43. Swift let p = UnsafeMutablePointer<CUnsignedChar>.alloc(4) p.advancedBy(1).memory

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

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

  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 }
  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 }
  48. Swift unsigned char controlExtension[3] = {0x04, 0x00, 0x00, 0xff};

  49. Swift let controlExtension = UnsafeMutablePointer<UInt8>.alloc(4) controlExtension[0] = 0x04 controlExtension[1] =

    0x00 controlExtension[2] = 0x00 controlExtension[3] = 0xff
  50. Type Casting

  51. Type Casting UnsafeMutablePointer<UInt8>.self UnsafeMutablePointer<Void>.self

  52. Type Casting let address = baseAddress as UnsafeMutablePointer<UInt8>

  53. Type Casting let address = baseAddress as UnsafeMutablePointer<UInt8>

  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<T, U>(x: T, _: U.Type) -> U
  55. Swift let address = CVPixelBufferGetBaseAddress(imageBuffer!) let address2 = unsafeBitCast( baseAddress,

    UnsafeMutablePointer<UInt8>.self )
  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<T, U>(x: T, _: U.Type) -> U
  57. Memory Management

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

  59. Memory Management /* ANSI-C */ public func memchr(_: UnsafePointer<Void>, _:

    Int32, _: Int) -> UnsafeMutablePointer<Void> public func memcmp(_: UnsafePointer<Void>, _: UnsafePointer<Void>, _: Int) -> Int32 public func memcpy(_: UnsafeMutablePointer<Void>, _: UnsafePointer<Void>, _: Int) -> UnsafeMutablePointer<Void> public func memmove(_: UnsafeMutablePointer<Void>, _: UnsafePointer<Void>, _: Int) -> UnsafeMutablePointer<Void> public func memset(_: UnsafeMutablePointer<Void>, _: Int32, _: Int) -> UnsafeMutablePointer<Void>
  60. Swift let buffer = UnsafeMutablePointer<UInt8>.alloc( width * height * 4

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

    ) memset(buffer, 0, width * height * 4) (…snip…) free(buffer) buffer.destroy()
  62. C APIs

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

  64. UnsafeMutablePointer IUUQTEFWFMPQFSBQQMFDPNMJCSBSZXBUDIPTEPDVNFOUBUJPO4XJGU 3FGFSFODF4XJGU@6OTBGF.VUBCMF1PJOUFS@4USVDUVSFJOEFYIUNM

  65. GIFLIBΛ࢖͍͍ͨ

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

  67. ࠂ஌

  68. iOSDC

  69. Thanks.