aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Embroidery/Tango.Embroidery/Adapter.cpp
blob: 3c68c0b5748717ddf169899f7fbfd23ad7ee3ee7 (plain)
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
#include "Adapter.h"
#include "emb-pattern.h"
#include "PMR\Embroidery\AnalyzeInput.pb-c.h"
#include "PMR\Embroidery\AnalyzeOutput.pb-c.h"
#include "PMR\Embroidery\EmbroideryFile.pb-c.h"
#include "PMR\Embroidery\Stitch.pb-c.h"
#include "PMR\Embroidery\StitchFlag.pb-c.h"
#include "PMR\Embroidery\StitchColor.pb-c.h"
#include "PMR\Embroidery\Extents.pb-c.h"
#include "PMR\Embroidery\ConvertFileInput.pb-c.h"
#include "PMR\Embroidery\ConvertFileOutput.pb-c.h"



Adapter::Adapter()
{
}


Adapter::~Adapter()
{
}

size_t Adapter::AnalyzeEmbroideryFile(uint8_t * input_buffer, size_t input_buffer_size, uint8_t *& output_buffer)
{
	//Unpack input...
	AnalyzeInput *input = analyze_input__unpack(NULL, input_buffer_size, input_buffer);

	//Initialize Output...
	AnalyzeOutput* output = (AnalyzeOutput*)malloc(sizeof(AnalyzeOutput));
	analyze_output__init(output);

	EmbPattern* p = embPattern_create();

	int ok = embPattern_read(p, input->filepath);

	if (ok)
	{
		int stitchCount = embStitchList_count(p->stitchList);
		int colorCount = embThreadList_count(p->threadList);

		EmbroideryFile* embroideryFile = (EmbroideryFile*)malloc(sizeof(EmbroideryFile));
		embroidery_file__init(embroideryFile);

		embroideryFile->colorcount = colorCount;
		embroideryFile->has_colorcount = true;
		embroideryFile->n_colors = colorCount;
		embroideryFile->stitchcount = stitchCount;
		embroideryFile->has_stitchcount = true;
		embroideryFile->n_stitches = stitchCount;

		embroideryFile->colors = (StitchColor**)malloc(sizeof(StitchColor*) * colorCount);
		embroideryFile->stitches = (Stitch**)malloc(sizeof(Stitch*) * stitchCount);

		for (size_t i = 0; i < colorCount; i++)
		{
			StitchColor* color = (StitchColor*)malloc(sizeof(StitchColor));
			stitch_color__init(color);

			EmbThread t = embThreadList_getAt(p->threadList, i);

			color->red = (int)t.color.r;
			color->has_red = true;
			color->green = (int)t.color.g;
			color->has_green = true;
			color->blue = (int)t.color.b;
			color->has_blue = true;

			embroideryFile->colors[i] = color;
		}

		for (size_t i = 0; i < stitchCount; i++)
		{
			EmbStitch intStitch = embStitchList_getAt(p->stitchList, i);

			Stitch* outStitch = (Stitch*)malloc(sizeof(Stitch));
			stitch__init(outStitch);
			outStitch->xx = intStitch.xx;
			outStitch->has_xx = true;
			outStitch->yy = intStitch.yy;
			outStitch->has_yy = true;
			outStitch->flag = (StitchFlag)intStitch.flags;
			outStitch->has_flag = true;
			outStitch->colorindex = intStitch.color;
			outStitch->has_colorindex = true;

			embroideryFile->stitches[i] = outStitch;
		}

		output->embroideryfile = embroideryFile;
	}

	//Pack output...
	output_buffer = (uint8_t*)malloc(analyze_output__get_packed_size(output));

	return analyze_output__pack(output, output_buffer);
}

size_t Adapter::ConvertFile(uint8_t * input_buffer, size_t input_buffer_size, uint8_t *& output_buffer)
{
	//Unpack input...
	ConvertFileInput *input = convert_file_input__unpack(NULL, input_buffer_size, input_buffer);

	//Initialize Output...
	ConvertFileOutput* output = (ConvertFileOutput*)malloc(sizeof(ConvertFileOutput));
	convert_file_output__init(output);

	EmbPattern* p = 0;
	int successful = 0;

	output->has_successful = true;

	p = embPattern_create(); 

	output->successful = embPattern_read(p, input->sourcefile);

	if (output->successful)
	{
		output->successful = embPattern_write(p, input->targetfile);
		embPattern_free(p);
	}

	//Pack output...
	output_buffer = (uint8_t*)malloc(convert_file_output__get_packed_size(output));

	return convert_file_output__pack(output, output_buffer);
}