用 cxImage 提升 Debian 上的图像处理体验
一 适用场景与收益
二 快速安装与环境准备
三 典型用法与可复用脚本
#include “ximage.h”
#include
CxImage img; int fmt_in = CXIMAGE_FORMAT_UNKNOWN; if (strcasecmp(fmt, “png”) == 0) fmt_in = CXIMAGE_FORMAT_PNG; else if (strcasecmp(fmt, “jpg”) == 0 || strcasecmp(fmt, “jpeg”) == 0) fmt_in = CXIMAGE_FORMAT_JPG; else { std::cerr << “Unsupported input format\n”; return 1; }
if (!img.Load(in, fmt_in)) { std::cerr << "Load failed: " << in << “\n”; return 1; }
int w = img.GetWidth(), h = img.GetHeight(); int new_h = (w > max_w) ? (h * max_w) / w : h; if (!img.Resample(max_w, new_h)) { std::cerr << “Resample failed\n”; return 1; }
int fmt_out = CXIMAGE_FORMAT_UNKNOWN; if (strcasecmp(argv[3], “png”) == 0) fmt_out = CXIMAGE_FORMAT_PNG; else if (strcasecmp(argv[3], “jpg”) == 0 || strcasecmp(argv[3], “jpeg”) == 0) { fmt_out = CXIMAGE_FORMAT_JPG; img.SetJpegQuality(90); // 质量示例:90/100 } else if (strcasecmp(argv[3], “webp”) == 0) fmt_out = CXIMAGE_FORMAT_WEBP;
if (!img.Save(out, fmt_out)) { std::cerr << "Save failed: " << out << “\n”; return 1; } std::cout << “Saved: " << out << " (” << img.GetWidth() << “x” << img.GetHeight() << “)\n”; return 0; }
四 性能与体验优化
五 常见问题与排查