1
0

ImageExport.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "ImageExport.h"
  2. #include "Bitmap.h"
  3. #include <boost/endian/buffers.hpp>
  4. extern "C" {
  5. # include <png.h>
  6. }
  7. #include <cstdio>
  8. #ifdef WITH_LIBJPEG
  9. extern "C" {
  10. # include <jpeglib.h>
  11. }
  12. #endif // WITH_LIBJPEG
  13. namespace alm
  14. {
  15. const std::vector<ImageFormat> supportedImageFormats = {
  16. ImageFormat::BMP,
  17. ImageFormat::PNG,
  18. #ifdef WITH_LIBJPEG
  19. ImageFormat::JPEG,
  20. #endif
  21. };
  22. bool supportsImageFormat(ImageFormat imgf)
  23. {
  24. for (auto f : supportedImageFormats) {
  25. if (imgf == f)
  26. return true;
  27. }
  28. return false;
  29. }
  30. ImageExportException::ImageExportException(const std::string& err) :
  31. std::runtime_error{ err }
  32. {
  33. }
  34. void exportImage(const ImageExportInfo& iei, std::function<void(float)> progressCallback)
  35. {
  36. auto hasSuffix = [] (const std::string& path, const std::string& suffix) -> bool
  37. {
  38. return path.size() >= suffix.size() &&
  39. path.compare(path.size() - suffix.size(),
  40. suffix.size(), suffix) == 0;
  41. };
  42. if (hasSuffix(iei.path, ".bmp") ||
  43. hasSuffix(iei.path, ".dib")) {
  44. exportBmp(iei, progressCallback);
  45. }
  46. else if (hasSuffix(iei.path, ".png")) {
  47. exportPng(iei, progressCallback);
  48. }
  49. #ifdef WITH_LIBJPEG
  50. else if (hasSuffix(iei.path, ".jpg") ||
  51. hasSuffix(iei.path, ".jpeg")) {
  52. exportJpeg(iei, progressCallback);
  53. }
  54. #endif // WITH_LIBJPEG
  55. else {
  56. throw ImageExportException{ "Could not guess image format" };
  57. }
  58. }
  59. void exportPng(const ImageExportInfo& iei, std::function<void(float)> progressCallback)
  60. {
  61. if (iei.generator == nullptr) {
  62. throw "no generator";
  63. }
  64. progressCallback(0.0f);
  65. mnd::MandelGenerator& generator = *iei.generator;
  66. FILE* file = fopen(iei.path.c_str(), "wb");
  67. if(!file)
  68. throw ImageExportException{ std::string("could not open file '") + iei.path + "'" };
  69. png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  70. if (!png)
  71. throw ImageExportException{ "error creating png write struct" };
  72. png_infop info = png_create_info_struct(png);
  73. if (!info)
  74. throw ImageExportException{ "error creating png info struct" };
  75. if (setjmp(png_jmpbuf(png))) {
  76. png_destroy_write_struct(&png, &info);
  77. throw ImageExportException{ "error while creating png" };
  78. }
  79. png_init_io(png, file);
  80. const long width = iei.drawInfo.bWidth;
  81. const long height = iei.drawInfo.bHeight;
  82. png_set_IHDR(
  83. png,
  84. info,
  85. width, height,
  86. 8,
  87. PNG_COLOR_TYPE_RGB,
  88. PNG_INTERLACE_NONE,
  89. PNG_COMPRESSION_TYPE_DEFAULT,
  90. PNG_FILTER_TYPE_DEFAULT
  91. );
  92. png_write_info(png, info);
  93. long chunkHeight = height / 20;
  94. if (chunkHeight < 1)
  95. chunkHeight = 1;
  96. while (width * chunkHeight > 512 * 512)
  97. chunkHeight /= 2;
  98. auto rowPointers = std::make_unique<png_byte*[]>(chunkHeight);
  99. for (long chunkY = 0; chunkY < height; chunkY += chunkHeight) {
  100. auto minimum = [] (const auto& a, const auto& b) { return a < b ? a : b; };
  101. long tmpHeight = minimum(chunkHeight, height - chunkY);
  102. mnd::MandelInfo chunkInfo = iei.drawInfo;
  103. chunkInfo.bHeight = tmpHeight;
  104. chunkInfo.view.y += chunkInfo.view.height * chunkY / height;
  105. chunkInfo.view.height *= mnd::Real(tmpHeight) / height;
  106. Bitmap<float> chunk(width, tmpHeight);
  107. generator.generate(chunkInfo, chunk.pixels.get());
  108. Bitmap<RGBColor> coloredChunk = chunk.map<RGBColor>([&iei] (float i) {
  109. return i >= iei.drawInfo.maxIter ? RGBColor{ 0, 0, 0 } : iei.gradient.get(i);
  110. });
  111. for (long i = 0; i < tmpHeight; i++) {
  112. rowPointers[i] = reinterpret_cast<png_byte*>(&coloredChunk.get(0, i));
  113. }
  114. png_write_rows(png, &rowPointers[0], tmpHeight);
  115. if (chunkY < height)
  116. progressCallback(100.0f * chunkY / height);
  117. }
  118. png_write_end(png, NULL);
  119. fclose(file);
  120. png_destroy_write_struct(&png, &info);
  121. progressCallback(100.0f);
  122. }
  123. #ifdef WITH_LIBJPEG
  124. void exportJpeg(const ImageExportInfo& iei, std::function<void(float)> progressCallback)
  125. {
  126. if (iei.generator == nullptr) {
  127. throw "no generator";
  128. }
  129. progressCallback(0.0f);
  130. mnd::MandelGenerator& generator = *iei.generator;
  131. FILE* file = fopen(iei.path.c_str(), "wb");
  132. if(!file)
  133. throw ImageExportException{ std::string("could not open file '") + iei.path + "'" };
  134. const long width = iei.drawInfo.bWidth;
  135. const long height = iei.drawInfo.bHeight;
  136. struct jpeg_compress_struct jpegInfo;
  137. struct jpeg_error_mgr jerr;
  138. int rowLength = width * 3;
  139. jpegInfo.err = jpeg_std_error(&jerr);
  140. jerr.error_exit = [](j_common_ptr cinfo) {
  141. auto errmsg = std::make_unique<char[]>(JMSG_LENGTH_MAX);
  142. (cinfo->err->format_message)(cinfo, errmsg.get());
  143. throw ImageExportException{ std::string("libjpeg error: ") + errmsg.get() };
  144. };
  145. jpeg_create_compress(&jpegInfo);
  146. jpeg_stdio_dest(&jpegInfo, file);
  147. jpegInfo.image_width = width;
  148. jpegInfo.image_height = height;
  149. jpegInfo.input_components = 3;
  150. jpegInfo.in_color_space = JCS_RGB;
  151. jpeg_set_defaults(&jpegInfo);
  152. jpeg_set_quality(&jpegInfo, iei.options.jpegQuality, TRUE);
  153. jpeg_start_compress(&jpegInfo, TRUE);
  154. long chunkHeight = height / 20;
  155. if (chunkHeight < 1)
  156. chunkHeight = 1;
  157. while (width * chunkHeight > 512 * 512)
  158. chunkHeight /= 2;
  159. auto rowPointers = std::make_unique<JSAMPROW[]>(chunkHeight);
  160. for (long chunkY = 0; chunkY < height; chunkY += chunkHeight) {
  161. auto minimum = [] (const auto& a, const auto& b) { return a < b ? a : b; };
  162. long tmpHeight = minimum(chunkHeight, height - chunkY);
  163. mnd::MandelInfo chunkInfo = iei.drawInfo;
  164. chunkInfo.bHeight = tmpHeight;
  165. chunkInfo.view.y += chunkInfo.view.height * chunkY / height;
  166. chunkInfo.view.height *= mnd::Real(tmpHeight) / height;
  167. Bitmap<float> chunk(width, tmpHeight);
  168. generator.generate(chunkInfo, chunk.pixels.get());
  169. Bitmap<RGBColor> coloredChunk = chunk.map<RGBColor>([&iei] (float i) {
  170. return i >= iei.drawInfo.maxIter ? RGBColor{ 0, 0, 0 } : iei.gradient.get(i);
  171. });
  172. for (long i = 0; i < tmpHeight; i++) {
  173. rowPointers[i] = reinterpret_cast<JSAMPROW>(&coloredChunk.get(0, i));
  174. }
  175. int written = jpeg_write_scanlines(&jpegInfo, rowPointers.get(), tmpHeight);
  176. if (written < tmpHeight) {
  177. throw ImageExportException{ "error writing jpeg rows" };
  178. }
  179. if (chunkY < height)
  180. progressCallback(100.0f * chunkY / height);
  181. }
  182. jpeg_finish_compress(&jpegInfo);
  183. fclose(file);
  184. jpeg_destroy_compress(&jpegInfo);
  185. progressCallback(100.0f);
  186. }
  187. #endif // WITH_LIBJPEG
  188. void exportBmp(const ImageExportInfo& iei, std::function<void(float)> progressCallback)
  189. {
  190. if (iei.generator == nullptr) {
  191. throw "no generator";
  192. }
  193. progressCallback(0.0f);
  194. mnd::MandelGenerator& generator = *iei.generator;
  195. FILE* file = fopen(iei.path.c_str(), "wb");
  196. if(!file)
  197. throw ImageExportException{ std::string("could not open file '") + iei.path + "'" };
  198. const long width = iei.drawInfo.bWidth;
  199. const long height = iei.drawInfo.bHeight;
  200. // bmp needs rows to be 4-byte-aligned
  201. const long rowLength = (width * 3 + 3) & ~0x03;
  202. using namespace boost::endian;
  203. const int fileHeaderSize = 14;
  204. const int infoHeaderSize = 40;
  205. const int pixelSize = rowLength * height;
  206. // file header
  207. little_int8_buf_t fhType[2];
  208. little_uint32_buf_t fhSize;
  209. little_uint16_buf_t fhReserved1;
  210. little_uint16_buf_t fhReserved2;
  211. little_uint32_buf_t fhOffset;
  212. fhType[0] = 0x42; // 'B'
  213. fhType[1] = 0x4D; // 'M'
  214. fhSize = fileHeaderSize + infoHeaderSize + pixelSize;
  215. fhReserved1 = 0;
  216. fhReserved1 = 2;
  217. fhOffset = fileHeaderSize + infoHeaderSize;
  218. // info header
  219. little_uint32_buf_t ihSize;
  220. little_int32_buf_t ihWidth;
  221. little_int32_buf_t ihHeight; // for top-down, height must be negative
  222. little_uint16_buf_t ihPlanes;
  223. little_uint16_buf_t ihBitCount;
  224. little_uint32_buf_t ihCompression; // 0 = no compression
  225. little_uint32_buf_t ihSizeImage; // for uncompressed bmps 0 can be set here
  226. little_int32_buf_t ihXPixelsPerMeter;
  227. little_int32_buf_t ihYPixelsPerMeter;
  228. little_uint32_buf_t ihClrUsed;
  229. little_uint32_buf_t ihClrImportant;
  230. ihSize = infoHeaderSize;
  231. ihWidth = width;
  232. ihHeight = -height; // for top-down, height must be negative
  233. ihPlanes = 1;
  234. ihBitCount = 24;
  235. ihCompression = 0; // 0 = no compression
  236. ihSizeImage = 0; // for uncompressed bmps 0 can be set here
  237. ihXPixelsPerMeter = 0;
  238. ihYPixelsPerMeter = 0;
  239. ihClrUsed = 0;
  240. ihClrImportant = 0;
  241. auto writeCmp = [&file] (auto& buf) {
  242. std::fwrite(&buf, sizeof(buf), 1, file);
  243. };
  244. writeCmp(fhType[0]);
  245. writeCmp(fhType[1]);
  246. writeCmp(fhSize);
  247. writeCmp(fhReserved1);
  248. writeCmp(fhReserved2);
  249. writeCmp(fhOffset);
  250. writeCmp(ihSize);
  251. writeCmp(ihWidth);
  252. writeCmp(ihHeight);
  253. writeCmp(ihPlanes);
  254. writeCmp(ihBitCount);
  255. writeCmp(ihCompression);
  256. writeCmp(ihSizeImage);
  257. writeCmp(ihXPixelsPerMeter);
  258. writeCmp(ihYPixelsPerMeter);
  259. writeCmp(ihClrUsed);
  260. writeCmp(ihClrImportant);
  261. long chunkHeight = height / 20;
  262. if (chunkHeight < 1)
  263. chunkHeight = 1;
  264. while (width * chunkHeight > 512 * 512)
  265. chunkHeight /= 2;
  266. for (long chunkY = 0; chunkY < height; chunkY += chunkHeight) {
  267. auto minimum = [] (const auto& a, const auto& b) { return a < b ? a : b; };
  268. long tmpHeight = minimum(chunkHeight, height - chunkY);
  269. mnd::MandelInfo chunkInfo = iei.drawInfo;
  270. chunkInfo.bHeight = tmpHeight;
  271. chunkInfo.view.y += chunkInfo.view.height * chunkY / height;
  272. chunkInfo.view.height *= mnd::Real(tmpHeight) / height;
  273. Bitmap<float> chunk(width, tmpHeight);
  274. generator.generate(chunkInfo, chunk.pixels.get());
  275. for (long i = 0; i < tmpHeight; i++) {
  276. for (long j = 0; j < width; j++) {
  277. float iters = chunk.get(j, i);
  278. RGBColor color =
  279. iters >= iei.drawInfo.maxIter ?
  280. RGBColor{ 0, 0, 0 } :
  281. iei.gradient.get(iters);
  282. uint8_t bgr[3] = { color.b, color.g, color.r };
  283. fwrite(bgr, 1, 3, file);
  284. }
  285. // write line padding
  286. const uint8_t zeroes[] = { 0, 0, 0, 0 };
  287. fwrite(zeroes, 1, rowLength - width * 3, file);
  288. }
  289. if (chunkY < height)
  290. progressCallback(100.0f * chunkY / height);
  291. }
  292. fflush(file);
  293. fclose(file);
  294. progressCallback(100.0f);
  295. }
  296. }