image_to_code.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/python
  2. import os
  3. import sys
  4. import getopt
  5. headerTemplate = """/*
  6. * Copyright 2010-present Facebook.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /*
  20. * This file was built by running
  21. *
  22. * ./image_to_code.py %(args)s
  23. *
  24. * Thanks to the Facebook SDK for providing the base for this script
  25. */
  26. """
  27. def bytes_from_file(filename, chunksize=8192):
  28. with open(filename, "rb") as f:
  29. while True:
  30. chunk = f.read(chunksize)
  31. if chunk:
  32. for b in chunk:
  33. yield b
  34. else:
  35. break
  36. def write_header_file(header, className, outputFile):
  37. with open(outputFile, "w") as f:
  38. f.write(header)
  39. f.write("""
  40. #import <Foundation/Foundation.h>
  41. #import <UIKit/UIKit.h>
  42. """)
  43. f.write("@interface " + className + " : NSObject\n\n")
  44. f.write("+ (UIImage *)image;\n\n")
  45. f.write("@end")
  46. def write_implementation_file(inputFile, header, className, outputFile):
  47. formattedBytes = ["0x{0:02x}".format(ord(x)) for x in bytes_from_file(inputFile)]
  48. with open(outputFile, "w") as f:
  49. f.write(header)
  50. f.write("\n")
  51. f.write("#import \"" + className + ".h\"\n")
  52. f.write("#import \"FLEXImageLoader.h\"\n\n")
  53. # Write standard bytes out
  54. f.write("const Byte " + className + "_standard[] = {\n")
  55. f.write(", ".join(formattedBytes))
  56. f.write("\n")
  57. f.write("};\n")
  58. # Write retina if present
  59. useRetina = True
  60. try:
  61. (name, ext) = inputFile.rsplit(".", 1)
  62. name = name + "@2x"
  63. formattedBytes = ["0x{0:02x}".format(ord(x)) for x in bytes_from_file(name + "." + ext)]
  64. f.write("const Byte " + className + "_retina[] = {\n")
  65. f.write(", ".join(formattedBytes))
  66. f.write("\n")
  67. f.write("};\n")
  68. except IOError:
  69. useRetina = False
  70. # Enter the bundle path override
  71. # strip of the PNG that was added for our class name for the resource name
  72. bundlepath = "@\"" + "FacebookSDKImages/" + className[0:-3] + ".png\""
  73. f.write("\n")
  74. f.write("@implementation " + className + "\n\n")
  75. f.write("+ (UIImage *)image {\n")
  76. f.write(" return [FLEXImageLoader imageFromBytes:" + className + "_standard ")
  77. f.write("length:sizeof(" + className + "_standard)/sizeof(" + className + "_standard[0]) ")
  78. if useRetina:
  79. f.write("fromRetinaBytes:" + className + "_retina ")
  80. f.write("retinaLength:sizeof(" + className + "_retina)/sizeof(" + className + "_retina[0])];\n")
  81. else:
  82. f.write("fromRetinaBytes:NULL ")
  83. f.write("retinaLength:0];\n")
  84. f.write("}\n")
  85. f.write("@end\n")
  86. print(", ".join(formattedBytes))
  87. def main(argv):
  88. inputFile = ''
  89. outputClass = 'ImageCode'
  90. outputDir = os.getcwd()
  91. try:
  92. opts, args = getopt.getopt(argv,"hi:c:o:")
  93. except getopt.GetoptError:
  94. print('image_to_code.py -i <inputFile> [-c <class>] [-o <outputDir>]')
  95. sys.exit(2)
  96. for opt, arg in opts:
  97. if opt == '-h':
  98. print('image_to_code.py -i <inputFile> [-c <class>] [-o <outputDir>]')
  99. sys.exit()
  100. elif opt == '-i':
  101. inputFile = arg
  102. elif opt == '-c':
  103. outputClass = arg
  104. elif opt in '-o':
  105. outputDir = arg
  106. # Build file headers
  107. header = headerTemplate % {"args" : " ".join(argv)}
  108. # outputClass needs to add PNG as part of it
  109. outputClass = outputClass + "PNG"
  110. # Build the output base filename
  111. outputFileBase = outputDir + "/" + outputClass
  112. # Build .h file
  113. outputFile = outputFileBase + ".h"
  114. write_header_file(header, outputClass, outputFile)
  115. # Build .m file
  116. outputFile = outputFileBase + ".m"
  117. write_implementation_file(inputFile, header, outputClass, outputFile)
  118. if __name__ == "__main__":
  119. main(sys.argv[1:])