#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); }