-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathdocs-examples-imagecache.cpp
More file actions
78 lines (64 loc) · 2.47 KB
/
Copy pathdocs-examples-imagecache.cpp
File metadata and controls
78 lines (64 loc) · 2.47 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
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
///////////////////////////////////////////////////////////////////////////
// This file contains code examples from the ImageCache chapter of the
// main OpenImageIO documentation.
//
// To add an additional test, replicate the section below. Change
// "example1" to a helpful short name that identifies the example.
// BEGIN-imagecache-example1
#include <OpenImageIO/imagecache.h>
#include <iostream>
#include <vector>
using namespace OIIO;
void
example1()
{
// Create an image cache and set some options
auto cache = ImageCache::create();
cache->attribute("max_memory_MB", 500.0f);
cache->attribute("autotile", 64);
ustring filename("tahoe.tif");
// Get information about a file
ImageSpec spec;
bool ok = cache->get_imagespec(filename, spec);
if (ok)
std::cout << "resolution is " << spec.width << "x" << spec.height
<< "\n";
// Get a block of pixels from a file.
const int xbegin = 0, xend = 4, ybegin = 0, yend = 4;
const int nchannels = spec.nchannels;
ROI roi(xbegin, xend, ybegin, yend, 0, 1, 0, nchannels);
std::vector<float> pixels(roi.width() * roi.height() * nchannels);
image_span<float> pixelspan(pixels.data(), nchannels, roi.width(),
roi.height());
cache->get_pixels(filename, 0, 0, roi, pixelspan);
// Request and hold a tile, do some work with its pixels, then release
ImageCache::Tile* tile = cache->get_tile(filename, 0, 0, 0, 0, 0);
if (tile) {
// The tile won't be freed until we release it, so this is safe:
TypeDesc format;
const void* p = cache->tile_pixels(tile, format);
// Now p points to the raw pixels of the tile, whose data format
// is given by 'format'.
(void)p;
cache->release_tile(tile);
// Now cache is permitted to free the tile when needed
}
// Note that all files were referenced by name, we never had to open
// or close any files, and all the resource and memory management
// was automatic.
ImageCache::destroy(cache);
}
// END-imagecache-example1
//
///////////////////////////////////////////////////////////////////////////
int
main(int /*argc*/, char** /*argv*/)
{
// Each example function needs to get called here, or it won't execute
// as part of the test.
example1();
return 0;
}