图片格式转换
QImage 转 Pix
std::shared_ptr<Pix> suiyi::ai::RGB32QImage2Pix(const QImage &im){
Pix * pixs;
// RGB to BGR format
int width = im.width();
int height = im.height();
int depth = im.depth();
// pix per line
int wpl = im.bytesPerLine() / 4;
pixs = pixCreate(width, height, depth);
// safe pix
std::shared_ptr<Pix> safe_pix(pixs,[](Pix *p){pixDestroy(&p);});
pixSetWpl(pixs, wpl);
pixSetColormap(pixs, nullptr);
uint32_t *datas = pixs->data;
// copy data
for (int l = 0; l < height; l++) {
auto *offset = (uint8_t*)(datas + l * wpl);
const auto * img_data = (const uint8_t *)im.scanLine(l);
for(int i = 0; i < wpl; i++) {
int index = i*4;
// A
offset[index] = img_data[index+3];
// B
offset[index+1] = img_data[index+0];
// G
offset[index+2] = img_data[index+1];
// R
offset[index+3] = img_data[index+2];
}
}
const qreal toDPM = 1.0 / 0.0254;
int resolutionX = im.dotsPerMeterX() / toDPM;
int resolutionY = im.dotsPerMeterY() / toDPM;
// if (resolutionX < 300) resolutionX = 300;
// if (resolutionY < 300) resolutionY = 300;
pixSetResolution(pixs, resolutionX, resolutionY);
return safe_pix;
}