-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmain.c
More file actions
1007 lines (921 loc) · 37.5 KB
/
main.c
File metadata and controls
1007 lines (921 loc) · 37.5 KB
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
/**
* @file
* Copyright (c) 2011-2025, CESNET
* Copyright (c) 2011, Silicon Genome, LLC.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_MSC_VER)
#include "utils/getopt.h"
#else
#include <getopt.h>
#endif
// WIN32 wchar support
#if defined(_UNICODE)
#include <windows.h> // WideCharToMultiByte
#include <tchar.h>
#define option _toption
#define optarg _toptarg
#define getopt_long _tgetopt_long
#define atoi _tstoi
#else
#define _T(...) __VA_ARGS__
#define TCHAR char
#define _tcscmp strcmp
#define _tcsstr strstr
#endif
#include "../libgpujpeg/gpujpeg.h"
#include "../libgpujpeg/gpujpeg_common.h"
#define RAW_EXTS "*.rgb, *.yuv, *.r, *.bmp, *.pnm, *.y4m..."
#define USE_IF_NOT_NULL_ELSE(cond, alt_val) (cond) ? (cond) : (alt_val)
static char*
tstr_to_mbs_helper(const TCHAR* tstr, char* mbs_buf, size_t mbs_len)
{
#if _UNICODE
const int size_needed = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, tstr, -1, NULL, 0, NULL, NULL);
if ( size_needed == 0 ) {
fprintf(stderr, "WideCharToMultiByte error: %d (0x%x)!\n", GetLastError(), GetLastError());
return NULL;
}
if (size_needed > (int) mbs_len) {
fprintf(stderr, "buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, mbs_len);
return NULL;
}
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, tstr, -1, mbs_buf, size_needed, NULL, NULL);
return mbs_buf;
#else
(void) mbs_buf, (void) mbs_len;
return (char *) tstr;
#endif
}
#define tstr_to_mbs(tstr) tstr_to_mbs_helper(tstr, (char[1024]){0}, 1024)
#ifdef _UNICODE
static wchar_t*
mbs_to_wstr_helper(const char* mbstr, wchar_t* wstr_buf, size_t wstr_len)
{
const int size_needed = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, NULL, 0);
if (size_needed == 0) {
fprintf(stderr, "MultiByteToWideChar error: %d (0x%x)!\n", GetLastError(), GetLastError());
return NULL;
}
if (size_needed > (int) wstr_len) {
fprintf(stderr, "buffer provided to %s too short - needed %d, got %zu!\n", __func__, size_needed, wstr_len);
return NULL;
}
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, mbstr, -1, wstr_buf, size_needed);
return wstr_buf;
}
#define mbs_to_wstr(tstr) mbs_to_wstr_helper(tstr, (wchar_t[1024]){0}, 1024)
#endif
static void
print_help(bool full)
{
printf("gpujpegtool [options] input.rgb output.jpg [input2.rgb output2.jpg ...]\n"
" -h, --help print help\n");
if (!full) {
printf(" -H, --fullhelp print all options\n");
}
printf(" -v, --verbose verbose output (multiply to increase verbosity - max 3) \n"
" -D, --device set cuda device id (default 0)\n"
" -L, --device-list list cuda devices\n"
"\n");
printf(" -s, --size set input image size in pixels, e.g. 1920x1080\n"
" -f, --pixel-format set input/output image pixel format, one of the\n"
" following (example in parenthesis):\n");
gpujpeg_print_pixel_formats();
printf("\n"
" -c, --colorspace set input/output image colorspace, e.g. rgb, ycbcr-jpeg (full\n"
" range BT.601), ycbcr-bt601 (limited 601), ycbcr-bt709 (limited)\n"
"\n");
printf(" -q, --quality set JPEG encoder quality level 0-100 (default 75)\n"
" -r, --restart set JPEG encoder restart interval (default 8)\n"
" -S, --subsampled[=<s>] set JPEG encoder chroma subsampling in J:a:b[:A] format (default 4:2:0)\n"
" -i --interleaved set JPEG encoder to use interleaved stream\n"
" -g --segment-info set JPEG encoder to use segment info in stream\n"
" for fast decoding\n"
"\n");
printf(" -e, --encode perform JPEG encoding\n"
" -d, --decode perform JPEG decoding\n"
" -C, --convert convert input image to output image (change\n"
" color space and/or sampling factor)\n"
" -R, --component-range show samples range for each component in image\n"
"\n");
printf(" -n --iterate perform encoding/decoding in specified number of\n"
" iterations for each image\n"
" -o --use-opengl use an OpenGL texture as input/output\n"
" -I --info print JPEG file info\n"
" -a --alpha encode/decode alpha channel (otherwise stripped)\n"
" -N --native create native JPEG (Adobe RGB for RGB, SPIFF for Y709;\n"
" may be incompatible with some decoders;\n"
" works also for decoding)\n"
" -V --version print GPUJPEG version\n"
);
if ( full ) {
printf(" -b, --debug debug helpers (reset GPU for leakcheck, dump infile if not regular)\n"
" -O <key>=<value>|help set encoder/decoder option, 'help' for list\n");
}
printf("recognized raw input/output file extensions: rgb, yuv, pnm... (use`gpujpegtool exts` for the full list)\n");
}
static void
print_gpujpeg_image_parameters(struct gpujpeg_image_parameters params_image, bool oneline,
const char* subsampling_details)
{
const char *sep_str = oneline ? " " : "\n";
const char *separator = ""; // first time empty
if ( params_image.width ) {
printf("%s%d", oneline ? "" : "width: ", params_image.width);
}
if ( params_image.height ) {
printf("%s%d%s", oneline ? "x" : "\nheight: ", params_image.height, (separator = sep_str));
}
if ( params_image.pixel_format != GPUJPEG_PIXFMT_NONE ) {
printf("%s%s",
oneline ? "" : "internal representation: ", gpujpeg_pixel_format_get_name(params_image.pixel_format));
if ( !oneline && subsampling_details != NULL ) {
printf(" (%s)", subsampling_details);
}
printf("%s", (separator = sep_str));
}
if ( params_image.color_space ) {
printf("%s%s", oneline ? "" : "color space: ", gpujpeg_color_space_get_name(params_image.color_space));
}
printf("\n");
}
static int print_image_info_jpeg(const char *filename, int verbose) {
#ifdef _UNICODE
FILE* f = _wfopen(mbs_to_wstr(filename), L"rb");
#else
FILE *f = fopen(filename, "rb");
#endif
if (!f) {
perror("Cannot open");
return 1;
}
fseek(f, 0L, SEEK_END);
long int len = ftell(f);
printf("size: %lu B\n", len);
fseek(f, 0L, SEEK_SET);
uint8_t *jpeg = malloc(len);
size_t ret = fread(jpeg, len, 1, f);
fclose(f);
if (ret == 0) {
fprintf(stderr, "Cannot read image contents.\n");
return 1;
}
struct gpujpeg_image_info info = { 0 };
if (gpujpeg_decoder_get_image_info2(jpeg, len, &info, verbose, GPUJPEG_COUNT_SEG_COUNT_REQ) == 0) {
print_gpujpeg_image_parameters(info.param_image, false,
gpujpeg_subsampling_get_name(info.param.comp_count, info.param.sampling_factor));
printf("interleaved: %s\n", info.param.interleaved ? "yes" : "no");
printf("header type: %s\n", gpujpeg_header_type_get_name(info.header_type));
if ( info.metadata.vals[GPUJPEG_METADATA_ORIENTATION].set ) {
printf("orientation: %s\n",
gpujpeg_orientation_get_name(info.metadata.vals[GPUJPEG_METADATA_ORIENTATION].orient));
}
if ( info.segment_count ) {
printf("segment count: %d (DRI = %d)\n", info.segment_count, info.param.restart_interval);
}
if ( info.comment != NULL) {
printf("comment: %s\n", info.comment);
}
}
free(jpeg);
return 0;
}
static int print_image_info(const char *filename, int verbose) {
if (!filename) {
fprintf(stderr, "Missing filename!\n");
return 1;
}
printf("name: %s\n", filename);
enum gpujpeg_image_file_format format = gpujpeg_image_get_file_format(filename);
if (format == GPUJPEG_IMAGE_FILE_JPEG ) {
return print_image_info_jpeg(filename, verbose);
}
struct gpujpeg_image_parameters param_image = { 0 };
if ( gpujpeg_image_get_properties(filename, ¶m_image, 1) < 0 ) {
fprintf(stderr, "Error getting raw image %s info!\n", filename);
return 1;
}
print_gpujpeg_image_parameters(param_image, false, NULL);
return 0;
}
struct options
{
gpujpeg_sampling_factor_t subsampling;
bool native_file_format;
bool keep_alpha;
};
static bool
adjust_params(struct gpujpeg_parameters* param, struct gpujpeg_image_parameters* param_image, const char* in,
const char* out, bool encode, const struct options* opts)
{
// if possible, read properties from file
struct gpujpeg_image_parameters file_param_image = gpujpeg_default_image_parameters();
const char *raw_file = encode ? in : out;
if ( param_image->width == 0 || param_image->height == 0 || param_image->pixel_format == GPUJPEG_PIXFMT_NONE ||
param_image->color_space == GPUJPEG_NONE ) {
gpujpeg_image_get_properties(raw_file, &file_param_image, encode);
}
param_image->width = USE_IF_NOT_NULL_ELSE(param_image->width, file_param_image.width);
param_image->height = USE_IF_NOT_NULL_ELSE(param_image->height, file_param_image.height);
if ( param_image->color_space == GPUJPEG_NONE ) {
param_image->color_space = USE_IF_NOT_NULL_ELSE(file_param_image.color_space, GPUJPEG_RGB);
}
if ( param_image->pixel_format == GPUJPEG_PIXFMT_NONE ) {
param_image->pixel_format = file_param_image.pixel_format;
if (!encode && !opts->keep_alpha && file_param_image.pixel_format == GPUJPEG_PIXFMT_AUTODETECT) {
param_image->pixel_format = GPUJPEG_PIXFMT_NO_ALPHA; // keep alpha only if requested
}
}
if ( opts->keep_alpha && encode && param_image->pixel_format == GPUJPEG_4444_U8_P0123 ) {
gpujpeg_sampling_factor_t subs = GPUJPEG_SUBSAMPLING_4444;
if ( opts->subsampling != GPUJPEG_SUBSAMPLING_UNKNOWN && (opts->subsampling & 0xFF) == 0 ) {
subs = opts->subsampling | opts->subsampling >> 24; // copy Y samp factor to alpha
}
gpujpeg_parameters_chroma_subsampling(param, subs);
}
if (encode && (param_image->width <= 0 || param_image->height <= 0)) {
fprintf(stderr, "Image dimensions must be set to nonzero values!\n");
return false;
}
if (encode && param_image->pixel_format == GPUJPEG_PIXFMT_NONE) {
fprintf(stderr, "Pixel format must be set!\n");
return false;
}
return true;
}
static enum gpujpeg_pixel_format
parse_pixel_format(const TCHAR *arg)
{
const char* pf = tstr_to_mbs(arg);
if ( strcmp(pf, "help") == 0 ) {
printf("Available pixel formats:\n");
gpujpeg_print_pixel_formats();
return GPUJPEG_PIXFMT_NONE;
}
const enum gpujpeg_pixel_format ret = gpujpeg_pixel_format_by_name(pf);
if ( ret == GPUJPEG_PIXFMT_NONE ) {
fprintf(stderr, "Unknown pixel format '%s'!\n", pf);
}
return ret;
}
/// dump input file if not a regular file (eg. test patern or /dev/random)
static void
debug_dump_infile(const char* filename, const uint8_t* image_data, size_t image_size,
const struct gpujpeg_image_parameters* param_image)
{
#ifdef _UNICODE
FILE* f = _wfopen(mbs_to_wstr(filename), L"rb");
#else
FILE* f = fopen(filename, "rb");
#endif
long file_sz = 0;
if ( f != NULL ) {
fseek(f, 0, SEEK_END);
file_sz = ftell(f);
fclose(f);
}
if ( file_sz > 0 ) { // regular file
return;
}
char raw_name[256];
const char* beg = strrchr(filename, '/');
if ( beg == NULL ) {
beg = strrchr(filename, '\\');
}
if ( beg == NULL ) {
beg = filename;
}
else {
beg += 1;
}
snprintf(raw_name, sizeof raw_name, "input-%s", beg);
char* end = strrchr(raw_name, '.');
if ( end == NULL ) {
snprintf(raw_name + strlen(raw_name), sizeof raw_name - strlen(raw_name), ".");
end = raw_name + strlen(raw_name);
}
else {
end += 1;
}
snprintf(end, sizeof raw_name - (end - raw_name), "XXX");
if ( gpujpeg_image_save_to_file(raw_name, image_data, image_size, param_image) == 0 ) {
printf("Input data saved to file %s.\n", raw_name);
}
}
enum { CODER_OPTS_COUNT = 10 };
struct coder_opts
{
char* opt;
char* val;
};
static int
assign_coder_opt(struct coder_opts* encoder_opts, struct coder_opts* decoder_opts, char* optval)
{
if (strcmp(optval, "help") == 0) {
printf("Available options:\n");
printf("decoder:\n");
gpujpeg_decoder_print_options();
printf("\nencoder:\n");
gpujpeg_encoder_print_options();
return 1;
}
char* opt = optval;
char* delim = strchr(optval, '=');
if ( delim == NULL ) {
fprintf(stderr, "No value for %s!\n", optval);
return -1;
}
if ( strncmp(optval, "enc_", 4) != 0 && strncmp(optval, "dec_", 4) != 0 ) {
fprintf(stderr, "Option should start with either enc_ or dec_, given %s!\n", optval);
return -1;
}
*delim = '\0';
char *val = delim + 1;
struct coder_opts* opts = strncmp(optval, "enc_", 4) == 0 ? encoder_opts : decoder_opts;
for ( int i = 0; i < CODER_OPTS_COUNT; ++i ) {
if ( opts[i].opt == NULL ) {
opts[i].opt = opt;
opts[i].val = val;
return 0;
}
}
fprintf(stderr, "Too much options!\n");
return -1;
}
static bool
set_encoder_opts(struct gpujpeg_encoder* encoder, const struct coder_opts* opts)
{
while ( (*opts).opt != NULL ) {
if ( gpujpeg_encoder_set_option(encoder, (*opts).opt, (*opts).val) != 0 ) {
return false;
}
opts++;
}
return true;
}
static bool
set_decoder_opts(struct gpujpeg_decoder* decoder, const struct coder_opts* opts)
{
while ( (*opts).opt != NULL ) {
if ( gpujpeg_decoder_set_option(decoder, (*opts).opt, (*opts).val) != 0 ) {
return false;
}
opts++;
}
return true;
}
static gpujpeg_sampling_factor_t
gpujpeg_subsampling_from_name_from_tstr(const TCHAR* tsubsampling)
{
const char* subsampling = tstr_to_mbs(tsubsampling);
const gpujpeg_sampling_factor_t ret = gpujpeg_subsampling_from_name(subsampling);
if ( ret == GPUJPEG_SUBSAMPLING_UNKNOWN ) {
fprintf(stderr, "Unknown subsampling '%s'!\n", subsampling);
}
return ret;
}
#ifndef GIT_REV
#define GIT_REV "unknown"
#endif
int
#ifdef _UNICODE
wmain(int argc, wchar_t *argv[])
#else
main(int argc, char *argv[])
#endif
{
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8); // see also https://stackoverflow.com/questions/1660492/utf-8-output-on-windows-console
#endif
printf("GPUJPEG rev %s built " __DATE__ " " __TIME__ " \n", GIT_REV);
int ret = EXIT_SUCCESS;
// Default coder parameters
struct gpujpeg_parameters param;
gpujpeg_set_default_parameters(¶m);
param.verbose = GPUJPEG_LL_STATUS;
// Default image parameters
struct gpujpeg_image_parameters param_image;
gpujpeg_image_set_default_parameters(¶m_image);
// Original image parameters in conversion
struct gpujpeg_image_parameters param_image_original;
gpujpeg_image_set_default_parameters(¶m_image_original);
// Other parameters
int device_id = 0;
_Bool encode = 0;
_Bool decode = 0;
_Bool convert = 0;
_Bool component_range = 0;
int iterate = 1;
_Bool use_opengl = 0;
bool debug = false;
struct coder_opts decoder_options[CODER_OPTS_COUNT + 1] = {0};
struct coder_opts encoder_options[CODER_OPTS_COUNT + 1] = {0};
// Flags
struct options opts = {.subsampling = GPUJPEG_SUBSAMPLING_UNKNOWN,
.native_file_format = false,
.keep_alpha = false};
int rc;
param_image.color_space = GPUJPEG_NONE;
param_image.pixel_format = GPUJPEG_PIXFMT_NONE;
param.restart_interval = RESTART_AUTO;
// Parse command line
struct option longopts[] = {
{_T("alpha"), no_argument, 0, 'a'},
{_T("debug"), no_argument, 0, 'b'},
{_T("help"), no_argument, 0, 'h'},
{_T("fullhelp"), no_argument, 0, 'H'},
{_T("verbose"), optional_argument, 0, 'v'},
{_T("device"), required_argument, 0, 'D'},
{_T("device-list"), no_argument, 0, 'L'},
{_T("size"), required_argument, 0, 's'},
{_T("pixel-format"), required_argument, 0, 'f'},
{_T("colorspace"), required_argument, 0, 'c'},
{_T("quality"), required_argument, 0, 'q'},
{_T("restart"), required_argument, 0, 'r'},
{_T("segment-info"), optional_argument, 0, 'g'},
{_T("subsampled"), optional_argument, 0, 'S'},
{_T("interleaved"), optional_argument, 0, 'i'},
{_T("encode"), no_argument, 0, 'e'},
{_T("decode"), no_argument, 0, 'd'},
{_T("convert"), no_argument, 0, 'C'},
{_T("component-range"), no_argument, 0, 'R'},
{_T("iterate"), required_argument, 0, 'n'},
{_T("use-opengl"), no_argument, 0, 'o'},
{_T("info"), required_argument, 0, 'I'},
{_T("native"), no_argument, 0, 'N'},
{_T("version"), no_argument, 0, 'V'},
{0}
};
int ch = '\0';
int optindex = 0;
TCHAR* pos = 0;
while ( (ch = getopt_long(argc, argv, _T("CD:HI:LNO:RS::Vabc:edf:ghin:oq:r:s:v"), longopts, &optindex)) != -1 ) {
switch (ch) {
case 'a':
opts.keep_alpha = true;
break;
case 'h':
print_help(false);
return 0;
case 'H':
print_help(true);
return 0;
case 'v':
if (optarg) {
param.verbose = atoi(optarg); // NOLINT(cert-err34-c): not important
} else {
param.verbose += 1;
}
break;
case 's':
pos = _tcsstr(optarg, _T("x"));
if ( pos == NULL || pos == optarg ) {
fprintf(stderr, "Incorrect image size '%s'! Use a format 'WxH'.\n", tstr_to_mbs(optarg));
return -1;
}
param_image.width = atoi(optarg);
param_image.height = atoi(pos + 1);
break;
case 'c':
param_image.color_space = gpujpeg_color_space_by_name(tstr_to_mbs(optarg));
if ( param_image.color_space == GPUJPEG_NONE ) {
if ( _tcscmp(optarg, _T("help")) == 0 ) {
return EXIT_SUCCESS;
}
fprintf(stderr, "Colorspace '%s' is not available!\n", tstr_to_mbs(optarg));
return EXIT_FAILURE;
}
break;
case 'f':
param_image.pixel_format = parse_pixel_format(optarg);
if (param_image.pixel_format == GPUJPEG_PIXFMT_NONE) {
return 1;
}
break;
case 'q':
param.quality = atoi(optarg);
if ( param.quality < 0 )
param.quality = 0;
if ( param.quality > 100 )
param.quality = 100;
break;
case 'r':
param.restart_interval = atoi(optarg);
if ( param.restart_interval < RESTART_AUTO ) {
fprintf(stderr, "Wrong restart interval '%s'!\n", tstr_to_mbs(optarg));
return 1;
}
break;
case 'g':
if ( optarg == NULL || _tcscmp(optarg, _T("true")) == 0 || atoi(optarg) )
param.segment_info = 1;
else
param.segment_info = 0;
break;
case 'S':
if (optarg == NULL) {
opts.subsampling = GPUJPEG_SUBSAMPLING_420;
break;
}
opts.subsampling = gpujpeg_subsampling_from_name_from_tstr(optarg);
if ( opts.subsampling == GPUJPEG_SUBSAMPLING_UNKNOWN ) {
return 1;
}
break;
case 'L':
return gpujpeg_print_devices_info() < 0 ? 1 : 0;
case 'i':
if ( optarg == NULL || _tcscmp(optarg, _T("true")) == 0 || atoi(optarg) )
param.interleaved = 1;
else
param.interleaved = 0;
break;
case 'e':
encode = 1;
break;
case 'd':
decode = 1;
break;
case 'D':
device_id = atoi(optarg);
break;
case 'C':
convert = 1;
memcpy(¶m_image_original, ¶m_image, sizeof(struct gpujpeg_image_parameters));
break;
case 'R':
component_range = 1;
break;
case 'n':
iterate = atoi(optarg);
break;
case 'o':
use_opengl = 1;
break;
case 'I':
return print_image_info(tstr_to_mbs(optarg), param.verbose);
case 'N':
opts.native_file_format = true;
break;
case 'V':
return 0; // already printed, just exit
case 'b':
debug = true;
break;
case 'O':
rc = assign_coder_opt(encoder_options, decoder_options, tstr_to_mbs(optarg));
if (rc != 0) {
return rc < 0 ? 1 : 0;
}
break;
case '?':
return -1;
default:
fprintf(stderr, "Unrecognized option '%c' (code 0%o)\n", ch, ch);
print_help(false);
return -1;
}
}
argc -= optind;
argv += optind;
if ( opts.subsampling != GPUJPEG_SUBSAMPLING_UNKNOWN ) {
gpujpeg_parameters_chroma_subsampling(¶m, opts.subsampling);
}
// Show info about image samples range
if ( component_range == 1 ) {
// For each image
for ( int index = 0; index < argc; index++ ) {
gpujpeg_image_range_info(tstr_to_mbs(argv[index]), param_image.width, param_image.height,
param_image.pixel_format);
}
return 0;
}
if ( argv[0] != NULL && _tcscmp(argv[0], _T("exts")) == 0 ) {
gpujpeg_image_get_file_format("help");
return 0;
}
if (iterate <= 0) {
fprintf(stderr, "Number of iterations must be greater than zero, got: %d\n", iterate);
return -1;
}
// Source image and target image must be presented
if ( argc < 2 || argc % 2 != 0 ) {
fprintf(stderr, "Please supply source and destination image filename(s)!\n");
print_help(false);
return -1;
}
// Init device
int flags = GPUJPEG_INIT_DEV_VERBOSE;
struct gpujpeg_opengl_context *gl_context = NULL;
if ( use_opengl ) {
flags |= GPUJPEG_OPENGL_INTEROPERABILITY;
if ( gpujpeg_opengl_init(&gl_context) != 0 ) {
fprintf(stderr, "Cannot initialize OpenGL context!\n");
return -1;
}
}
if ( gpujpeg_init_device(device_id, flags) != 0 )
return -1;
// Convert
if ( convert == 1 ) {
// Encode images
for ( int index = 0; index < argc; index += 2 ) {
// Get input and output image
const char* input = tstr_to_mbs(argv[index]);
const char* output = tstr_to_mbs(argv[index + 1]);
// Perform conversion
gpujpeg_image_convert(input, output, param_image_original, param_image);
}
return 0;
}
// Detect action if none is specified
if ( encode == 0 && decode == 0 && argc == 2 ) {
enum gpujpeg_image_file_format input_format = gpujpeg_image_get_file_format(tstr_to_mbs(argv[0]));
enum gpujpeg_image_file_format output_format = gpujpeg_image_get_file_format(tstr_to_mbs(argv[1]));
if ( GPUJPEG_IMAGE_FORMAT_IS_RAW(input_format) && output_format == GPUJPEG_IMAGE_FILE_JPEG ) {
encode = 1;
}
else if ( input_format == GPUJPEG_IMAGE_FILE_JPEG && GPUJPEG_IMAGE_FORMAT_IS_RAW(output_format) ) {
decode = 1;
}
}
if ( encode == 0 && decode == 0 ) {
fprintf(stderr, "Action can't be recognized for specified images!\n");
fprintf(stderr, "You must specify --encode or --decode option!\n");
return -1;
}
struct gpujpeg_parameters param_saved = param;
struct gpujpeg_image_parameters param_image_saved = param_image;
if ( encode == 1 ) {
// Create OpenGL texture
struct gpujpeg_opengl_texture* texture = NULL;
if ( use_opengl ) {
assert(param_image.pixel_format == GPUJPEG_444_U8_P012);
int texture_id = gpujpeg_opengl_texture_create(param_image.width, param_image.height, NULL);
assert(texture_id != 0);
texture = gpujpeg_opengl_texture_register(texture_id, GPUJPEG_OPENGL_TEXTURE_READ);
assert(texture != NULL);
}
// Create encoder
struct gpujpeg_encoder* encoder = gpujpeg_encoder_create(NULL);
if ( encoder == NULL || !set_encoder_opts(encoder, encoder_options) ) {
fprintf(stderr, "Failed to create encoder!\n");
return -1;
}
// Encode images
for ( int index = 0; index < argc; index += 2 ) {
// Get and check input and output image
const char* input = tstr_to_mbs(argv[index]);
const char* output = tstr_to_mbs(argv[index + 1]);
enum gpujpeg_image_file_format input_format = gpujpeg_image_get_file_format(input);
enum gpujpeg_image_file_format output_format = gpujpeg_image_get_file_format(output);
if ( !GPUJPEG_IMAGE_FORMAT_IS_RAW(input_format) ) {
fprintf(stderr, "[Warning] Encoder input file [%s] should be raw image (" RAW_EXTS ")!\n", input);
gpujpeg_image_get_file_format("rawhelp");
if ( input_format & GPUJPEG_IMAGE_FILE_JPEG ) {
return -1;
}
}
if ( output_format != GPUJPEG_IMAGE_FILE_JPEG ) {
fprintf(stderr, "[%s] Encoder output file [%s] should be JPEG image (*.jpg)!\n",
output_format == GPUJPEG_IMAGE_FILE_UNKNOWN ? "Warning" : "Error", output);
if ( output_format != GPUJPEG_IMAGE_FILE_UNKNOWN ) {
ret = EXIT_FAILURE; continue;
}
}
param = param_saved;
param_image = param_image_saved;
if ( !adjust_params(¶m, ¶m_image, input, output, encode, &opts) ) {
ret = EXIT_FAILURE; continue;
}
if ( opts.native_file_format ) {
param.color_space_internal = param_image.color_space;
}
// Encode image
double duration = gpujpeg_get_time();
printf("\nEncoding Image [%s]: ", input);
print_gpujpeg_image_parameters(param_image, true, NULL);
// Load image
size_t image_size = gpujpeg_image_calculate_size(¶m_image);
uint8_t* image = NULL;
if ( gpujpeg_image_load_from_file(input, &image, &image_size) != 0 ) {
fprintf(stderr, "Failed to load image [%s]!\n", input);
ret = EXIT_FAILURE; continue;
}
if ( debug ) {
debug_dump_infile(input, image, image_size, ¶m_image);
}
duration = gpujpeg_get_time() - duration;
printf("Load Image: %10.2f ms\n", duration * 1000.0);
// Prepare encoder input
struct gpujpeg_encoder_input encoder_input;
if ( use_opengl ) {
gpujpeg_opengl_texture_set_data(texture->texture_id, image);
gpujpeg_encoder_input_set_texture(&encoder_input, texture);
} else {
gpujpeg_encoder_input_set_image(&encoder_input, image);
}
// Encode image
uint8_t* image_compressed = NULL;
size_t image_compressed_size = 0;
for ( int iteration = 0; iteration < iterate; iteration++ ) {
if ( iterate > 1 ) {
printf("\nIteration #%d:\n", iteration + 1);
}
rc = gpujpeg_encoder_encode(encoder, ¶m, ¶m_image, &encoder_input, &image_compressed, &image_compressed_size);
if ( rc != GPUJPEG_NOERR ) {
fprintf(stderr, "Failed to encode image [%s]!\n", input);
ret = EXIT_FAILURE; continue;
}
}
duration = gpujpeg_get_time();
// Save image
if ( image_compressed == NULL ||
gpujpeg_image_save_to_file(output, image_compressed, image_compressed_size, ¶m_image) != 0 ) {
fprintf(stderr, "Failed to save image [%s]!\n", output);
ret = EXIT_FAILURE;
} else {
duration = gpujpeg_get_time() - duration;
printf("Save Image: %10.4f ms\n", duration * 1000.0);
printf("Image Name: %10s\n", output);
}
// Destroy image
gpujpeg_image_destroy(image);
}
// Destroy OpenGL texture
if ( use_opengl ) {
int texture_id = texture->texture_id;
gpujpeg_opengl_texture_unregister(texture);
gpujpeg_opengl_texture_destroy(texture_id);
}
// Destroy encoder
gpujpeg_encoder_destroy(encoder);
}
if ( decode == 1 ) {
// Create OpenGL texture
struct gpujpeg_opengl_texture* texture = NULL;
if ( use_opengl ) {
assert(param_image.pixel_format == GPUJPEG_444_U8_P012);
int texture_id = gpujpeg_opengl_texture_create(param_image.width, param_image.height, NULL);
assert(texture_id != 0);
texture = gpujpeg_opengl_texture_register(texture_id, GPUJPEG_OPENGL_TEXTURE_WRITE);
assert(texture != NULL);
}
// Create decoder
struct gpujpeg_decoder* decoder = gpujpeg_decoder_create(NULL);
if ( decoder == NULL || !set_decoder_opts(decoder, decoder_options) ) {
fprintf(stderr, "Failed to create decoder!\n");
return -1;
}
// Init decoder if image size is filled
if ( gpujpeg_decoder_init(decoder, ¶m, ¶m_image) != 0 ) {
fprintf(stderr, "Failed to preinitialize decoder!\n");
return -1;
}
// Decode images
for ( int index = 0; index < argc; index += 2 ) {
// Get and check input and output image
const char * input = tstr_to_mbs(argv[index]);
char* output = tstr_to_mbs(argv[index + 1]);
if ( encode == 1 ) {
static char buffer_output[255];
if ( param_image.color_space != GPUJPEG_RGB ) {
sprintf(buffer_output, "%s.decoded.yuv", output);
}
else {
if ( param.comp_count == 1 ) {
sprintf(buffer_output, "%s.decoded.r", output);
} else {
sprintf(buffer_output, "%s.decoded.rgb", output);
}
}
input = output;
output = buffer_output;
}
enum gpujpeg_image_file_format input_format = gpujpeg_image_get_file_format(input);
enum gpujpeg_image_file_format output_format = gpujpeg_image_get_file_format(output);
if ( input_format != GPUJPEG_IMAGE_FILE_JPEG ) {
fprintf(stderr, "[Warning] Decoder input file [%s] should be JPEG image (*.jpg)!\n", input);
}
if ( !GPUJPEG_IMAGE_FORMAT_IS_RAW(output_format) ) {
fprintf(stderr, "[Warning] Decoder output file [%s] should be raw image (" RAW_EXTS ")!\n", output);
gpujpeg_image_get_file_format("rawhelp");
if ( output_format & GPUJPEG_IMAGE_FILE_JPEG ) {
ret = EXIT_FAILURE; continue;
}
}
param = param_saved;
param_image = param_image_saved;
adjust_params(¶m, ¶m_image, input, output, encode, &opts);
if ( opts.native_file_format ) {
param_image.color_space = GPUJPEG_NONE;
}
gpujpeg_decoder_set_output_format(decoder, param_image.color_space, param_image.pixel_format);
// Decode image
double duration = gpujpeg_get_time();
printf("\nDecoding Image [%s] to ", input);
print_gpujpeg_image_parameters(param_image, true, NULL);
// Load image
size_t image_size = 0;
uint8_t* image = NULL;
if ( gpujpeg_image_load_from_file(input, &image, &image_size) != 0 ) {
fprintf(stderr, "Failed to load image [%s]!\n", input);
ret = EXIT_FAILURE; continue;
}
duration = gpujpeg_get_time() - duration;
printf("Load Image: %10.2f ms\n", duration * 1000.0);
// Prepare decoder output buffer
struct gpujpeg_decoder_output decoder_output;
if ( use_opengl ) {
gpujpeg_decoder_output_set_texture(&decoder_output, texture);
} else {
gpujpeg_decoder_output_set_default(&decoder_output);
}
for ( int iteration = 0; iteration < iterate; iteration++ ) {
if ( iterate > 1 ) {
printf("\nIteration #%d:\n", iteration + 1);
}
// Decode image
if ( (rc = gpujpeg_decoder_decode(decoder, image, image_size, &decoder_output)) != 0 ) {
if (rc == GPUJPEG_ERR_RESTART_CHANGE && param_image.width != 0 && param_image.height != 0) {
fprintf(stderr, "Hint: Do not enter image dimensions to avoid preinitialization or correctly specify restart interval.\n");
}
fprintf(stderr, "Failed to decode image [%s]!\n", input);
ret = EXIT_FAILURE; continue;
}
}
uint8_t* data = NULL;
size_t data_size = 0;
if ( use_opengl ) {
data = malloc(decoder_output.data_size);
gpujpeg_opengl_texture_get_data(texture->texture_id, data, &data_size);
assert(data != NULL && data_size != 0);
} else {
data = decoder_output.data;
data_size = decoder_output.data_size;
}
duration = gpujpeg_get_time();
// Save image
if ( data == NULL ||
gpujpeg_image_save_to_file(output, data, data_size, &decoder_output.param_image) != 0 ) {
fprintf(stderr, "Failed to save image [%s]!\n", output);
ret = EXIT_FAILURE;
}
else {
duration = gpujpeg_get_time() - duration;
printf("Save Image: %10.2f ms\n", duration * 1000.0);
printf("Image Name: %10s\n", output);
}
if ( use_opengl ) {
free(data);
}
// Destroy image
gpujpeg_image_destroy(image);
}
// Destroy OpenGL texture
if ( use_opengl ) {
int texture_id = texture->texture_id;
gpujpeg_opengl_texture_unregister(texture);
gpujpeg_opengl_texture_destroy(texture_id);
}
// Destroy decoder
gpujpeg_decoder_destroy(decoder);
}
if ( use_opengl ) {
gpujpeg_opengl_destroy(gl_context);
}
if ( debug ) {