-
Notifications
You must be signed in to change notification settings - Fork 1
/
embroidery_software.cpp
239 lines (180 loc) · 7.86 KB
/
embroidery_software.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// A Ikic 2020
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdint>
// user #defines for image color processing parameters
#define POSTERIZE_LEVEL 1
#define POSTERIZED_PALETTE_SIZE 60 // assuming <= 60 colors after posterizing
#define DOMINANT_COLORS_NUMBER 3
#define COLORS_TO_EXPORT_NUMBER 2
// user #defines for G-code export parameters
#define G_CODE_HEIGHT_MM 90
#define G_CODE_WIDTH_MM 90
#define G_CODE_Z_MM 15.708
#define G_CODE_STITCH_NUMBER 2
#define G_CODE_DECIMAL_POINTS 3
#define X_INVERSION_TOGGLE 0
#define Y_INVERSION_TOGGLE 0
#define Z_INVERSION_TOGGLE 0
#define CROSS_PATTERN_TOGGLE 1
#define MM_BETWEEN_STITCH_ROWS 0.2
#define STRAIGHT_STITCH_LENGTH_MM 0 // Set to 0 for long stitches
// #defines used in G-code processing (do not modify)
#if X_INVERSION_TOGGLE
#define X_INVERSION_FACTOR 1
#else
#define X_INVERSION_FACTOR -1
#endif
#if Y_INVERSION_TOGGLE
#define Y_INVERSION_FACTOR 1
#else
#define Y_INVERSION_FACTOR -1
#endif
#if Z_INVERSION_TOGGLE
#define Z_INVERSION_FACTOR 1
#else
#define Z_INVERSION_FACTOR -1
#endif
int32_t imageHeightInPixels = 0;
int32_t imageWidthInPixels = 0;
#define PIXEL_HEIGHT_MM ((double)G_CODE_HEIGHT_MM / (double)imageHeightInPixels)
#define HALF_IMAGE_HEIGHT_IN_PIXELS (imageHeightInPixels / 2)
#define HALF_IMAGE_WIDTH_IN_PIXELS (imageWidthInPixels / 2)
#include "class_header.h"
#include "function_header.h"
int main()
{
std::fstream imageFile;
imageFile.open ("ladyLiberty.bmp", std::ios::in | std::ios::out | std::ios::binary);
if (!imageFile)
{
std::cout << "Error loading image.";
return EXIT_FAILURE;
}
char * fillerArray1 = new char [10];
imageFile.read(fillerArray1, 10);
char * pixelDataOffset = new char [4];
imageFile.read(pixelDataOffset, 4);
int32_t dataOffset = fourByteValue(pixelDataOffset);
char * headerSize = new char [4];
imageFile.read(headerSize, 4);
char * imageWidth = new char [4];
imageFile.read(imageWidth, 4);
imageWidthInPixels = fourByteValue(imageWidth);
char * imageHeight = new char [4];
imageFile.read(imageHeight, 4);
imageHeightInPixels = fourByteValue(imageHeight);
char * Planes = new char [2];
imageFile.read(Planes, 2);
char * bitsPerPixel = new char [2];
imageFile.read(bitsPerPixel, 2);
int16_t bytesPerPixel = twoByteValue(bitsPerPixel) / 8;
char * fillerArray2 = new char [24];
imageFile.read(fillerArray2, 24);
char * fillerArray3 = new char [(dataOffset - (40+14))];
imageFile.read(fillerArray3, (dataOffset - (40+14)) );
delete [] fillerArray1;
delete [] pixelDataOffset;
delete [] headerSize;
delete [] imageWidth;
delete [] imageHeight;
delete [] Planes;
delete [] bitsPerPixel;
delete [] fillerArray2;
delete [] fillerArray3;
fillerArray1 = nullptr;
pixelDataOffset = nullptr;
headerSize = nullptr;
imageWidth = nullptr;
imageHeight = nullptr;
Planes = nullptr;
bitsPerPixel = nullptr;
fillerArray2 = nullptr;
fillerArray3 = nullptr;
PixelData ** pixelArray = new PixelData * [imageHeightInPixels];
for (int32_t heightCount = 0; heightCount < imageHeightInPixels; heightCount++)
pixelArray[heightCount] = new PixelData[imageWidthInPixels];
PixelData * posterizedPalette = new PixelData [POSTERIZED_PALETTE_SIZE];
char pixelBuffer[4];
for (int32_t heightCount = imageHeightInPixels - 1; heightCount >= 0; heightCount--)
for (int32_t widthCount = 0; widthCount < imageWidthInPixels; widthCount++)
{
imageFile.read(pixelBuffer, bytesPerPixel);
pixelArray[heightCount][widthCount].set_BGR(pixelBuffer);
// GIMP posterizing method
double blueDecimal = pixelArray[heightCount][widthCount].get_blue() / 255.0;
double greenDecimal = pixelArray[heightCount][widthCount].get_green() / 255.0;
double redDecimal = pixelArray[heightCount][widthCount].get_red() / 255.0;
uint8_t bluePosterized = 255 * rint(blueDecimal*POSTERIZE_LEVEL) / POSTERIZE_LEVEL;
uint8_t greenPosterized = 255 * rint(greenDecimal*POSTERIZE_LEVEL) / POSTERIZE_LEVEL;
uint8_t redPosterized = 255 * rint(redDecimal*POSTERIZE_LEVEL) / POSTERIZE_LEVEL;
pixelArray[heightCount][widthCount].set_blueNumber(bluePosterized);
pixelArray[heightCount][widthCount].set_greenNumber(greenPosterized);
pixelArray[heightCount][widthCount].set_redNumber(redPosterized);
populatePaletteWithPixel(posterizedPalette, bluePosterized, greenPosterized, redPosterized);
}
PixelData * dominantColorsArray = new PixelData[DOMINANT_COLORS_NUMBER];
obtainDominantColors(posterizedPalette, dominantColorsArray, DOMINANT_COLORS_NUMBER);
delete [] posterizedPalette;
for (int32_t heightCount = (imageHeightInPixels - 1); heightCount >= 0; heightCount--)
for (int32_t widthCount = 0; widthCount < imageWidthInPixels; widthCount++)
setToClosestDominantColor(DOMINANT_COLORS_NUMBER, pixelArray[heightCount][widthCount], dominantColorsArray);
imageFile.close();
PixelData * exportColors = new PixelData[COLORS_TO_EXPORT_NUMBER];
/* exportColors consists of colors from dominantColorsArray
and their respective red, green and blue values. */
//White:
exportColors[0].set_blueNumber(255);
exportColors[0].set_greenNumber(255);
exportColors[0].set_redNumber(255);
// Red:
exportColors[1].set_blueNumber(0);
exportColors[1].set_greenNumber(0);
exportColors[1].set_redNumber(255);
int32_t * pocketsPerColor = new int32_t[COLORS_TO_EXPORT_NUMBER];
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
pocketsPerColor[color] = 1;
Pocket ** assortedPocketsOfColors = new Pocket * [COLORS_TO_EXPORT_NUMBER];
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
assortedPocketsOfColors[color] = new Pocket[pocketsPerColor[color]];
pocketGenerator(exportColors, assortedPocketsOfColors, pocketsPerColor, pixelArray,
imageHeightInPixels, imageWidthInPixels);
delete [] exportColors;
for (int32_t heightCount = 0; heightCount < imageHeightInPixels; heightCount++)
delete [] pixelArray[heightCount];
delete [] pixelArray;
pixelArray = nullptr;
int32_t * shapesPerColor = new int32_t [COLORS_TO_EXPORT_NUMBER];
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
shapesPerColor[color] = 1;
int32_t ** pocketsPerShape = new int32_t * [COLORS_TO_EXPORT_NUMBER];
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
{
pocketsPerShape[color] = new int32_t [shapesPerColor[color]];
for (int32_t shape = 0; shape < shapesPerColor[color]; shape++)
pocketsPerShape[color][shape] = 0;
}
Pocket *** shapePockets = new Pocket ** [COLORS_TO_EXPORT_NUMBER];
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
{
shapePockets[color] = new Pocket * [1];
shapePockets[color][0] = new Pocket [1];
}
pocketToShapeOrganizer(shapePockets, assortedPocketsOfColors, pocketsPerColor,
pocketsPerShape, shapesPerColor);
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
delete [] assortedPocketsOfColors[color];
delete [] assortedPocketsOfColors;
assortedPocketsOfColors = nullptr;
for (uint8_t color = 0; color < COLORS_TO_EXPORT_NUMBER; color++)
for (int32_t shape = 0; shape < shapesPerColor[color]; shape++)
sortPocketOrderOfShape(shapePockets, pocketsPerShape, color, shape);
std::ofstream gCodeExportFile("ladyLibertyG_CodeOutput.txt");
gCodeExportFile << "G90" << std::endl << "G21" << std::endl << "G0 X0.0 Y0.0" << std::endl;
for (uint8_t shape = 0; shape < shapesPerColor[0]; shape++)
exportShapeG_Code(gCodeExportFile, shapePockets, pocketsPerShape, 0, shape);
return EXIT_SUCCESS;
}