process_pipeline.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #include "process_pipeline.h"
  2. #include "pipeline.h"
  3. #include "zbar_pipeline.h"
  4. #include "io_pipeline.h"
  5. #include "main.h"
  6. #include "config.h"
  7. #include "gles2_debayer.h"
  8. #include <tiffio.h>
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <gtk/gtk.h>
  12. #include "gl_util.h"
  13. #include <sys/mman.h>
  14. #define TIFFTAG_FORWARDMATRIX1 50964
  15. static const float colormatrix_srgb[] = { 3.2409, -1.5373, -0.4986, -0.9692, 1.8759,
  16. 0.0415, 0.0556, -0.2039, 1.0569 };
  17. static MPPipeline *pipeline;
  18. static char burst_dir[23];
  19. static char processing_script[512];
  20. static volatile bool is_capturing = false;
  21. static volatile int frames_processed = 0;
  22. static volatile int frames_received = 0;
  23. static const struct mp_camera_config *camera;
  24. static int camera_rotation;
  25. static MPCameraMode mode;
  26. static int burst_length;
  27. static int captures_remaining = 0;
  28. static int preview_width;
  29. static int preview_height;
  30. static int device_rotation;
  31. static int output_buffer_width = -1;
  32. static int output_buffer_height = -1;
  33. // static bool gain_is_manual;
  34. static int gain;
  35. static int gain_max;
  36. static bool exposure_is_manual;
  37. static int exposure;
  38. static bool save_dng;
  39. static char capture_fname[255];
  40. static void
  41. register_custom_tiff_tags(TIFF *tif)
  42. {
  43. static const TIFFFieldInfo custom_fields[] = {
  44. { TIFFTAG_FORWARDMATRIX1, -1, -1, TIFF_SRATIONAL, FIELD_CUSTOM, 1, 1,
  45. "ForwardMatrix1" },
  46. };
  47. // Add missing dng fields
  48. TIFFMergeFieldInfo(tif, custom_fields,
  49. sizeof(custom_fields) / sizeof(custom_fields[0]));
  50. }
  51. static bool
  52. find_processor(char *script)
  53. {
  54. char filename[] = "postprocess.sh";
  55. // Check postprocess.sh in the current working directory
  56. sprintf(script, "./data/%s", filename);
  57. if (access(script, F_OK) != -1) {
  58. sprintf(script, "./data/%s", filename);
  59. printf("Found postprocessor script at %s\n", script);
  60. return true;
  61. }
  62. // Check for a script in XDG_CONFIG_HOME
  63. sprintf(script, "%s/megapixels/%s", g_get_user_config_dir(), filename);
  64. if (access(script, F_OK) != -1) {
  65. printf("Found postprocessor script at %s\n", script);
  66. return true;
  67. }
  68. // Check user overridden /etc/megapixels/postprocessor.sh
  69. sprintf(script, "%s/megapixels/%s", SYSCONFDIR, filename);
  70. if (access(script, F_OK) != -1) {
  71. printf("Found postprocessor script at %s\n", script);
  72. return true;
  73. }
  74. // Check packaged /usr/share/megapixels/postprocessor.sh
  75. sprintf(script, "%s/megapixels/%s", DATADIR, filename);
  76. if (access(script, F_OK) != -1) {
  77. printf("Found postprocessor script at %s\n", script);
  78. return true;
  79. }
  80. return false;
  81. }
  82. static void
  83. setup(MPPipeline *pipeline, const void *data)
  84. {
  85. TIFFSetTagExtender(register_custom_tiff_tags);
  86. if (!find_processor(processing_script)) {
  87. g_printerr("Could not find any post-process script\n");
  88. exit(1);
  89. }
  90. }
  91. void
  92. mp_process_pipeline_start()
  93. {
  94. pipeline = mp_pipeline_new();
  95. mp_pipeline_invoke(pipeline, setup, NULL, 0);
  96. mp_zbar_pipeline_start();
  97. }
  98. void
  99. mp_process_pipeline_stop()
  100. {
  101. mp_pipeline_free(pipeline);
  102. mp_zbar_pipeline_stop();
  103. }
  104. void
  105. mp_process_pipeline_sync()
  106. {
  107. mp_pipeline_sync(pipeline);
  108. }
  109. #define NUM_BUFFERS 4
  110. struct _MPProcessPipelineBuffer {
  111. GLuint texture_id;
  112. _Atomic(int) refcount;
  113. };
  114. static MPProcessPipelineBuffer output_buffers[NUM_BUFFERS];
  115. void
  116. mp_process_pipeline_buffer_ref(MPProcessPipelineBuffer *buf)
  117. {
  118. ++buf->refcount;
  119. }
  120. void
  121. mp_process_pipeline_buffer_unref(MPProcessPipelineBuffer *buf)
  122. {
  123. --buf->refcount;
  124. }
  125. uint32_t
  126. mp_process_pipeline_buffer_get_texture_id(MPProcessPipelineBuffer *buf)
  127. {
  128. return buf->texture_id;
  129. }
  130. static GLES2Debayer *gles2_debayer = NULL;
  131. static GdkGLContext *context;
  132. #define RENDERDOC
  133. #ifdef RENDERDOC
  134. #include <renderdoc/app.h>
  135. extern RENDERDOC_API_1_1_2 *rdoc_api;
  136. #endif
  137. static void
  138. init_gl(MPPipeline *pipeline, GdkSurface **surface)
  139. {
  140. GError *error = NULL;
  141. context = gdk_surface_create_gl_context(*surface, &error);
  142. if (context == NULL) {
  143. printf("Failed to initialize OpenGL context: %s\n", error->message);
  144. g_clear_error(&error);
  145. return;
  146. }
  147. gdk_gl_context_set_use_es(context, true);
  148. gdk_gl_context_set_required_version(context, 2, 0);
  149. gdk_gl_context_set_forward_compatible(context, false);
  150. #ifdef DEBUG
  151. gdk_gl_context_set_debug_enabled(context, true);
  152. #else
  153. gdk_gl_context_set_debug_enabled(context, false);
  154. #endif
  155. gdk_gl_context_realize(context, &error);
  156. if (error != NULL) {
  157. printf("Failed to create OpenGL context: %s\n", error->message);
  158. g_clear_object(&context);
  159. g_clear_error(&error);
  160. return;
  161. }
  162. gdk_gl_context_make_current(context);
  163. check_gl();
  164. // Make a VAO for OpenGL
  165. if (!gdk_gl_context_get_use_es(context)) {
  166. GLuint vao;
  167. glGenVertexArrays(1, &vao);
  168. glBindVertexArray(vao);
  169. check_gl();
  170. }
  171. gles2_debayer = gles2_debayer_new(MP_PIXEL_FMT_BGGR8);
  172. check_gl();
  173. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  174. check_gl();
  175. gles2_debayer_use(gles2_debayer);
  176. for (size_t i = 0; i < NUM_BUFFERS; ++i) {
  177. glGenTextures(1, &output_buffers[i].texture_id);
  178. glBindTexture(GL_TEXTURE_2D, output_buffers[i].texture_id);
  179. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  180. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  181. }
  182. glBindTexture(GL_TEXTURE_2D, 0);
  183. gboolean is_es = gdk_gl_context_get_use_es(context);
  184. int major, minor;
  185. gdk_gl_context_get_version(context, &major, &minor);
  186. printf("Initialized %s %d.%d\n", is_es ? "OpenGL ES" : "OpenGL", major, minor);
  187. }
  188. void
  189. mp_process_pipeline_init_gl(GdkSurface *surface)
  190. {
  191. mp_pipeline_invoke(pipeline, (MPPipelineCallback) init_gl, &surface, sizeof(GdkSurface *));
  192. }
  193. static GdkTexture *
  194. process_image_for_preview(const uint8_t *image)
  195. {
  196. #ifdef PROFILE_DEBAYER
  197. clock_t t1 = clock();
  198. #endif
  199. // Pick an available buffer
  200. MPProcessPipelineBuffer *output_buffer = NULL;
  201. for (size_t i = 0; i < NUM_BUFFERS; ++i) {
  202. if (output_buffers[i].refcount == 0) {
  203. output_buffer = &output_buffers[i];
  204. }
  205. }
  206. if (output_buffer == NULL) {
  207. return NULL;
  208. }
  209. assert(output_buffer != NULL);
  210. #ifdef RENDERDOC
  211. if (rdoc_api) rdoc_api->StartFrameCapture(NULL, NULL);
  212. #endif
  213. // Copy image to a GL texture. TODO: This can be avoided
  214. GLuint input_texture;
  215. glGenTextures(1, &input_texture);
  216. glBindTexture(GL_TEXTURE_2D, input_texture);
  217. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  218. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  219. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  220. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  221. glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, mode.width, mode.height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, image);
  222. check_gl();
  223. gles2_debayer_process(
  224. gles2_debayer, output_buffer->texture_id, input_texture);
  225. check_gl();
  226. glFinish();
  227. glDeleteTextures(1, &input_texture);
  228. #ifdef PROFILE_DEBAYER
  229. clock_t t2 = clock();
  230. printf("process_image_for_preview %fms\n", (float)(t2 - t1) / CLOCKS_PER_SEC * 1000);
  231. #endif
  232. #ifdef RENDERDOC
  233. if(rdoc_api) rdoc_api->EndFrameCapture(NULL, NULL);
  234. #endif
  235. mp_process_pipeline_buffer_ref(output_buffer);
  236. mp_main_set_preview(output_buffer);
  237. // Create a thumbnail from the preview for the last capture
  238. GdkTexture *thumb = NULL;
  239. if (captures_remaining == 1) {
  240. printf("Making thumbnail\n");
  241. size_t size = output_buffer_width * output_buffer_height * sizeof(uint32_t);
  242. uint32_t *data = g_malloc_n(size, 1);
  243. glReadPixels(0, 0, output_buffer_width, output_buffer_height, GL_RGBA, GL_UNSIGNED_BYTE, data);
  244. check_gl();
  245. // Flip vertically
  246. for (size_t y = 0; y < output_buffer_height / 2; ++y) {
  247. for (size_t x = 0; x < output_buffer_width; ++x) {
  248. uint32_t tmp = data[(output_buffer_height - y - 1) * output_buffer_width + x];
  249. data[(output_buffer_height - y - 1) * output_buffer_width + x] = data[y * output_buffer_width + x];
  250. data[y * output_buffer_width + x] = tmp;
  251. }
  252. }
  253. thumb = gdk_memory_texture_new(
  254. output_buffer_width,
  255. output_buffer_height,
  256. GDK_MEMORY_R8G8B8A8,
  257. g_bytes_new_take(data, size),
  258. output_buffer_width * sizeof(uint32_t));
  259. }
  260. return thumb;
  261. }
  262. static void
  263. process_image_for_capture(const uint8_t *image, int count)
  264. {
  265. time_t rawtime;
  266. time(&rawtime);
  267. struct tm tim = *(localtime(&rawtime));
  268. char datetime[20] = { 0 };
  269. strftime(datetime, 20, "%Y:%m:%d %H:%M:%S", &tim);
  270. char fname[255];
  271. sprintf(fname, "%s/%d.dng", burst_dir, count);
  272. TIFF *tif = TIFFOpen(fname, "w");
  273. if (!tif) {
  274. printf("Could not open tiff\n");
  275. }
  276. // Define TIFF thumbnail
  277. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 1);
  278. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode.width >> 4);
  279. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode.height >> 4);
  280. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
  281. TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
  282. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
  283. TIFFSetField(tif, TIFFTAG_MAKE, mp_get_device_make());
  284. TIFFSetField(tif, TIFFTAG_MODEL, mp_get_device_model());
  285. uint16_t orientation;
  286. if (camera_rotation == 0) {
  287. orientation = camera->mirrored ? ORIENTATION_TOPRIGHT :
  288. ORIENTATION_TOPLEFT;
  289. } else if (camera_rotation == 90) {
  290. orientation = camera->mirrored ? ORIENTATION_RIGHTBOT :
  291. ORIENTATION_LEFTBOT;
  292. } else if (camera_rotation == 180) {
  293. orientation = camera->mirrored ? ORIENTATION_BOTLEFT :
  294. ORIENTATION_BOTRIGHT;
  295. } else {
  296. orientation = camera->mirrored ? ORIENTATION_LEFTTOP :
  297. ORIENTATION_RIGHTTOP;
  298. }
  299. TIFFSetField(tif, TIFFTAG_ORIENTATION, orientation);
  300. TIFFSetField(tif, TIFFTAG_DATETIME, datetime);
  301. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
  302. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  303. TIFFSetField(tif, TIFFTAG_SOFTWARE, "Megapixels");
  304. long sub_offset = 0;
  305. TIFFSetField(tif, TIFFTAG_SUBIFD, 1, &sub_offset);
  306. TIFFSetField(tif, TIFFTAG_DNGVERSION, "\001\001\0\0");
  307. TIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, "\001\0\0\0");
  308. char uniquecameramodel[255];
  309. sprintf(uniquecameramodel, "%s %s", mp_get_device_make(),
  310. mp_get_device_model());
  311. TIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, uniquecameramodel);
  312. if (camera->colormatrix[0]) {
  313. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, camera->colormatrix);
  314. } else {
  315. TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colormatrix_srgb);
  316. }
  317. if (camera->forwardmatrix[0]) {
  318. TIFFSetField(tif, TIFFTAG_FORWARDMATRIX1, 9, camera->forwardmatrix);
  319. }
  320. static const float neutral[] = { 1.0, 1.0, 1.0 };
  321. TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
  322. TIFFSetField(tif, TIFFTAG_CALIBRATIONILLUMINANT1, 21);
  323. // Write black thumbnail, only windows uses this
  324. {
  325. unsigned char *buf =
  326. (unsigned char *)calloc(1, (mode.width >> 4) * 3);
  327. for (int row = 0; row < (mode.height >> 4); row++) {
  328. TIFFWriteScanline(tif, buf, row, 0);
  329. }
  330. free(buf);
  331. }
  332. TIFFWriteDirectory(tif);
  333. // Define main photo
  334. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);
  335. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, mode.width);
  336. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, mode.height);
  337. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, mp_pixel_format_bits_per_pixel(mode.pixel_format));
  338. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);
  339. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
  340. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  341. static const short cfapatterndim[] = { 2, 2 };
  342. TIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, cfapatterndim);
  343. #if (TIFFLIB_VERSION < 20201219) && !LIBTIFF_CFA_PATTERN
  344. TIFFSetField(tif, TIFFTAG_CFAPATTERN, "\002\001\001\000"); // BGGR
  345. #else
  346. TIFFSetField(tif, TIFFTAG_CFAPATTERN, 4, "\002\001\001\000"); // BGGR
  347. #endif
  348. printf("TIFF version %d\n", TIFFLIB_VERSION);
  349. if (camera->whitelevel) {
  350. TIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &camera->whitelevel);
  351. }
  352. if (camera->blacklevel) {
  353. const float blacklevel = camera->blacklevel;
  354. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &blacklevel);
  355. }
  356. TIFFCheckpointDirectory(tif);
  357. printf("Writing frame to %s\n", fname);
  358. for (int row = 0; row < mode.height; row++) {
  359. TIFFWriteScanline(tif, (void *) image + (row * mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width)), row, 0);
  360. }
  361. TIFFWriteDirectory(tif);
  362. // Add an EXIF block to the tiff
  363. TIFFCreateEXIFDirectory(tif);
  364. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  365. if (!exposure_is_manual) {
  366. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  367. } else {
  368. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
  369. }
  370. TIFFSetField(tif, EXIFTAG_EXPOSURETIME,
  371. (mode.frame_interval.numerator /
  372. (float)mode.frame_interval.denominator) /
  373. ((float)mode.height / (float)exposure));
  374. uint16_t isospeed[1];
  375. isospeed[0] = (uint16_t)remap(gain - 1, 0, gain_max, camera->iso_min,
  376. camera->iso_max);
  377. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, isospeed);
  378. TIFFSetField(tif, EXIFTAG_FLASH, 0);
  379. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  380. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  381. if (camera->fnumber) {
  382. TIFFSetField(tif, EXIFTAG_FNUMBER, camera->fnumber);
  383. }
  384. if (camera->focallength) {
  385. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, camera->focallength);
  386. }
  387. if (camera->focallength && camera->cropfactor) {
  388. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM,
  389. (short)(camera->focallength * camera->cropfactor));
  390. }
  391. uint64_t exif_offset = 0;
  392. TIFFWriteCustomDirectory(tif, &exif_offset);
  393. TIFFFreeDirectory(tif);
  394. // Update exif pointer
  395. TIFFSetDirectory(tif, 0);
  396. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  397. TIFFRewriteDirectory(tif);
  398. TIFFClose(tif);
  399. }
  400. static void
  401. post_process_finished(GSubprocess *proc, GAsyncResult *res, GdkTexture *thumb)
  402. {
  403. char *stdout;
  404. g_subprocess_communicate_utf8_finish(proc, res, &stdout, NULL, NULL);
  405. // The last line contains the file name
  406. int end = strlen(stdout);
  407. // Skip the newline at the end
  408. stdout[--end] = '\0';
  409. char *path = path = stdout + end - 1;
  410. do {
  411. if (*path == '\n') {
  412. path++;
  413. break;
  414. }
  415. --path;
  416. } while (path > stdout);
  417. mp_main_capture_completed(thumb, path);
  418. }
  419. static void
  420. process_capture_burst(GdkTexture *thumb)
  421. {
  422. time_t rawtime;
  423. time(&rawtime);
  424. struct tm tim = *(localtime(&rawtime));
  425. char timestamp[30];
  426. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  427. if (g_get_user_special_dir(G_USER_DIRECTORY_PICTURES) != NULL) {
  428. sprintf(capture_fname,
  429. "%s/IMG%s",
  430. g_get_user_special_dir(G_USER_DIRECTORY_PICTURES),
  431. timestamp);
  432. } else if (getenv("XDG_PICTURES_DIR") != NULL) {
  433. sprintf(capture_fname,
  434. "%s/IMG%s",
  435. getenv("XDG_PICTURES_DIR"),
  436. timestamp);
  437. } else {
  438. sprintf(capture_fname,
  439. "%s/Pictures/IMG%s",
  440. getenv("HOME"),
  441. timestamp);
  442. }
  443. char save_dng_s[2] = "0";
  444. if (save_dng) {
  445. save_dng_s[0] = '1';
  446. }
  447. // Start post-processing the captured burst
  448. g_print("Post process %s to %s.ext (save-dng %s)\n", burst_dir, capture_fname, save_dng_s);
  449. GError *error = NULL;
  450. GSubprocess *proc = g_subprocess_new(
  451. G_SUBPROCESS_FLAGS_STDOUT_PIPE,
  452. &error,
  453. processing_script,
  454. burst_dir,
  455. capture_fname,
  456. save_dng_s,
  457. NULL);
  458. if (!proc) {
  459. g_printerr("Failed to spawn postprocess process: %s\n",
  460. error->message);
  461. return;
  462. }
  463. g_subprocess_communicate_utf8_async(
  464. proc,
  465. NULL,
  466. NULL,
  467. (GAsyncReadyCallback)post_process_finished,
  468. thumb);
  469. }
  470. static void
  471. process_image(MPPipeline *pipeline, const MPBuffer *buffer)
  472. {
  473. #ifdef PROFILE_PROCESS
  474. clock_t t1 = clock();
  475. #endif
  476. size_t size =
  477. mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width) *
  478. mode.height;
  479. uint8_t *image = malloc(size);
  480. memcpy(image, buffer->data, size);
  481. mp_io_pipeline_release_buffer(buffer->index);
  482. MPZBarImage *zbar_image = mp_zbar_image_new(image, mode.pixel_format, mode.width, mode.height, camera_rotation, camera->mirrored);
  483. mp_zbar_pipeline_process_image(mp_zbar_image_ref(zbar_image));
  484. #ifdef PROFILE_PROCESS
  485. clock_t t2 = clock();
  486. #endif
  487. GdkTexture *thumb = process_image_for_preview(image);
  488. if (captures_remaining > 0) {
  489. int count = burst_length - captures_remaining;
  490. --captures_remaining;
  491. process_image_for_capture(image, count);
  492. if (captures_remaining == 0) {
  493. assert(thumb);
  494. process_capture_burst(thumb);
  495. } else {
  496. assert(!thumb);
  497. }
  498. } else {
  499. assert(!thumb);
  500. }
  501. mp_zbar_image_unref(zbar_image);
  502. ++frames_processed;
  503. if (captures_remaining == 0) {
  504. is_capturing = false;
  505. }
  506. #ifdef PROFILE_PROCESS
  507. clock_t t3 = clock();
  508. printf("process_image %fms, step 1:%fms, step 2:%fms\n",
  509. (float)(t3 - t1) / CLOCKS_PER_SEC * 1000,
  510. (float)(t2 - t1) / CLOCKS_PER_SEC * 1000,
  511. (float)(t3 - t2) / CLOCKS_PER_SEC * 1000);
  512. #endif
  513. }
  514. void
  515. mp_process_pipeline_process_image(MPBuffer buffer)
  516. {
  517. // If we haven't processed the previous frame yet, drop this one
  518. if (frames_received != frames_processed && !is_capturing) {
  519. mp_io_pipeline_release_buffer(buffer.index);
  520. return;
  521. }
  522. ++frames_received;
  523. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_image, &buffer,
  524. sizeof(MPBuffer));
  525. }
  526. static void
  527. capture()
  528. {
  529. char template[] = "/tmp/megapixels.XXXXXX";
  530. char *tempdir;
  531. tempdir = mkdtemp(template);
  532. if (tempdir == NULL) {
  533. g_printerr("Could not make capture directory %s\n", template);
  534. exit(EXIT_FAILURE);
  535. }
  536. strcpy(burst_dir, tempdir);
  537. captures_remaining = burst_length;
  538. }
  539. void
  540. mp_process_pipeline_capture()
  541. {
  542. is_capturing = true;
  543. mp_pipeline_invoke(pipeline, capture, NULL, 0);
  544. }
  545. static void
  546. on_output_changed()
  547. {
  548. output_buffer_width = mode.width / 2;
  549. output_buffer_height = mode.height / 2;
  550. if (camera->rotate != 0 || camera->rotate != 180) {
  551. int tmp = output_buffer_width;
  552. output_buffer_width = output_buffer_height;
  553. output_buffer_height = tmp;
  554. }
  555. for (size_t i = 0; i < NUM_BUFFERS; ++i) {
  556. glBindTexture(GL_TEXTURE_2D, output_buffers[i].texture_id);
  557. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, output_buffer_width, output_buffer_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  558. }
  559. glBindTexture(GL_TEXTURE_2D, 0);
  560. gles2_debayer_configure(
  561. gles2_debayer,
  562. output_buffer_width, output_buffer_height,
  563. mode.width, mode.height,
  564. camera->rotate, camera->mirrored,
  565. camera->previewmatrix[0] == 0 ? NULL : camera->previewmatrix,
  566. camera->blacklevel);
  567. }
  568. static int
  569. mod(int a, int b)
  570. {
  571. int r = a % b;
  572. return r < 0 ? r + b : r;
  573. }
  574. static void
  575. update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state)
  576. {
  577. const bool output_changed =
  578. !mp_camera_mode_is_equivalent(&mode, &state->mode)
  579. || preview_width != state->preview_width
  580. || preview_height != state->preview_height
  581. || device_rotation != state->device_rotation;
  582. camera = state->camera;
  583. mode = state->mode;
  584. preview_width = state->preview_width;
  585. preview_height = state->preview_height;
  586. device_rotation = state->device_rotation;
  587. burst_length = state->burst_length;
  588. save_dng = state->save_dng;
  589. // gain_is_manual = state->gain_is_manual;
  590. gain = state->gain;
  591. gain_max = state->gain_max;
  592. exposure_is_manual = state->exposure_is_manual;
  593. exposure = state->exposure;
  594. if (output_changed) {
  595. camera_rotation = mod(camera->rotate - device_rotation, 360);
  596. on_output_changed();
  597. }
  598. struct mp_main_state main_state = {
  599. .camera = camera,
  600. .mode = mode,
  601. .image_width = output_buffer_width,
  602. .image_height = output_buffer_height,
  603. .gain_is_manual = state->gain_is_manual,
  604. .gain = gain,
  605. .gain_max = gain_max,
  606. .exposure_is_manual = exposure_is_manual,
  607. .exposure = exposure,
  608. .has_auto_focus_continuous = state->has_auto_focus_continuous,
  609. .has_auto_focus_start = state->has_auto_focus_start,
  610. };
  611. mp_main_update_state(&main_state);
  612. }
  613. void
  614. mp_process_pipeline_update_state(const struct mp_process_pipeline_state *new_state)
  615. {
  616. mp_pipeline_invoke(pipeline, (MPPipelineCallback)update_state, new_state,
  617. sizeof(struct mp_process_pipeline_state));
  618. }
  619. // GTK4 seems to require this
  620. void pango_fc_font_get_languages() {}