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 
19 namespace android {
20 namespace img_utils {
21 
EndianOutput(Output * out,Endianness end)22 EndianOutput::EndianOutput(Output* out, Endianness end)
23         : mOffset(0), mOutput(out), mEndian(end) {}
24 
~EndianOutput()25 EndianOutput::~EndianOutput() {}
26 
open()27 status_t EndianOutput::open() {
28     mOffset = 0;
29     return mOutput->open();
30 }
31 
close()32 status_t EndianOutput::close() {
33     return mOutput->close();
34 }
35 
setEndianness(Endianness end)36 void EndianOutput::setEndianness(Endianness end) {
37     mEndian = end;
38 }
39 
getCurrentOffset() const40 uint32_t EndianOutput::getCurrentOffset() const {
41     return mOffset;
42 }
43 
getEndianness() const44 Endianness EndianOutput::getEndianness() const {
45     return mEndian;
46 }
47 
write(const uint8_t * buf,size_t offset,size_t count)48 status_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 
write(const int8_t * buf,size_t offset,size_t count)56 status_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_) \
61 status_t EndianOutput::write(const _type_* buf, size_t offset, size_t count) { \
62     return writeHelper<_type_>(buf, offset, count); \
63 }
64 
65 DEFINE_WRITE(uint16_t)
DEFINE_WRITE(int16_t)66 DEFINE_WRITE(int16_t)
67 DEFINE_WRITE(uint32_t)
68 DEFINE_WRITE(int32_t)
69 DEFINE_WRITE(uint64_t)
70 DEFINE_WRITE(int64_t)
71 
72 status_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 
write(const double * buf,size_t offset,size_t count)77 status_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