1/*
2 * Copyright 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <img_utils/EndianUtils.h>
18
19namespace android {
20namespace img_utils {
21
22EndianOutput::EndianOutput(Output* out, Endianness end)
23        : mOffset(0), mOutput(out), mEndian(end) {}
24
25EndianOutput::~EndianOutput() {}
26
27status_t EndianOutput::open() {
28    mOffset = 0;
29    return mOutput->open();
30}
31
32status_t EndianOutput::close() {
33    return mOutput->close();
34}
35
36void EndianOutput::setEndianness(Endianness end) {
37    mEndian = end;
38}
39
40uint32_t EndianOutput::getCurrentOffset() const {
41    return mOffset;
42}
43
44Endianness EndianOutput::getEndianness() const {
45    return mEndian;
46}
47
48status_t EndianOutput::write(const uint8_t* buf, size_t offset, size_t count) {
49    status_t res = OK;
50    if((res = mOutput->write(buf, offset, count)) == OK) {
51        mOffset += count;
52    }
53    return res;
54}
55
56status_t EndianOutput::write(const int8_t* buf, size_t offset, size_t count) {
57    return write(reinterpret_cast<const uint8_t*>(buf), offset, count);
58}
59
60#define DEFINE_WRITE(_type_) \
61status_t EndianOutput::write(const _type_* buf, size_t offset, size_t count) { \
62    return writeHelper<_type_>(buf, offset, count); \
63}
64
65DEFINE_WRITE(uint16_t)
66DEFINE_WRITE(int16_t)
67DEFINE_WRITE(uint32_t)
68DEFINE_WRITE(int32_t)
69DEFINE_WRITE(uint64_t)
70DEFINE_WRITE(int64_t)
71
72status_t EndianOutput::write(const float* buf, size_t offset, size_t count) {
73    assert(sizeof(float) == sizeof(uint32_t));
74    return writeHelper<uint32_t>(reinterpret_cast<const uint32_t*>(buf), offset, count);
75}
76
77status_t EndianOutput::write(const double* buf, size_t offset, size_t count) {
78    assert(sizeof(double) == sizeof(uint64_t));
79    return writeHelper<uint64_t>(reinterpret_cast<const uint64_t*>(buf), offset, count);
80}
81
82} /*namespace img_utils*/
83} /*namespace android*/
84