File: CImageStat.c

package info (click to toggle)
gambas3 3.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 76,984 kB
  • sloc: ansic: 197,178; cpp: 124,076; sh: 18,999; javascript: 7,761; sql: 5,399; makefile: 2,354; perl: 1,397; xml: 490; python: 335
file content (140 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (3)
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
/***************************************************************************

  CImageStat.c

  (c) 2000-2017 BenoƮt Minisini <benoit.minisini@gambas-basic.org>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.

***************************************************************************/

#define __CIMAGESTAT_C

#include <sys/mman.h>
#include "main.h"
#include "image_stat.h"
#include "CImageStat.h"

// static int my_mmap(const char *path, char **paddr, int *plen)
// {
//   struct stat info;
// 	int fd;
// 	void *addr;
// 	size_t len;
// 	  
// 	fd = open(path, O_RDONLY);
//   if (fd < 0)
//   	return -1;
// 
//   if (fstat(fd, &info) < 0)
//     return -1;
// 
//   len = info.st_size;
//   addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
//   if (addr == MAP_FAILED)
//     return -1;
// 	
// 	*paddr = addr;
// 	*plen = len;
// 	return fd;
// }

BEGIN_METHOD(CIMAGESTAT_call, GB_STRING path)

	char *path = GB.FileName(STRING(path), LENGTH(path));
	IMAGE_STREAM stream;
	IMAGE_INFO info = {0};
	CIMAGESTAT *stat;
	
	if (GB.LoadFile(path, strlen(path), &stream.addr, &stream.len))
		return;
	
	stream.pos = 0;
	
	if (IMAGE_get_info(&stream, &info))
	{
		GB.Error("Unable to stat image: &1", IMAGE_error);
		stat = NULL;
	}
	else
	{
		stat = GB.New(GB.FindClass("ImageStat"), NULL, NULL);
		stat->path = GB.NewZeroString(path);
		stat->type = info.type;
		stat->width = info.width;
		stat->height = info.height;
		stat->depth = info.depth;
	}
	
	GB.ReleaseFile(stream.addr, stream.len);
	
	GB.ReturnObject(stat);
	
END_METHOD

BEGIN_METHOD_VOID(CIMAGESTAT_free)

	GB.FreeString(&THIS->path);

END_METHOD

BEGIN_PROPERTY(CIMAGESTAT_path)

	GB.ReturnString(THIS->path);

END_PROPERTY

BEGIN_PROPERTY(CIMAGESTAT_type)

	GB.ReturnConstZeroString(THIS->type);

END_PROPERTY

BEGIN_PROPERTY(CIMAGESTAT_width)

	GB.ReturnInteger(THIS->width);

END_PROPERTY

BEGIN_PROPERTY(CIMAGESTAT_height)

	GB.ReturnInteger(THIS->height);

END_PROPERTY

BEGIN_PROPERTY(CIMAGESTAT_depth)

	GB.ReturnInteger(THIS->depth);

END_PROPERTY

GB_DESC CImageStatDesc[] =
{
  GB_DECLARE("ImageStat", sizeof(CIMAGESTAT)),
  GB_NOT_CREATABLE(),

	GB_STATIC_METHOD("_call", "ImageStat", CIMAGESTAT_call, "(Path)s"),
	GB_METHOD("_free", NULL, CIMAGESTAT_free, NULL),
	
  GB_PROPERTY_READ("Path", "s", CIMAGESTAT_path),
  GB_PROPERTY_READ("Type", "s", CIMAGESTAT_type),
  GB_PROPERTY_READ("Width", "i", CIMAGESTAT_width),
  GB_PROPERTY_READ("Height", "i", CIMAGESTAT_height),
  GB_PROPERTY_READ("Depth", "i", CIMAGESTAT_depth),
  
  GB_END_DECLARE
};