1 /*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bmp_stream.h"
17
18 #include "image_log.h"
19
20 #undef LOG_DOMAIN
21 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
22
23 #undef LOG_TAG
24 #define LOG_TAG "BmpStream"
25
26 namespace OHOS {
27 namespace ImagePlugin {
BmpStream(InputDataStream * stream)28 BmpStream::BmpStream(InputDataStream *stream) : inputStream_(stream) {}
29
read(void * buffer,size_t size)30 size_t BmpStream::read(void *buffer, size_t size)
31 {
32 if (inputStream_ == nullptr) {
33 IMAGE_LOGE("read failed, inputStream_ is null");
34 return 0;
35 }
36 if (buffer == nullptr) {
37 size_t curPosition = static_cast<size_t>(inputStream_->Tell());
38 if (!inputStream_->Seek(curPosition + size)) {
39 IMAGE_LOGE("read failed, curpositon=%{public}zu, skip size=%{public}zu", curPosition, size);
40 return 0;
41 }
42 return size;
43 }
44 uint32_t desireSize = static_cast<uint32_t>(size);
45 uint32_t bufferSize = desireSize;
46 uint32_t readSize = desireSize;
47 if (!inputStream_->Read(desireSize, static_cast<uint8_t *>(buffer), bufferSize, readSize)) {
48 IMAGE_LOGE("read failed, desire read size=%{public}u", desireSize);
49 return 0;
50 }
51 return readSize;
52 }
53
peek(void * buffer,size_t size) const54 size_t BmpStream::peek(void *buffer, size_t size) const
55 {
56 if (inputStream_ == nullptr) {
57 IMAGE_LOGE("peek failed, inputStream_ is null");
58 return 0;
59 }
60 if (buffer == nullptr) {
61 IMAGE_LOGE("peek failed, output buffer is null");
62 return 0;
63 }
64 uint32_t desireSize = static_cast<uint32_t>(size);
65 uint32_t bufferSize = desireSize;
66 uint32_t readSize = desireSize;
67 if (!inputStream_->Peek(desireSize, static_cast<uint8_t *>(buffer), bufferSize, readSize)) {
68 IMAGE_LOGE("peek failed, desire peek size=%{public}u", desireSize);
69 return 0;
70 }
71 return readSize;
72 }
73
isAtEnd() const74 bool BmpStream::isAtEnd() const
75 {
76 if (inputStream_ == nullptr) {
77 IMAGE_LOGE("get stream status failed, inputStream_ is null.");
78 return false;
79 }
80 size_t size = inputStream_->GetStreamSize();
81 return (inputStream_->Tell() == size);
82 }
83 } // namespace ImagePlugin
84 } // namespace OHOS
85