1 /*
2 * image_file_handle.cpp - Image file handle
3 *
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 * Author: Yinhang Liu <yinhangx.liu@intel.com>
20 */
21
22 #include "image_file_handle.h"
23
24 namespace XCam {
25
ImageFileHandle()26 ImageFileHandle::ImageFileHandle ()
27 {
28 }
29
ImageFileHandle(const char * name,const char * option)30 ImageFileHandle::ImageFileHandle (const char *name, const char *option)
31 : FileHandle (name, option)
32 {
33 }
34
~ImageFileHandle()35 ImageFileHandle::~ImageFileHandle ()
36 {
37 close ();
38 }
39
40 XCamReturn
read_buf(const SmartPtr<VideoBuffer> & buf)41 ImageFileHandle::read_buf (const SmartPtr<VideoBuffer> &buf)
42 {
43 const VideoBufferInfo info = buf->get_video_info ();
44 VideoBufferPlanarInfo planar;
45 uint8_t *memory = NULL;
46 XCamReturn ret = XCAM_RETURN_NO_ERROR;
47
48 XCAM_ASSERT (is_valid ());
49
50 memory = buf->map ();
51 for (uint32_t index = 0; index < info.components; index++) {
52 info.get_planar_info (planar, index);
53 uint32_t line_bytes = planar.width * planar.pixel_bytes;
54
55 for (uint32_t i = 0; i < planar.height; i++) {
56 if (fread (memory + info.offsets [index] + i * info.strides [index], 1, line_bytes, _fp) != line_bytes) {
57 if (end_of_file ())
58 ret = XCAM_RETURN_BYPASS;
59 else {
60 XCAM_LOG_ERROR ("read file failed, size doesn't match");
61 ret = XCAM_RETURN_ERROR_FILE;
62 }
63 }
64 }
65 }
66 buf->unmap ();
67 return ret;
68 }
69
70 XCamReturn
write_buf(const SmartPtr<VideoBuffer> & buf)71 ImageFileHandle::write_buf (const SmartPtr<VideoBuffer> &buf)
72 {
73 const VideoBufferInfo info = buf->get_video_info ();
74 VideoBufferPlanarInfo planar;
75 uint8_t *memory = NULL;
76 XCamReturn ret = XCAM_RETURN_NO_ERROR;
77
78 XCAM_ASSERT (is_valid ());
79
80 memory = buf->map ();
81 for (uint32_t index = 0; index < info.components; index++) {
82 info.get_planar_info (planar, index);
83 uint32_t line_bytes = planar.width * planar.pixel_bytes;
84
85 for (uint32_t i = 0; i < planar.height; i++) {
86 if (fwrite (memory + info.offsets [index] + i * info.strides [index], 1, line_bytes, _fp) != line_bytes) {
87 XCAM_LOG_ERROR ("write file failed, size doesn't match");
88 ret = XCAM_RETURN_ERROR_FILE;
89 }
90 }
91 }
92 buf->unmap ();
93 return ret;
94 }
95
96 }
97