-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_Thermal_Export.py
executable file
·127 lines (113 loc) · 3.98 KB
/
Adafruit_Thermal_Export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#####################################################################################
## This code is released under the GPL liscence by Raul Aguaviva (aguaviva--gmail.com)
## http://www.gnu.org/licenses/gpl.html
#####################################################################################
## The Gimp exporter 1bit bitmaps to a C language header file Textures
#####################################################################################
## * Installation
##
## Linux: Copy this file in to your ~/.gimp-2.6/plug-ins
## Windows: Not sure how to install it in windows :-)
##
## * Instructions
##
## 1) Open the image to export in GIMP
## 2) Resize the image to 128x64
## 3) Convert the image to a "indexed image" ( /Image/Transform/Indexed/ )
## a) Select black adn white (1-bit) palette
## b) Select the dithering method (Floyd steinberg is a good one )
## 4) Select "Save as" and look for the KS0108
##
## * How lo load it
##
## void LoadBitmap( uint8_t *bitmap )
## {
## for(y=0;y<8;y++)
## {
## SelectLeftSide()
## LocateXY( 0, y )
##
## for(x=0;x<64;x++)
## {
## ks0108_Write( pgm_read_byte(bitmap++) )
## }
##
## SelectRightSide()
## LocateXY( 0, y )
##
## for(x=0;x<64;x++)
## {
## ks0108_Write( pgm_read_byte(bitmap++) )
## }
## }
## }
##
## * Greetings
##
## Thanks to M-A Loyer since this script was heavily inspired by his "Gimp exporter for NDS 3D Textures"
##
#####################################################################################
import struct
import gimp
from gimpfu import *
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def python_ks0108export(img, drawable, filename, raw_filename):
pdb.gimp_layer_resize_to_image_size(drawable)
width = drawable.width
height = drawable.height
fileOut = open(filename,"w")
#remove path from filename and extension
i = filename.rfind("/")
p = filename.rfind(".")
if ( i >= 0 ):
if ( p == -1 ):
filename = filename[i+1:len(filename) ]
else:
filename = filename[i+1:p ]
gimp.progress_init(_("Saving as HEX (1 bit color)"))
fileOut.write("#ifndef %s\n" % filename.upper() )
fileOut.write("#define %s\n" % filename.upper() )
fileOut.write("\n")
fileOut.write("int %sWidth = " % filename)
fileOut.write("%d;" % width)
fileOut.write("\n")
fileOut.write("int %sHeight = " % filename)
fileOut.write("%d;" % height)
fileOut.write("\n\n")
fileOut.write("static unsigned char __attribute__ ((progmem)) %s[] = {\n" % filename )
for y in range(0, height):
for x in range(0, width, 8):
byte = 0
for xx in range(0, 8):
(channels,pixel) = pdb.gimp_drawable_get_pixel(drawable,x + xx, y)
byte = byte | ( pixel[0] << (7 - xx) )
fileOut.write("0x%02X, " % byte)
gimp.progress_update(float(y) / (height))
fileOut.write( "\n" )
fileOut.write("};\n" )
fileOut.write("#endif /* %s */\n" % filename.upper() )
gimp.progress_update(1)
fileOut.close()
def register_save_ks0108():
gimp.register_save_handler("file-KS0108exportH-save", "h", "")
register(
"file-KS0108exportH-save",
N_("Save in raw HEX format as header file"),
"Export an image to a .h file",
"Raul Aguaviva",
"Raul Aguaviva",
"2009",
N_("Adafruit_Thermal Printer 1bit HEX header file"),
"INDEXED",
[
(PF_IMAGE, "image", "Input image", None),
(PF_DRAWABLE, "drawable", "Input drawable", None),
(PF_STRING, "filename", "The name of the file", None),
(PF_STRING, "raw-filename", "The name of the file", None),
],
[],
python_ks0108export, on_query=register_save_ks0108,
menu="<Save>", domain=("gimp20-python", gimp.locale_directory))
main()