-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImage_converter.m
158 lines (144 loc) · 4.83 KB
/
Image_converter.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
##PC to SD printer, Raphael BOICHOT 2023/08/26
##this must be run with GNU Octave
##just run this script with images into the folder "Images"
clc;
clear;
disp('-----------------------------------------------------------')
disp('|Beware, this code is for GNU Octave ONLY !!! |')
disp('-----------------------------------------------------------')
pkg load image
margin=3;% you can choose the number of blank lines between images here
imagefiles = dir('Images/*.png');% the default format is png, other are ignored
nfiles = length(imagefiles); % Number of files found
fid=fopen('Hex_data.txt','w');
packets=0;
for k=1:1:nfiles
currentfilename = imagefiles(k).name;
disp(['Converting image ',currentfilename,' in progress...'])
[a,map]=imread(['Images/',currentfilename]);
if not(isempty(map));##dealing with indexed images
disp('Indexed image, converting to grayscale');
a=ind2gray(a,map);
end
[height, width, layers]=size(a);
if layers>1##dealing with color images
disp('Color image, converting to grayscale');
a=rgb2gray(a);
[height, width, layers]=size(a);
end
C=unique(a);
if (length(C)<=4 && height==160);##dealing with pixel perfect image, bad orientation
disp('Bad orientation, image rotated');
a=imrotate(a,270);
[heigth, width,layers]=size(a);
end
if (length(C)<=4 && not(width==160));##dealing with pixel perfect upscaled/downscaled images
disp('Image is 2 bpp or less, which is good, but bad size: fixing it');
a=imresize(a,160/width,"nearest");
[heigth, width,layers]=size(a);
end
if (length(C)>4 || not(width==160));##dealing with 8-bit images in general
disp('8-bits image rectified and dithered with Bayer matrices');
a=image_rectifier(a);
[height, width, layers]=size(a);
end
if length(C)==1;##dealing with one color images
disp('Empty image -> neutralization, will print full white');
a=zeros(height, width);
end
if not(rem(height,16)==0);##Fixing images not multiple of 16 pixels
disp('Image height is not a multiple of 16 : fixing image');
C=unique(a);
new_lines=ceil(height/16)*16-height;
color_footer=double(C(end));
footer=color_footer.*ones(new_lines,width, layers);
a=[a;footer];
[height, width, layers]=size(a);
end
[height, width, layers]=size(a);
C=unique(a);
disp(['Buffering image ',currentfilename,' into GB tile data...'])
switch length(C)
case 4;##4 colors, OK
Black=C(1);
Dgray=C(2);
Lgray=C(3);
White=C(4);
case 3;##3 colors, sacrify LG (not well printed)
Black=C(1);
Dgray=C(2);
Lgray=[];
White=C(3);
case 2;##2 colors, sacrify LG and DG
Black=C(1)
Dgray=[];
Lgray=[];
White=C(2);
end
hor_tile=width/8;
vert_tile=height/8;
tile=0;
H=1;
L=1;
H_tile=1;
L_tile=1;
O=[];
y_graph=0;
total_tiles=hor_tile*vert_tile;
for x=1:1:hor_tile
for y=1:1:vert_tile
tile=tile+1;
b=a((H:H+7),(L:L+7));
for i=1:8
for j=1:8
if b(i,j)==Lgray; V1(j)=('1'); V2(j)=('0');end
if b(i,j)==Dgray; V1(j)=('0'); V2(j)=('1');end
if b(i,j)==White; V1(j)=('0'); V2(j)=('0');end
if b(i,j)==Black; V1(j)=('1'); V2(j)=('1');end
end
O=[O,num2str(dec2hex(bin2dec(V1),2),2),' ',num2str(dec2hex(bin2dec(V2),2),2),' '];
end
if tile==40
imagesc(a)
colormap gray
h=rectangle('Position',[1 y_graph 160-1 16],'EdgeColor','r', 'LineWidth',3,'FaceColor', [1, 0, 0]);
drawnow
y_graph=y_graph+16;
O=O(1:end-1);
disp(O)
fprintf(fid,O,'%s');
fprintf(fid,'\n\r');
length(O);
O=[];
tile=0;
##---------------------------------------------------
O=[];
tile=0;
end
L=L+8;
L_tile=L_tile+1;
if L>=width
L=1;
L_tile=1;
H=H+8;
H_tile=H_tile+1;
end
end
end
packets=packets+3;
imagesc(a)
drawnow
##--------printing loop-----------------------------
blank=[];
for i=1:1:640;
blank=[blank,'00 '];
end
blank=blank(1:1:end-1);
for i=1:1:margin
fprintf(fid,blank,'%s');
fprintf(fid,'\n\r');
##---------------------------------------------------
end
end
fclose(fid);
disp('End of conversion')