process_pipeline.c 19 KB

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