FreeRDP
Loading...
Searching...
No Matches
img-cnv.c
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <winpr/image.h>
5
6static const int formats[] = { WINPR_IMAGE_BITMAP, WINPR_IMAGE_PNG, WINPR_IMAGE_JPEG,
7 WINPR_IMAGE_WEBP };
8
9static void usage(int argc, char* argv[])
10{
11 const char* prg = "INVALID";
12 if (argc > 0)
13 prg = argv[0];
14
15 (void)fprintf(stdout, "%s <src> <dst>\n", prg);
16 (void)fprintf(stdout, "\tConvert image <src> to <dst>\n");
17 (void)fprintf(stdout, "\tSupported formats (for this build):\n");
18
19 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
20 {
21 const int format = formats[x];
22 const char* ext = winpr_image_format_extension(format);
23 const char* mime = winpr_image_format_mime(format);
24 const BOOL supported = winpr_image_format_is_supported(format);
25 if (supported)
26 {
27 (void)fprintf(stdout, "\t\t%s [.%s]\n", mime, ext);
28 }
29 }
30}
31
32static int detect_format(const char* name)
33{
34 const char* dot = strrchr(name, '.');
35 if (!dot)
36 {
37 (void)fprintf(stderr, "'%s' does not have a file extension\n", name);
38 return -1;
39 }
40
41 for (size_t x = 0; x < ARRAYSIZE(formats); x++)
42 {
43 const int format = formats[x];
44 const char* ext = winpr_image_format_extension(format);
45 const char* mime = winpr_image_format_mime(format);
46 const BOOL supported = winpr_image_format_is_supported(format);
47 if (strcmp(&dot[1], ext) == 0)
48 {
49 (void)fprintf(stdout, "'%s' is of format %s [supported:%s]\n", name, mime,
50 supported ? "true" : "false");
51 if (!supported)
52 return -2;
53 return format;
54 }
55 }
56
57 (void)fprintf(stderr, "'%s' is a unsupported format\n", name);
58 return -3;
59}
60
61int main(int argc, char* argv[])
62{
63 int rc = -4;
64 if (argc != 3)
65 {
66 usage(argc, argv);
67 return -1;
68 }
69
70 const char* src = argv[1];
71 const char* dst = argv[2];
72
73 const int sfmt = detect_format(src);
74 const int dfmt = detect_format(dst);
75 if ((sfmt < 0) || (dfmt < 0))
76 {
77 usage(argc, argv);
78 return -2;
79 }
80
81 wImage* img = winpr_image_new();
82 if (!img)
83 {
84 return -3;
85 }
86
87 const int rrc = winpr_image_read(img, src);
88 if (rrc <= 0)
89 {
90 (void)fprintf(stderr, "Failed to read image '%s': %d\n", src, rrc);
91 goto fail;
92 }
93 const int wrc = winpr_image_write(img, dst);
94 if (wrc <= 0)
95 {
96 (void)fprintf(stderr, "Failed to write image '%s': %d\n", dst, wrc);
97 goto fail;
98 }
99
100 (void)fprintf(stdout, "Successfully converted '%s' to '%s'\n", src, dst);
101 rc = 0;
102fail:
103 winpr_image_free(img, TRUE);
104 return rc;
105}