source: branches/4.2/components/native/bf-cpp/source/minimum_writer.cpp @ 6496

Revision 6496, 5.7 KB checked in by melissa, 10 years ago (diff)

Updated bf-cpp to compile with latest ome.xml.* and loci.formats.meta.* changes.

Line 
1//
2// minimum_writer.cpp
3//
4
5/*
6OME Bio-Formats C++ bindings for native access to Bio-Formats Java library.
7Copyright (c) 2008-@year@, UW-Madison LOCI.
8All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17    * Neither the name of the UW-Madison LOCI nor the
18      names of its contributors may be used to endorse or promote products
19      derived from this software without specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY
22EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY
25DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33/*
34IMPORTANT NOTE: Although this software is distributed according to a
35"BSD-style" license, it requires the Bio-Formats Java library to do
36anything useful, which is licensed under the GPL v2 or later.
37As such, if you wish to distribute this software with Bio-Formats itself,
38your combined work must be distributed under the terms of the GPL.
39*/
40
41// A C++ version of the Bio-Formats MinimumWriter example.
42// For the original Java version, see:
43//   components/bio-formats/utils/MinimumWriter.java
44
45// for Bio-Formats C++ bindings
46#include "bio-formats.h"
47using jace::JNIException;
48using jace::proxy::java::io::IOException;
49using jace::proxy::java::lang::Boolean;
50using jace::proxy::java::lang::Integer;
51using jace::proxy::loci::formats::FormatException;
52using jace::proxy::loci::formats::FormatTools;
53using jace::proxy::loci::formats::ImageWriter;
54using jace::proxy::loci::formats::MetadataTools;
55using jace::proxy::loci::formats::meta::IMetadata;
56#include "ome-xml.h"
57using jace::proxy::ome::xml::model::enums::DimensionOrder;
58using jace::proxy::ome::xml::model::enums::PixelType;
59using jace::proxy::ome::xml::model::primitives::PositiveInteger;
60
61#include <iostream>
62using std::cout;
63using std::endl;
64
65#include <string>
66using std::string;
67
68// for rand
69#include <stdio.h>
70#include <stdlib.h>
71
72#if defined (_WIN32)
73#define PATHSEP string(";")
74#else
75#define PATHSEP string(":")
76#endif
77
78// -- Methods --
79
80/* Initializes the Java virtual machine. */
81void createJVM() {
82  cout << "Creating JVM... ";
83  jace::StaticVmLoader loader(JNI_VERSION_1_4);
84  jace::OptionList list;
85  list.push_back(jace::ClassPath(
86    "jace-runtime.jar" + PATHSEP +
87    "bio-formats.jar" + PATHSEP +
88    "loci_tools.jar"));
89  list.push_back(jace::CustomOption("-Xcheck:jni"));
90  list.push_back(jace::CustomOption("-Xmx256m"));
91  list.push_back(jace::CustomOption("-Djava.awt.headless=true"));
92  //list.push_back(jace::CustomOption("-verbose:jni"));
93  jace::helper::createVm(loader, list, false);
94  cout << "JVM created." << endl;
95}
96
97/*
98 * Demonstrates the minimum amount of metadata
99 * necessary to write out an image plane.
100 */
101bool minWrite(int argc, const char *argv[]) {
102  if (argc < 2) {
103    cout << "Please specify an output file name." << endl;
104    return -1;
105  }
106  string id = argv[1];
107
108  // create blank 512x512 image
109  cout << "Creating random image..." << endl;
110  int w = 512, h = 512;
111  int pixelType = FormatTools::UINT16();
112
113  //byte[] img = new byte[w * h * FormatTools.getBytesPerPixel(pixelType)];
114  int planeSize = w * h * FormatTools::getBytesPerPixel(pixelType);
115  ByteArray img(planeSize); // pre-allocate buffer
116
117  // fill with random data
118  for (int i=0; i<planeSize; i++) img[i] = rand();
119
120  // create metadata object with minimum required metadata fields
121  cout << "Populating metadata..." << endl;
122  IMetadata meta = MetadataTools::createOMEXMLMetadata();
123  meta.createRoot();
124  meta.setImageID("Image:0", 0);
125  meta.setPixelsID("Pixels:0", 0);
126  meta.setPixelsBinDataBigEndian(Boolean(1), 0, 0);
127  meta.setPixelsDimensionOrder(DimensionOrder::XYZCT(), 0);
128  meta.setPixelsType(
129    PixelType::fromString(FormatTools::getPixelTypeString(pixelType)), 0);
130  meta.setPixelsSizeX(PositiveInteger(Integer(w)), 0);
131  meta.setPixelsSizeY(PositiveInteger(Integer(h)), 0);
132  meta.setPixelsSizeZ(PositiveInteger(Integer(1)), 0);
133  meta.setPixelsSizeC(PositiveInteger(Integer(1)), 0);
134  meta.setPixelsSizeT(PositiveInteger(Integer(1)), 0);
135  meta.setChannelID("Channel:0:0", 0, 0);
136  meta.setChannelSamplesPerPixel(Integer(1), 0, 0);
137
138  // write image plane to disk
139  cout << "Writing image to '" << id << "'..." << endl;
140  ImageWriter writer;
141  writer.setMetadataRetrieve(meta);
142  writer.setId(id);
143  writer.saveBytes(img, true);
144  writer.close();
145
146  cout << "Done." << endl;
147}
148
149int main(int argc, const char *argv[]) {
150  try {
151    createJVM();
152    minWrite(argc, argv);
153  }
154  catch (FormatException& fe) {
155    fe.printStackTrace();
156    return -2;
157  }
158  catch (IOException& ioe) {
159    ioe.printStackTrace();
160    return -3;
161  }
162  catch (JNIException& jniException) {
163    cout << "An unexpected JNI error occurred. " << jniException.what() << endl;
164    return -4;
165  }
166  catch (std::exception& e) {
167    cout << "An unexpected C++ error occurred. " << e.what() << endl;
168    return -5;
169  }
170}
Note: See TracBrowser for help on using the repository browser.