forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
picture.c
1280 lines (1088 loc) · 38.8 KB
/
picture.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* picture.c
*
* Various funtions for saving/loading pictures.
* Copyright 2002 by Jeroen Vreeken ([email protected])
* Portions of this file are Copyright by Lionnel Maugis
* Portions of this file are Copyright 2010 by Wim Lewis ([email protected])
* This software is distributed under the GNU public license version 2
* See also the file 'COPYING'.
*
*/
#include "translate.h"
#include "picture.h"
#include "event.h"
#include <assert.h>
#ifdef HAVE_WEBP
#include <webp/encode.h>
#include <webp/mux.h>
#endif /* HAVE_WEBP */
#include <jpeglib.h>
#include <jerror.h>
/*
* The following declarations and 5 functions are jpeg related
* functions used by put_jpeg_grey_memory and put_jpeg_yuv420p_memory.
*/
typedef struct {
struct jpeg_destination_mgr pub;
JOCTET *buf;
size_t bufsize;
size_t jpegsize;
} mem_destination_mgr;
typedef mem_destination_mgr *mem_dest_ptr;
METHODDEF(void) init_destination(j_compress_ptr cinfo)
{
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
dest->pub.next_output_byte = dest->buf;
dest->pub.free_in_buffer = dest->bufsize;
dest->jpegsize = 0;
}
METHODDEF(boolean) empty_output_buffer(j_compress_ptr cinfo)
{
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
dest->pub.next_output_byte = dest->buf;
dest->pub.free_in_buffer = dest->bufsize;
return FALSE;
ERREXIT(cinfo, JERR_BUFFER_SIZE);
}
METHODDEF(void) term_destination(j_compress_ptr cinfo)
{
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
dest->jpegsize = dest->bufsize - dest->pub.free_in_buffer;
}
static GLOBAL(void) _jpeg_mem_dest(j_compress_ptr cinfo, JOCTET* buf, size_t bufsize)
{
mem_dest_ptr dest;
if (cinfo->dest == NULL) {
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_PERMANENT,
sizeof(mem_destination_mgr));
}
dest = (mem_dest_ptr) cinfo->dest;
dest->pub.init_destination = init_destination;
dest->pub.empty_output_buffer = empty_output_buffer;
dest->pub.term_destination = term_destination;
dest->buf = buf;
dest->bufsize = bufsize;
dest->jpegsize = 0;
}
static GLOBAL(int) _jpeg_mem_size(j_compress_ptr cinfo)
{
mem_dest_ptr dest = (mem_dest_ptr) cinfo->dest;
return dest->jpegsize;
}
/* EXIF image data is always in TIFF format, even if embedded in another
* file type. This consists of a constant header (TIFF file header,
* IFD header) followed by the tags in the IFD and then the data
* from any tags which do not fit inline in the IFD.
*
* The tags we write in the main IFD are:
* 0x010E Image description
* 0x8769 Exif sub-IFD
* 0x882A Time zone of time stamps
* and in the Exif sub-IFD:
* 0x9000 Exif version
* 0x9003 File date and time
* 0x9291 File date and time subsecond info
* But we omit any empty IFDs.
*/
#define TIFF_TAG_IMAGE_DESCRIPTION 0x010E
#define TIFF_TAG_DATETIME 0x0132
#define TIFF_TAG_EXIF_IFD 0x8769
#define TIFF_TAG_TZ_OFFSET 0x882A
#define EXIF_TAG_EXIF_VERSION 0x9000
#define EXIF_TAG_ORIGINAL_DATETIME 0x9003
#define EXIF_TAG_SUBJECT_AREA 0x9214
#define EXIF_TAG_TIFF_DATETIME_SS 0x9290
#define EXIF_TAG_ORIGINAL_DATETIME_SS 0x9291
#define TIFF_TYPE_ASCII 2 /* ASCII text */
#define TIFF_TYPE_USHORT 3 /* Unsigned 16-bit int */
#define TIFF_TYPE_LONG 4 /* Unsigned 32-bit int */
#define TIFF_TYPE_UNDEF 7 /* Byte blob */
#define TIFF_TYPE_SSHORT 8 /* Signed 16-bit int */
static const char exif_marker_start[14] = {
'E', 'x', 'i', 'f', 0, 0, /* EXIF marker signature */
'M', 'M', 0, 42, /* TIFF file header (big-endian) */
0, 0, 0, 8, /* Offset to first toplevel IFD */
};
static const char exif_version_tag[12] = {
0x90, 0x00, /* EXIF version tag, 0x9000 */
0x00, 0x07, /* Data type 7 = "unknown" (raw byte blob) */
0x00, 0x00, 0x00, 0x04, /* Data length */
0x30, 0x32, 0x32, 0x30 /* Inline data, EXIF version 2.2 */
};
static const char exif_subifd_tag[8] = {
0x87, 0x69, /* EXIF Sub-IFD tag */
0x00, 0x04, /* Data type 4 = uint32 */
0x00, 0x00, 0x00, 0x01, /* Number of values */
};
static const char exif_tzoffset_tag[12] = {
0x88, 0x2A, /* TIFF/EP time zone offset tag */
0x00, 0x08, /* Data type 8 = sint16 */
0x00, 0x00, 0x00, 0x01, /* Number of values */
0, 0, 0, 0 /* Dummy data */
};
static void put_uint16(JOCTET *buf, unsigned value)
{
buf[0] = ( value & 0xFF00 ) >> 8;
buf[1] = ( value & 0x00FF );
}
static void put_sint16(JOCTET *buf, int value)
{
buf[0] = ( value & 0xFF00 ) >> 8;
buf[1] = ( value & 0x00FF );
}
static void put_uint32(JOCTET *buf, unsigned value)
{
buf[0] = ( value & 0xFF000000 ) >> 24;
buf[1] = ( value & 0x00FF0000 ) >> 16;
buf[2] = ( value & 0x0000FF00 ) >> 8;
buf[3] = ( value & 0x000000FF );
}
struct tiff_writing {
JOCTET * const base;
JOCTET *buf;
unsigned data_offset;
};
static void put_direntry(struct tiff_writing *into, const char *data, unsigned length)
{
if (length <= 4) {
/* Entries that fit in the directory entry are stored there */
memset(into->buf, 0, 4);
memcpy(into->buf, data, length);
} else {
/* Longer entries are stored out-of-line */
unsigned offset = into->data_offset;
while ((offset & 0x03) != 0) { /* Alignment */
into->base[offset] = 0;
offset ++;
}
put_uint32(into->buf, offset);
memcpy(into->base + offset, data, length);
into->data_offset = offset + length;
}
}
static void put_stringentry(struct tiff_writing *into, unsigned tag, const char *str, int with_nul)
{
unsigned stringlength = strlen(str) + (with_nul?1:0);
put_uint16(into->buf, tag);
put_uint16(into->buf + 2, TIFF_TYPE_ASCII);
put_uint32(into->buf + 4, stringlength);
into->buf += 8;
put_direntry(into, str, stringlength);
into->buf += 4;
}
static void put_subjectarea(struct tiff_writing *into, const struct coord *box)
{
put_uint16(into->buf , EXIF_TAG_SUBJECT_AREA);
put_uint16(into->buf + 2, TIFF_TYPE_USHORT);
put_uint32(into->buf + 4, 4 /* Four USHORTs */);
put_uint32(into->buf + 8, into->data_offset);
into->buf += 12;
JOCTET *ool = into->base + into->data_offset;
put_uint16(ool , box->x); /* Center.x */
put_uint16(ool+2, box->y); /* Center.y */
put_uint16(ool+4, box->width);
put_uint16(ool+6, box->height);
into->data_offset += 8;
}
/*
* prepare_exif() is a comon function used to prepare
* exif data to be inserted into jpeg or webp files
*
*/
static unsigned prepare_exif(unsigned char **exif,
const struct context *cnt,
const struct timeval *tv_in1,
const struct coord *box)
{
/* description, datetime, and subtime are the values that are actually
* put into the EXIF data
*/
char *description, *datetime, *subtime;
char datetime_buf[22];
struct tm timestamp_tm;
struct timeval tv1;
gettimeofday(&tv1, NULL);
if (tv_in1 != NULL) {
tv1.tv_sec = tv_in1->tv_sec;
tv1.tv_usec = tv_in1->tv_usec;
}
localtime_r(&tv1.tv_sec, ×tamp_tm);
/* Exif requires this exact format */
snprintf(datetime_buf, 21, "%04d:%02d:%02d %02d:%02d:%02d",
timestamp_tm.tm_year + 1900,
timestamp_tm.tm_mon + 1,
timestamp_tm.tm_mday,
timestamp_tm.tm_hour,
timestamp_tm.tm_min,
timestamp_tm.tm_sec);
datetime = datetime_buf;
// TODO: Extract subsecond timestamp from somewhere, but only
// use as much of it as is indicated by conf->frame_limit
subtime = NULL;
if (cnt->conf.picture_exif) {
description = malloc(PATH_MAX);
mystrftime(cnt, description, PATH_MAX-1, cnt->conf.picture_exif, &tv1, NULL, 0);
} else {
description = NULL;
}
/* Calculate an upper bound on the size of the APP1 marker so
* we can allocate a buffer for it.
*/
/* Count up the number of tags and max amount of OOL data */
int ifd0_tagcount = 0;
int ifd1_tagcount = 0;
unsigned datasize = 0;
if (description) {
ifd0_tagcount ++;
datasize += 5 + strlen(description); /* Add 5 for NUL and alignment */
}
if (datetime) {
/* We write this to both the TIFF datetime tag (which most programs
* treat as "last-modified-date") and the EXIF "time of creation of
* original image" tag (which many programs ignore). This is
* redundant but seems to be the thing to do.
*/
ifd0_tagcount++;
ifd1_tagcount++;
/* We also write the timezone-offset tag in IFD0 */
ifd0_tagcount++;
/* It would be nice to use the same offset for both tags' values,
* but I don't want to write the bookkeeping for that right now */
datasize += 2 * (5 + strlen(datetime));
}
if (subtime) {
ifd1_tagcount++;
datasize += 5 + strlen(subtime);
}
if (box) {
ifd1_tagcount++;
datasize += 2 * 4; /* Four 16-bit ints */
}
if (ifd1_tagcount > 0) {
/* If we're writing the Exif sub-IFD, account for the
* two tags that requires */
ifd0_tagcount ++; /* The tag in IFD0 that points to IFD1 */
ifd1_tagcount ++; /* The EXIF version tag */
}
/* Each IFD takes 12 bytes per tag, plus six more (the tag count and the
* pointer to the next IFD, always zero in our case)
*/
int ifds_size =
( ifd1_tagcount > 0 ? ( 12 * ifd1_tagcount + 6 ) : 0 ) +
( ifd0_tagcount > 0 ? ( 12 * ifd0_tagcount + 6 ) : 0 );
if (ifds_size == 0) {
/* We're not actually going to write any information. */
return 0;
}
unsigned int buffer_size = 6 /* EXIF marker signature */ +
8 /* TIFF file header */ +
ifds_size /* the tag directories */ +
datasize;
JOCTET *marker = malloc(buffer_size);
memcpy(marker, exif_marker_start, 14); /* EXIF and TIFF headers */
struct tiff_writing writing = (struct tiff_writing) {
.base = marker + 6, /* base address for intra-TIFF offsets */
.buf = marker + 14, /* current write position */
.data_offset = 8 + ifds_size, /* where to start storing data */
};
/* Write IFD 0 */
/* Note that tags are stored in numerical order */
put_uint16(writing.buf, ifd0_tagcount);
writing.buf += 2;
if (description)
put_stringentry(&writing, TIFF_TAG_IMAGE_DESCRIPTION, description, 1);
if (datetime)
put_stringentry(&writing, TIFF_TAG_DATETIME, datetime, 1);
if (ifd1_tagcount > 0) {
/* Offset of IFD1 - TIFF header + IFD0 size. */
unsigned ifd1_offset = 8 + 6 + ( 12 * ifd0_tagcount );
memcpy(writing.buf, exif_subifd_tag, 8);
put_uint32(writing.buf + 8, ifd1_offset);
writing.buf += 12;
}
if (datetime) {
memcpy(writing.buf, exif_tzoffset_tag, 12);
put_sint16(writing.buf+8, timestamp_tm.tm_gmtoff / 3600);
writing.buf += 12;
}
put_uint32(writing.buf, 0); /* Next IFD offset = 0 (no next IFD) */
writing.buf += 4;
/* Write IFD 1 */
if (ifd1_tagcount > 0) {
/* (remember that the tags in any IFD must be in numerical order
* by tag) */
put_uint16(writing.buf, ifd1_tagcount);
memcpy(writing.buf + 2, exif_version_tag, 12); /* tag 0x9000 */
writing.buf += 14;
if (datetime)
put_stringentry(&writing, EXIF_TAG_ORIGINAL_DATETIME, datetime, 1);
if (box)
put_subjectarea(&writing, box);
if (subtime)
put_stringentry(&writing, EXIF_TAG_ORIGINAL_DATETIME_SS, subtime, 0);
put_uint32(writing.buf, 0); /* Next IFD = 0 (no next IFD) */
writing.buf += 4;
}
/* We should have met up with the OOL data */
assert( (writing.buf - writing.base) == 8 + ifds_size );
/* The buffer is complete; write it out */
unsigned marker_len = 6 + writing.data_offset;
/* assert we didn't underestimate the original buffer size */
assert(marker_len <= buffer_size);
free(description);
*exif = marker;
return marker_len;
}
/*
* put_jpeg_exif writes the EXIF APP1 chunk to the jpeg file.
* It must be called after jpeg_start_compress() but before
* any image data is written by jpeg_write_scanlines().
*/
static void put_jpeg_exif(j_compress_ptr cinfo,
const struct context *cnt,
const struct timeval *tv1,
const struct coord *box)
{
unsigned char *exif = NULL;
unsigned exif_len = prepare_exif(&exif, cnt, tv1, box);
if(exif_len > 0) {
/* EXIF data lives in a JPEG APP1 marker */
jpeg_write_marker(cinfo, JPEG_APP0 + 1, exif, exif_len);
free(exif);
}
}
#ifdef HAVE_WEBP
/*
* put_webp_exif writes the EXIF APP1 chunk to the webp file.
* It must be called after WebPEncode() and the result
* can then be written out to webp a file
*/
static void put_webp_exif(WebPMux* webp_mux,
const struct context *cnt,
const struct timeval *tv1,
const struct coord *box)
{
unsigned char *exif = NULL;
unsigned exif_len = prepare_exif(&exif, cnt, tv1, box);
if(exif_len > 0) {
WebPData webp_exif;
/* EXIF in WEBP does not need the EXIF marker signature (6 bytes) that are needed by jpeg */
webp_exif.bytes = exif + 6;
webp_exif.size = exif_len - 6;
WebPMuxError err = WebPMuxSetChunk(webp_mux, "EXIF", &webp_exif, 1);
if (err != WEBP_MUX_OK) {
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO
, _("Unable to set set EXIF to webp chunk"));
}
free(exif);
}
}
#endif /* HAVE_WEBP */
/**
* put_jpeg_yuv420p_memory
* Converts an input image in the YUV420P format into a jpeg image and puts
* it in a memory buffer.
* Inputs:
* - image_size is the size of the input image buffer.
* - input_image is the image in YUV420P format.
* - width and height are the dimensions of the image
* - quality is the jpeg encoding quality 0-100%
*
* Output:
* - dest_image is a pointer to the jpeg image buffer
*
* Returns buffer size of jpeg image
*/
static int put_jpeg_yuv420p_memory(unsigned char *dest_image, int image_size,
unsigned char *input_image, int width, int height, int quality,
struct context *cnt, struct timeval *tv1, struct coord *box)
{
int i, j, jpeg_image_size;
JSAMPROW y[16],cb[16],cr[16]; // y[2][5] = color sample of row 2 and pixel column 5; (one plane)
JSAMPARRAY data[3]; // t[0][2][5] = color sample 0 of row 2 and column 5
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
data[0] = y;
data[1] = cb;
data[2] = cr;
cinfo.err = jpeg_std_error(&jerr); // Errors get written to stderr
jpeg_create_compress(&cinfo);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
jpeg_set_defaults(&cinfo);
jpeg_set_colorspace(&cinfo, JCS_YCbCr);
cinfo.raw_data_in = TRUE; // Supply downsampled data
#if JPEG_LIB_VERSION >= 70
cinfo.do_fancy_downsampling = FALSE; // Fix segfault with v7
#endif
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
cinfo.comp_info[1].h_samp_factor = 1;
cinfo.comp_info[1].v_samp_factor = 1;
cinfo.comp_info[2].h_samp_factor = 1;
cinfo.comp_info[2].v_samp_factor = 1;
jpeg_set_quality(&cinfo, quality, TRUE);
cinfo.dct_method = JDCT_FASTEST;
_jpeg_mem_dest(&cinfo, dest_image, image_size); // Data written to mem
jpeg_start_compress(&cinfo, TRUE);
put_jpeg_exif(&cinfo, cnt, tv1, box);
/* If the image is not a multiple of 16, this overruns the buffers
* we'll just pad those last bytes with zeros
*/
for (j = 0; j < height; j += 16) {
for (i = 0; i < 16; i++) {
if ((width * (i + j)) < (width * height)) {
y[i] = input_image + width * (i + j);
if (i % 2 == 0) {
cb[i / 2] = input_image + width * height + width / 2 * ((i + j) /2);
cr[i / 2] = input_image + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
}
} else {
y[i] = 0x00;
cb[i] = 0x00;
cr[i] = 0x00;
}
}
jpeg_write_raw_data(&cinfo, data, 16);
}
jpeg_finish_compress(&cinfo);
jpeg_image_size = _jpeg_mem_size(&cinfo);
jpeg_destroy_compress(&cinfo);
return jpeg_image_size;
}
/**
* put_jpeg_grey_memory
* Converts an input image in the grayscale format into a jpeg image.
*
* Inputs:
* - image_size is the size of the input image buffer.
* - input_image is the image in grayscale format.
* - width and height are the dimensions of the image
* - quality is the jpeg encoding quality 0-100%
*
* Output:
* - dest_image is a pointer to the jpeg image buffer
*
* Returns buffer size of jpeg image.
*/
static int put_jpeg_grey_memory(unsigned char *dest_image, int image_size,
unsigned char *input_image, int width, int height, int quality,
struct context *cnt, struct timeval *tv1, struct coord *box)
{
int y, dest_image_size;
JSAMPROW row_ptr[1];
struct jpeg_compress_struct cjpeg;
struct jpeg_error_mgr jerr;
cjpeg.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cjpeg);
cjpeg.image_width = width;
cjpeg.image_height = height;
cjpeg.input_components = 1; /* One colour component */
cjpeg.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&cjpeg);
jpeg_set_quality(&cjpeg, quality, TRUE);
cjpeg.dct_method = JDCT_FASTEST;
_jpeg_mem_dest(&cjpeg, dest_image, image_size); // Data written to mem
jpeg_start_compress (&cjpeg, TRUE);
put_jpeg_exif(&cjpeg, cnt, tv1, box);
row_ptr[0] = input_image;
for (y = 0; y < height; y++) {
jpeg_write_scanlines(&cjpeg, row_ptr, 1);
row_ptr[0] += width;
}
jpeg_finish_compress(&cjpeg);
dest_image_size = _jpeg_mem_size(&cjpeg);
jpeg_destroy_compress(&cjpeg);
return dest_image_size;
}
#ifdef HAVE_WEBP
/**
* put_webp_yuv420p_file
* Converts an YUV420P coded image to a webp image and writes
* it to an already open file.
*
* Inputs:
* - image is the image in YUV420P format.
* - width and height are the dimensions of the image
* - quality is the webp encoding quality 0-100%
*
* Output:
* - The webp is written directly to the file given by the file pointer fp
*
* Returns nothing
*/
static void put_webp_yuv420p_file(FILE *fp,
unsigned char *image, int width, int height,
int quality, struct context *cnt, struct timeval *tv1, struct coord *box)
{
/* Create a config present and check for compatible library version */
WebPConfig webp_config;
if (!WebPConfigPreset(&webp_config, WEBP_PRESET_DEFAULT, (float) quality)){
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO, _("libwebp version error"));
return;
}
/* Create the input data structure and check for compatible library version */
WebPPicture webp_image;
if (!WebPPictureInit(&webp_image)){
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("libwebp version error"));
return;
}
/* Allocate the image buffer based on image width and height */
webp_image.width = width;
webp_image.height = height;
if (!WebPPictureAlloc(&webp_image)){
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("libwebp image buffer allocation error"));
return;
}
/* Map the input YUV420P buffer as individual Y, U and V pointers */
webp_image.y = image;
webp_image.u = image + width * height;
webp_image.v = webp_image.u + (width * height) / 4;
/* Setup the memory writting method */
WebPMemoryWriter webp_writer;
WebPMemoryWriterInit(&webp_writer);
webp_image.writer = WebPMemoryWrite;
webp_image.custom_ptr = (void*) &webp_writer;
/* Encode the YUV image as webp */
if (!WebPEncode(&webp_config, &webp_image))
MOTION_LOG(WRN, TYPE_CORE, NO_ERRNO,_("libwebp image compression error"));
/* A bitstream object is needed for the muxing proces */
WebPData webp_bitstream;
webp_bitstream.bytes = webp_writer.mem;
webp_bitstream.size = webp_writer.size;
/* Create a mux from the prepared image data */
WebPMux* webp_mux = WebPMuxCreate(&webp_bitstream, 1);
put_webp_exif(webp_mux, cnt, tv1, box);
/* Add Exif data to the webp image data */
WebPData webp_output;
WebPMuxError err = WebPMuxAssemble(webp_mux, &webp_output);
if (err != WEBP_MUX_OK) {
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("unable to assemble webp image"));
}
/* Write the webp final bitstream to the file */
if (fwrite(webp_output.bytes, sizeof(uint8_t), webp_output.size, fp) != webp_output.size)
MOTION_LOG(ERR, TYPE_CORE, NO_ERRNO,_("unable to save webp image to file"));
#if WEBP_ENCODER_ABI_VERSION > 0x0202
/* writer.mem must be freed by calling WebPMemoryWriterClear */
WebPMemoryWriterClear(&webp_writer);
#else
/* writer.mem must be freed by calling 'free(writer.mem)' */
free(webp_writer.mem);
#endif /* WEBP_ENCODER_ABI_VERSION */
/* free the memory used by webp for image data */
WebPPictureFree(&webp_image);
/* free the memory used by webp mux object */
WebPMuxDelete(webp_mux);
/* free the memory used by webp for output data */
WebPDataClear(&webp_output);
}
#endif /* HAVE_WEBP */
/**
* put_jpeg_yuv420p_file
* Converts an YUV420P coded image to a jpeg image and writes
* it to an already open file.
*
* Inputs:
* - image is the image in YUV420P format.
* - width and height are the dimensions of the image
* - quality is the jpeg encoding quality 0-100%
*
* Output:
* - The jpeg is written directly to the file given by the file pointer fp
*
* Returns nothing
*/
static void put_jpeg_yuv420p_file(FILE *fp,
unsigned char *image, int width, int height,
int quality,
struct context *cnt, struct timeval *tv1, struct coord *box)
{
int i, j;
JSAMPROW y[16],cb[16],cr[16]; // y[2][5] = color sample of row 2 and pixel column 5; (one plane)
JSAMPARRAY data[3]; // t[0][2][5] = color sample 0 of row 2 and column 5
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
data[0] = y;
data[1] = cb;
data[2] = cr;
cinfo.err = jpeg_std_error(&jerr); // Errors get written to stderr
jpeg_create_compress(&cinfo);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
jpeg_set_defaults(&cinfo);
jpeg_set_colorspace(&cinfo, JCS_YCbCr);
cinfo.raw_data_in = TRUE; // Supply downsampled data
#if JPEG_LIB_VERSION >= 70
cinfo.do_fancy_downsampling = FALSE; // Fix segfault with v7
#endif
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
cinfo.comp_info[1].h_samp_factor = 1;
cinfo.comp_info[1].v_samp_factor = 1;
cinfo.comp_info[2].h_samp_factor = 1;
cinfo.comp_info[2].v_samp_factor = 1;
jpeg_set_quality(&cinfo, quality, TRUE);
cinfo.dct_method = JDCT_FASTEST;
jpeg_stdio_dest(&cinfo, fp); // Data written to file
jpeg_start_compress(&cinfo, TRUE);
put_jpeg_exif(&cinfo, cnt, tv1, box);
for (j = 0; j < height; j += 16) {
for (i = 0; i < 16; i++) {
if ((width * (i + j)) < (width * height)) {
y[i] = image + width * (i + j);
if (i % 2 == 0) {
cb[i / 2] = image + width * height + width / 2 * ((i + j) / 2);
cr[i / 2] = image + width * height + width * height / 4 + width / 2 * ((i + j) / 2);
}
} else {
y[i] = 0x00;
cb[i] = 0x00;
cr[i] = 0x00;
}
}
jpeg_write_raw_data(&cinfo, data, 16);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
}
/**
* put_jpeg_grey_file
* Converts an greyscale image to a jpeg image and writes
* it to an already open file.
*
* Inputs:
* - image is the image in greyscale format.
* - width and height are the dimensions of the image
* - quality is the jpeg encoding quality 0-100%
* Output:
* - The jpeg is written directly to the file given by the file pointer fp
*
* Returns nothing
*/
static void put_jpeg_grey_file(FILE *picture, unsigned char *image, int width, int height, int quality)
{
int y;
JSAMPROW row_ptr[1];
struct jpeg_compress_struct cjpeg;
struct jpeg_error_mgr jerr;
cjpeg.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cjpeg);
cjpeg.image_width = width;
cjpeg.image_height = height;
cjpeg.input_components = 1; /* One colour component */
cjpeg.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&cjpeg);
jpeg_set_quality(&cjpeg, quality, TRUE);
cjpeg.dct_method = JDCT_FASTEST;
jpeg_stdio_dest(&cjpeg, picture);
jpeg_start_compress(&cjpeg, TRUE);
put_jpeg_exif(&cjpeg, NULL, NULL, NULL);
row_ptr[0] = image;
for (y = 0; y < height; y++) {
jpeg_write_scanlines(&cjpeg, row_ptr, 1);
row_ptr[0] += width;
}
jpeg_finish_compress(&cjpeg);
jpeg_destroy_compress(&cjpeg);
}
/**
* put_ppm_bgr24_file
* Converts an greyscale image to a PPM image and writes
* it to an already open file.
* Inputs:
* - image is the image in YUV420P format.
* - width and height are the dimensions of the image
*
* Output:
* - The PPM is written directly to the file given by the file pointer fp
*
* Returns nothing
*/
static void put_ppm_bgr24_file(FILE *picture, unsigned char *image, int width, int height)
{
int x, y;
unsigned char *l = image;
unsigned char *u = image + width * height;
unsigned char *v = u + (width * height) / 4;
int r, g, b;
unsigned char rgb[3];
/*
* ppm header
* width height
* maxval
*/
fprintf(picture, "P6\n");
fprintf(picture, "%d %d\n", width, height);
fprintf(picture, "%d\n", 255);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
r = 76283 * (((int)*l) - 16)+104595*(((int)*u) - 128);
g = 76283 * (((int)*l) - 16)- 53281*(((int)*u) - 128) - 25625 * (((int)*v) - 128);
b = 76283 * (((int)*l) - 16) + 132252 * (((int)*v) - 128);
r = r >> 16;
g = g >> 16;
b = b >> 16;
if (r < 0)
r = 0;
else if (r > 255)
r = 255;
if (g < 0)
g = 0;
else if (g > 255)
g = 255;
if (b < 0)
b = 0;
else if (b > 255)
b = 255;
rgb[0] = b;
rgb[1] = g;
rgb[2] = r;
l++;
if (x%2 != 0) {
u++;
v++;
}
/* ppm is rgb not bgr */
fwrite(rgb, 1, 3, picture);
}
if (y%2 == 0) {
u -= width / 2;
v -= width / 2;
}
}
}
/**
* overlay_smartmask
* Copies smartmask as an overlay into motion images and movies.
*
* Returns nothing.
*/
void overlay_smartmask(struct context *cnt, unsigned char *out)
{
int i, x, v, width, height, line;
struct images *imgs = &cnt->imgs;
unsigned char *smartmask = imgs->smartmask_final;
unsigned char *out_y, *out_u, *out_v;
i = imgs->motionsize;
v = i + ((imgs->motionsize) / 4);
width = imgs->width;
height = imgs->height;
/* Set V to 255 to make smartmask appear red. */
out_v = out + v;
out_u = out + i;
for (i = 0; i < height; i += 2) {
line = i * width;
for (x = 0; x < width; x += 2) {
if (smartmask[line + x] == 0 || smartmask[line + x + 1] == 0 ||
smartmask[line + width + x] == 0 ||
smartmask[line + width + x + 1] == 0) {
*out_v = 255;
*out_u = 128;
}
out_v++;
out_u++;
}
}
out_y = out;
/* Set colour intensity for smartmask. */
for (i = 0; i < imgs->motionsize; i++) {
if (smartmask[i] == 0)
*out_y = 0;
out_y++;
}
}
/**
* overlay_fixed_mask
* Copies fixed mask as green overlay into motion images and movies.
*
* Returns nothing.
*/
void overlay_fixed_mask(struct context *cnt, unsigned char *out)
{
int i, x, v, width, height, line;
struct images *imgs = &cnt->imgs;
unsigned char *mask = imgs->mask;
unsigned char *out_y, *out_u, *out_v;
i = imgs->motionsize;
v = i + ((imgs->motionsize) / 4);
width = imgs->width;
height = imgs->height;
/* Set U and V to 0 to make fixed mask appear green. */
out_v = out + v;
out_u = out + i;
for (i = 0; i < height; i += 2) {
line = i * width;
for (x = 0; x < width; x += 2) {
if (mask[line + x] == 0 || mask[line + x + 1] == 0 ||
mask[line + width + x] == 0 ||
mask[line + width + x + 1] == 0) {
*out_v = 0;
*out_u = 0;
}
out_v++;
out_u++;
}
}
out_y = out;
/* Set colour intensity for mask. */
for (i = 0; i < imgs->motionsize; i++) {
if (mask[i] == 0)
*out_y = 0;
out_y++;
}
}
/**
* overlay_largest_label
* Copies largest label as an overlay into motion images and movies.
*
* Returns nothing.
*/
void overlay_largest_label(struct context *cnt, unsigned char *out)
{
int i, x, v, width, height, line;
struct images *imgs = &cnt->imgs;
int *labels = imgs->labels;
unsigned char *out_y, *out_u, *out_v;
i = imgs->motionsize;
v = i + ((imgs->motionsize) / 4);