process_pipeline.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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, (mode.width >> 4) * 3);
  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, mp_pixel_format_bits_per_pixel(mode.pixel_format));
  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. const float blacklevel = camera->blacklevel;
  353. TIFFSetField(tif, TIFFTAG_BLACKLEVEL, 1, &blacklevel);
  354. }
  355. TIFFCheckpointDirectory(tif);
  356. printf("Writing frame to %s\n", fname);
  357. unsigned char *pLine = (unsigned char *)malloc(mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width));
  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. free(pLine);
  362. TIFFWriteDirectory(tif);
  363. // Add an EXIF block to the tiff
  364. TIFFCreateEXIFDirectory(tif);
  365. // 1 = manual, 2 = full auto, 3 = aperture priority, 4 = shutter priority
  366. if (!exposure_is_manual) {
  367. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 2);
  368. } else {
  369. TIFFSetField(tif, EXIFTAG_EXPOSUREPROGRAM, 1);
  370. }
  371. TIFFSetField(tif, EXIFTAG_EXPOSURETIME,
  372. (mode.frame_interval.numerator /
  373. (float)mode.frame_interval.denominator) /
  374. ((float)mode.height / (float)exposure));
  375. uint16_t isospeed[1];
  376. isospeed[0] = (uint16_t)remap(gain - 1, 0, gain_max, camera->iso_min,
  377. camera->iso_max);
  378. TIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, isospeed);
  379. TIFFSetField(tif, EXIFTAG_FLASH, 0);
  380. TIFFSetField(tif, EXIFTAG_DATETIMEORIGINAL, datetime);
  381. TIFFSetField(tif, EXIFTAG_DATETIMEDIGITIZED, datetime);
  382. if (camera->fnumber) {
  383. TIFFSetField(tif, EXIFTAG_FNUMBER, camera->fnumber);
  384. }
  385. if (camera->focallength) {
  386. TIFFSetField(tif, EXIFTAG_FOCALLENGTH, camera->focallength);
  387. }
  388. if (camera->focallength && camera->cropfactor) {
  389. TIFFSetField(tif, EXIFTAG_FOCALLENGTHIN35MMFILM,
  390. (short)(camera->focallength * camera->cropfactor));
  391. }
  392. uint64_t exif_offset = 0;
  393. TIFFWriteCustomDirectory(tif, &exif_offset);
  394. TIFFFreeDirectory(tif);
  395. // Update exif pointer
  396. TIFFSetDirectory(tif, 0);
  397. TIFFSetField(tif, TIFFTAG_EXIFIFD, exif_offset);
  398. TIFFRewriteDirectory(tif);
  399. TIFFClose(tif);
  400. }
  401. static void
  402. post_process_finished(GSubprocess *proc, GAsyncResult *res, GdkTexture *thumb)
  403. {
  404. char *stdout;
  405. g_subprocess_communicate_utf8_finish(proc, res, &stdout, NULL, NULL);
  406. // The last line contains the file name
  407. int end = strlen(stdout);
  408. // Skip the newline at the end
  409. stdout[--end] = '\0';
  410. char *path = path = stdout + end - 1;
  411. do {
  412. if (*path == '\n') {
  413. path++;
  414. break;
  415. }
  416. --path;
  417. } while (path > stdout);
  418. mp_main_capture_completed(thumb, path);
  419. }
  420. static void
  421. process_capture_burst(GdkTexture *thumb)
  422. {
  423. time_t rawtime;
  424. time(&rawtime);
  425. struct tm tim = *(localtime(&rawtime));
  426. char timestamp[30];
  427. strftime(timestamp, 30, "%Y%m%d%H%M%S", &tim);
  428. if (g_get_user_special_dir(G_USER_DIRECTORY_PICTURES) != NULL) {
  429. sprintf(capture_fname,
  430. "%s/IMG%s",
  431. g_get_user_special_dir(G_USER_DIRECTORY_PICTURES),
  432. timestamp);
  433. } else if (getenv("XDG_PICTURES_DIR") != NULL) {
  434. sprintf(capture_fname,
  435. "%s/IMG%s",
  436. getenv("XDG_PICTURES_DIR"),
  437. timestamp);
  438. } else {
  439. sprintf(capture_fname,
  440. "%s/Pictures/IMG%s",
  441. getenv("HOME"),
  442. timestamp);
  443. }
  444. // Start post-processing the captured burst
  445. g_print("Post process %s to %s.ext\n", burst_dir, capture_fname);
  446. GError *error = NULL;
  447. GSubprocess *proc = g_subprocess_new(
  448. G_SUBPROCESS_FLAGS_STDOUT_PIPE,
  449. &error,
  450. processing_script,
  451. burst_dir,
  452. capture_fname,
  453. NULL);
  454. if (!proc) {
  455. g_printerr("Failed to spawn postprocess process: %s\n",
  456. error->message);
  457. return;
  458. }
  459. g_subprocess_communicate_utf8_async(
  460. proc,
  461. NULL,
  462. NULL,
  463. (GAsyncReadyCallback)post_process_finished,
  464. thumb);
  465. }
  466. static void
  467. process_image(MPPipeline *pipeline, const MPBuffer *buffer)
  468. {
  469. #ifdef PROFILE_PROCESS
  470. clock_t t1 = clock();
  471. #endif
  472. size_t size =
  473. mp_pixel_format_width_to_bytes(mode.pixel_format, mode.width) *
  474. mode.height;
  475. uint8_t *image = malloc(size);
  476. memcpy(image, buffer->data, size);
  477. mp_io_pipeline_release_buffer(buffer->index);
  478. MPZBarImage *zbar_image = mp_zbar_image_new(image, mode.pixel_format, mode.width, mode.height, camera_rotation, camera->mirrored);
  479. mp_zbar_pipeline_process_image(mp_zbar_image_ref(zbar_image));
  480. #ifdef PROFILE_PROCESS
  481. clock_t t2 = clock();
  482. #endif
  483. GdkTexture *thumb = process_image_for_preview(image);
  484. if (captures_remaining > 0) {
  485. int count = burst_length - captures_remaining;
  486. --captures_remaining;
  487. process_image_for_capture(image, count);
  488. if (captures_remaining == 0) {
  489. assert(thumb);
  490. process_capture_burst(thumb);
  491. } else {
  492. assert(!thumb);
  493. }
  494. } else {
  495. assert(!thumb);
  496. }
  497. mp_zbar_image_unref(zbar_image);
  498. ++frames_processed;
  499. if (captures_remaining == 0) {
  500. is_capturing = false;
  501. }
  502. #ifdef PROFILE_PROCESS
  503. clock_t t3 = clock();
  504. printf("process_image %fms, step 1:%fms, step 2:%fms\n",
  505. (float)(t3 - t1) / CLOCKS_PER_SEC * 1000,
  506. (float)(t2 - t1) / CLOCKS_PER_SEC * 1000,
  507. (float)(t3 - t2) / CLOCKS_PER_SEC * 1000);
  508. #endif
  509. }
  510. void
  511. mp_process_pipeline_process_image(MPBuffer buffer)
  512. {
  513. // If we haven't processed the previous frame yet, drop this one
  514. if (frames_received != frames_processed && !is_capturing) {
  515. mp_io_pipeline_release_buffer(buffer.index);
  516. return;
  517. }
  518. ++frames_received;
  519. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_image, &buffer,
  520. sizeof(MPBuffer));
  521. }
  522. static void
  523. capture()
  524. {
  525. char template[] = "/tmp/megapixels.XXXXXX";
  526. char *tempdir;
  527. tempdir = mkdtemp(template);
  528. if (tempdir == NULL) {
  529. g_printerr("Could not make capture directory %s\n", template);
  530. exit(EXIT_FAILURE);
  531. }
  532. strcpy(burst_dir, tempdir);
  533. captures_remaining = burst_length;
  534. }
  535. void
  536. mp_process_pipeline_capture()
  537. {
  538. is_capturing = true;
  539. mp_pipeline_invoke(pipeline, capture, NULL, 0);
  540. }
  541. static void
  542. on_output_changed()
  543. {
  544. output_buffer_width = mode.width / 2;
  545. output_buffer_height = mode.height / 2;
  546. if (camera->rotate != 0 || camera->rotate != 180) {
  547. int tmp = output_buffer_width;
  548. output_buffer_width = output_buffer_height;
  549. output_buffer_height = tmp;
  550. }
  551. for (size_t i = 0; i < NUM_BUFFERS; ++i) {
  552. glBindTexture(GL_TEXTURE_2D, output_buffers[i].texture_id);
  553. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, output_buffer_width, output_buffer_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  554. }
  555. glBindTexture(GL_TEXTURE_2D, 0);
  556. gles2_debayer_configure(
  557. gles2_debayer,
  558. output_buffer_width, output_buffer_height,
  559. mode.width, mode.height,
  560. camera->rotate, camera->mirrored,
  561. camera->previewmatrix[0] == 0 ? NULL : camera->previewmatrix,
  562. camera->blacklevel);
  563. }
  564. static int
  565. mod(int a, int b)
  566. {
  567. int r = a % b;
  568. return r < 0 ? r + b : r;
  569. }
  570. static void
  571. update_state(MPPipeline *pipeline, const struct mp_process_pipeline_state *state)
  572. {
  573. const bool output_changed =
  574. !mp_camera_mode_is_equivalent(&mode, &state->mode)
  575. || preview_width != state->preview_width
  576. || preview_height != state->preview_height
  577. || device_rotation != state->device_rotation;
  578. camera = state->camera;
  579. mode = state->mode;
  580. preview_width = state->preview_width;
  581. preview_height = state->preview_height;
  582. device_rotation = state->device_rotation;
  583. burst_length = state->burst_length;
  584. // gain_is_manual = state->gain_is_manual;
  585. gain = state->gain;
  586. gain_max = state->gain_max;
  587. exposure_is_manual = state->exposure_is_manual;
  588. exposure = state->exposure;
  589. if (output_changed) {
  590. camera_rotation = mod(camera->rotate - device_rotation, 360);
  591. on_output_changed();
  592. }
  593. struct mp_main_state main_state = {
  594. .camera = camera,
  595. .mode = mode,
  596. .image_width = output_buffer_width,
  597. .image_height = output_buffer_height,
  598. .gain_is_manual = state->gain_is_manual,
  599. .gain = gain,
  600. .gain_max = gain_max,
  601. .exposure_is_manual = exposure_is_manual,
  602. .exposure = exposure,
  603. .has_auto_focus_continuous = state->has_auto_focus_continuous,
  604. .has_auto_focus_start = state->has_auto_focus_start,
  605. };
  606. mp_main_update_state(&main_state);
  607. }
  608. void
  609. mp_process_pipeline_update_state(const struct mp_process_pipeline_state *new_state)
  610. {
  611. mp_pipeline_invoke(pipeline, (MPPipelineCallback)update_state, new_state,
  612. sizeof(struct mp_process_pipeline_state));
  613. }
  614. // GTK4 seems to require this
  615. void pango_fc_font_get_languages() {}