summaryrefslogtreecommitdiff
path: root/android/skia-encoder.cpp
blob: 1a405bb39b5c025c4182c9b0bbce4d2c6cd3c19a (plain)
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
#include <getopt.h>
#include <stdio.h>

#include "SkBitmap.h"
#include "SkPaint.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
#include "SkString.h"
#include "SkImageEncoder.h"
#include "SkImageDecoder.h"

static SkImageEncoder::Type image_type;
static char* src_image = NULL;
static char* dst_image = NULL;

static void usage(const char* pname)
{
    fprintf(stderr,
        "\n  USAGE: %s -src [path] -dst [path] \n"
        "\n  Image Support .PNG(.png), .WEBP(.webp) or .JPEG(.jpeg) Format Only.\n"
        ,pname);
}


static int process_cmdline(int argc, char *argv[])
{
	char c;

#if 0
	int i;
	printf("Argumen Num = %d\n", argc);
	for (i = 0; i < argc; i++) {
		printf("Argument %d:%s\n", i, argv[i]);
	}
	printf("==>%s:%d\n", __FUNCTION__, __LINE__);
#endif

	const struct option long_opts[] = {
		{"help", no_argument, NULL, 0 },
		{"src", required_argument, NULL, 1 },
		{"dst", required_argument, NULL, 2 },
		{NULL, 0, 0, 0}};

	if (argc <= 1) {
        usage(argv[0]); /* No argument */
        return 1;
    }

	while ((c = getopt_long_only(argc,argv,"?",long_opts,NULL)) != EOF) {
        switch (c) {
			case '?':
				goto out;
				break;

			case 0:
				goto out;
				break;

			case 1:
				src_image = strdup(optarg);
				break;

			case 2:
				dst_image = strdup(optarg);
				break;

			default:
				goto out;
				break;
        }
	}

	if ((NULL == src_image) || (NULL == dst_image)) {
		goto out;
	}

	if ((NULL != strstr(dst_image, ".PNG")) || (NULL != strstr(dst_image, ".png"))) {
		image_type = SkImageEncoder::kPNG_Type;
	} else if ((NULL != strstr(dst_image, ".WEBP")) || (NULL != strstr(dst_image, ".webp"))) {
		image_type = SkImageEncoder::kWEBP_Type;
	} else if ((NULL != strstr(dst_image, ".JPEG")) || (NULL != strstr(dst_image, ".jpeg"))) {
		image_type = SkImageEncoder::kJPEG_Type;
	} else {
		goto out;
	}

	printf("=============  Start Encode ==============\n");
	if (SkImageEncoder::kPNG_Type == image_type) {
		printf("INPUT: IMAGE TYPE  : PNG IMAGE\n");
	} else if (SkImageEncoder::kWEBP_Type == image_type) {
		printf("INPUT: IMAGE TYPE  : WEBP IMAGE\n");
	} else if (SkImageEncoder::kJPEG_Type == image_type) {
		printf("INPUT: IMAGE TYPE  : JPEG IMAGE\n");
	}

	printf("INPUT: Src IMAGE   : %s\n", src_image);
	printf("INPUT: Dest IMAGE  : %s\n", dst_image);

	return 1;

out:
	usage(argv[0]);
	exit(0);
}


int main(int argc, char* argv[])
{
	int ret = -1;
	int i = 0;

	process_cmdline(argc, argv);

	SkBitmap srcImage;

	ret = SkImageDecoder::DecodeFile(src_image, &srcImage);
	printf("Decode is successful? %s \n", (ret ? "Yes" : "No"));

	ret =SkImageEncoder::EncodeFile(dst_image, srcImage, image_type, 100);
	printf("Encode is successful? %s \n", (ret ? "Yes" : "No"));

	printf("=============  Finish Encode ==============\n");

	return 0;
}