You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.3 KiB

  1. #!/usr/bin/env python
  2. # This program source code file is part of KiCad, a free EDA CAD application.
  3. #
  4. # Copyright (C) 2016 CERN
  5. # @author Maciej Suminski <maciej.suminski@cern.ch>
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, you may find one here:
  19. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. # or you may search the http://www.gnu.org website for the version 2 license,
  21. # or you may write to the Free Software Foundation, Inc.,
  22. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. # Converts a bitmap font atlas image in PNG format to raw pixel data stored
  24. # in a C structure.
  25. import os
  26. import png
  27. import sys
  28. def convert(png_file):
  29. r = png.Reader(file=open(png_file, 'rb'))
  30. img = r.read()
  31. print(img);
  32. if img[3].get("alpha") == True:
  33. print('Detected alpha channel!');
  34. output = open(os.path.splitext(png_file)[0] + '_img.c', 'w')
  35. width = img[0]
  36. height = img[1]
  37. # Header
  38. output.write(
  39. """
  40. /* generated with png2struct.py, do not modify by hand */
  41. static const struct {
  42. unsigned int width, height;
  43. unsigned char pixels[%d * %d];
  44. } bitmap_font = {
  45. """ % (width, height));
  46. output.write('%d, %d,\n{' % (width, height))
  47. if img[3].get("alpha") == True:
  48. for row in img[2]:
  49. for p in row[3::4]:
  50. output.write('%d,' % p)
  51. output.write('\n');
  52. else:
  53. for row in img[2]:
  54. for p in row:
  55. output.write('%d,' % p)
  56. output.write('\n');
  57. output.write('}\n};\n')
  58. output.close()
  59. #----------------------------------------------------------------------
  60. if __name__ == "__main__":
  61. argc = len(sys.argv)
  62. if(argc == 2):
  63. convert(sys.argv[1])
  64. else:
  65. print("usage: %s <xml-file>" % sys.argv[0])