void TThumbGeneratorImpl::DrawText(TImage& image, size_t x, size_t y, const wstring& wtext, size_t size) { float scale = stbtt_ScaleForPixelHeight(&Font, size); int w, h, xo, yo; unsigned char* r = image.data(0, 0, 0, 0); unsigned char* g = image.data(0, 0, 0, 1); unsigned char* b = image.data(0, 0, 0, 2); size_t pos = x; for (size_t i = 0; i < wtext.size(); i++) { if (iswspace(wtext[i])) { pos += size * 0.3; continue; } unsigned char* d = stbtt_GetCodepointBitmap(&Font, 0, scale, wtext[i], &w, &h, &xo, &yo); for (size_t wx = 0; wx < w; wx++) { for (size_t wy = 0; wy < h; wy++) { size_t cx = pos + wx + xo; size_t cy = y + wy + yo + size; if (cy >= image.height() || cx >= image.width()) { continue; } size_t dcord = image.width() * cy + cx; size_t scord = wy * w + wx; r[dcord] = d[scord]; g[dcord] = d[scord]; b[dcord] = d[scord]; } } pos += w; } }
// store image inside png void SaveImage(const TImage& image, const string& destFile) { vector<unsigned char> result(image.width() * image.height() * 4); unsigned char* d; const unsigned char* r = image.data(0, 0, 0, 0); const unsigned char* g = image.data(0, 0, 0, 1); const unsigned char* b = image.data(0, 0, 0, 2); int sp = image.spectrum(); if (sp == 4) { const unsigned char* a = image.data(0, 0, 0, 3); for (d = result.data(); d < result.data() + result.size();) { *(d++) = *(r++); *(d++) = *(g++); *(d++) = *(b++); *(d++) = *(a++); } } else if (sp == 3) { for (d = result.data(); d < result.data() + result.size();) { *(d++) = *(r++); *(d++) = *(g++); *(d++) = *(b++); *(d++) = 255; } } else { throw UException("failed to save image - wrong type"); } size_t err = lodepng::encode(destFile, result, image.width(), image.height()); if (err) { throw UException(lodepng_error_text(err)); } }
TImage TThumbGeneratorImpl::MakeImageThumb(const string& sourceFile) { // todo: correct thumbs for images width / height rate not only 1:1 TImage image = LoadImageFile(sourceFile); if (image.width() > image.height()) { image = image.get_crop(0, 0, image.height(), image.height(), true); } else if (image.height() > image.width()) { image = image.get_crop(0, 0, image.width(), image.width(), true); } return image.get_resize(Width, Height, -100, -100, 5); }