|
| 1 | +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 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 "himax_driver_HM01B0.h" |
| 17 | +#include "am_bsp.h" //NOLINT |
| 18 | +#include "am_mcu_apollo.h" //NOLINT |
| 19 | +#include "hm01b0_platform.h" // TARGET specific implementation |
| 20 | + |
| 21 | +// Image is down-sampled by applying a stride of 2 pixels in both the x and y |
| 22 | +// directions. |
| 23 | +static const int kStrideShift = 1; |
| 24 | + |
| 25 | +//***************************************************************************** |
| 26 | +// |
| 27 | +//! @brief Read one frame of data from HM01B0 scaled to 96x96 RGB. |
| 28 | +//! |
| 29 | +//! @param buffer - Pointer to the frame buffer. |
| 30 | +//! @param w - Image width. |
| 31 | +//! @param h - Image height. |
| 32 | +//! @param channels - Number of channels per pixel. |
| 33 | +//! |
| 34 | +//! This function reads data of one frame from HM01B0. It trims the image to an |
| 35 | +//! even power of two mulitple of the requested width and height. It down |
| 36 | +//! samples the original image and duplicates the greyscale value for each color |
| 37 | +//! channel. |
| 38 | +//! |
| 39 | +//! @return Error code. |
| 40 | +// |
| 41 | +//***************************************************************************** |
| 42 | +uint32_t hm01b0_blocking_read_oneframe_scaled( |
| 43 | + hm01b0_cfg_t* psCfg, uint8_t* buffer, int w, int h, int channels) { |
| 44 | + hm01b0_single_frame_capture(psCfg); |
| 45 | + |
| 46 | + // Calculate the number of pixels to crop to get a centered image. |
| 47 | + const int offset_x = (HM01B0_PIXEL_X_NUM - (w * (1 << kStrideShift))) / 2; |
| 48 | + const int offset_y = (HM01B0_PIXEL_Y_NUM - (h * (1 << kStrideShift))) / 2; |
| 49 | + |
| 50 | + uint32_t hsync_count = 0; |
| 51 | + |
| 52 | + while ((hsync_count < HM01B0_PIXEL_Y_NUM)) { |
| 53 | + // Wait for horizontal sync. |
| 54 | + while (!read_hsync()); |
| 55 | + |
| 56 | + // Get resulting image position. When hsync_count < offset_y, this will |
| 57 | + // underflow resulting in an index out of bounds which we check later, |
| 58 | + // avoiding an unnecessary conditional. |
| 59 | + const uint32_t output_y = (hsync_count - offset_y) >> kStrideShift; |
| 60 | + uint32_t rowidx = 0; |
| 61 | + |
| 62 | + // Read one row. Hsync is held high for the duration of a row read. |
| 63 | + while (read_hsync()) { |
| 64 | + // Wait for pixel value to be ready. |
| 65 | + while (!read_pclk()); |
| 66 | + |
| 67 | + // Read 8-bit value from camera. |
| 68 | + const uint8_t value = read_byte(); |
| 69 | + const uint32_t output_x = (rowidx++ - offset_x) >> kStrideShift; |
| 70 | + if (output_x < w && output_y < h) { |
| 71 | + const int output_idx = (output_y * w + output_x) * channels; |
| 72 | + for (int i=0; i<channels; i++) { |
| 73 | + buffer[output_idx + i] = value; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // Wait for next pixel clock. |
| 78 | + while (read_pclk()); |
| 79 | + } |
| 80 | + |
| 81 | + hsync_count++; |
| 82 | + } |
| 83 | + return HM01B0_ERR_OK; |
| 84 | +} |
0 commit comments