-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.c
88 lines (72 loc) · 2.44 KB
/
query.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
// SCMTIFF Copyright (C) 2012-2016 Robert Kooima
//
// 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 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include <stdio.h>
#include <stdbool.h>
#include "scm.h"
#include "scmdat.h"
#include "scmdef.h"
//------------------------------------------------------------------------------
// Determine whether page x is a leaf of scm s. That is: if x has data but
// has no children with data.
static bool isleaf(scm *s, long long i)
{
if (scm_get_offset(s, i))
{
long long x = scm_get_index(s, i);
long long j;
for (int c = 0; c < 4; c++)
{
// If child c exists and has data, then x is not a leaf.
if ((j = scm_search(s, scm_page_child(x, c))) != -1)
if (scm_get_offset(s, j) > 0)
return false;
}
return true;
}
return false;
}
int query(int argc, char **argv)
{
long long total = 0;
// Iterate over all input file arguments.
for (int argi = 0; argi < argc; argi++)
{
long long size = 0;
long long length = 0;
long long leaves = 0;
scm *s;
if ((s = scm_ifile(argv[argi])))
{
if (scm_read_catalog(s))
{
length = scm_get_length(s);
for (long long i = 0; i < length; i++)
{
if (isleaf(s, i))
{
leaves += 1;
size += (long long) s->n
* (long long) s->n
* (long long) s->c
* (long long) s->b / 8;
}
}
}
scm_close(s);
printf("%s pixels: %d channels: %d bits: %d pages: %lld leaves: %lld bytes: %lld\n", argv[argi], s->n, s->c, s->b, length, leaves, size);
}
total += size;
}
printf("total bytes: %lld\n", total);
return 0;
}
//------------------------------------------------------------------------------