/********************************************************************* * Software License Agreement (BSD License) * * Copyright (C) 2010-2012 Ken Tossell * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the author nor other contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. *********************************************************************/ /** * @defgroup frame Frame processing * @brief Tools for managing frame buffers and converting between image formats */ #include "libuvc.h" #include "libuvc_internal.h" /** @internal */ uvc_error_t uvc_ensure_frame_size(uvc_frame_t *frame, size_t need_bytes) { if (frame->library_owns_data) { if (!frame->data || frame->data_bytes != need_bytes) { frame->data_bytes = need_bytes; frame->data = realloc(frame->data, frame->data_bytes); } if (!frame->data) return UVC_ERROR_NO_MEM; return UVC_SUCCESS; } else { if (!frame->data || frame->data_bytes < need_bytes) return UVC_ERROR_NO_MEM; return UVC_SUCCESS; } } /** @brief Allocate a frame structure * @ingroup frame * * @param data_bytes Number of bytes to allocate, or zero * @return New frame, or NULL on error */ uvc_frame_t *uvc_allocate_frame(size_t data_bytes) { uvc_frame_t *frame = malloc(sizeof(*frame)); if (!frame) return NULL; memset(frame, 0, sizeof(*frame)); frame->library_owns_data = 1; if (data_bytes > 0) { frame->data_bytes = data_bytes; frame->data = malloc(data_bytes); if (!frame->data) { free(frame); return NULL; } } return frame; } /** @brief Free a frame structure * @ingroup frame * * @param frame Frame to destroy */ void uvc_free_frame(uvc_frame_t *frame) { if (frame->data_bytes > 0 && frame->library_owns_data) free(frame->data); free(frame); } static inline unsigned char sat(int i) { return (unsigned char)( i >= 255 ? 255 : (i < 0 ? 0 : i)); } /** @brief Duplicate a frame, preserving color format * @ingroup frame * * @param in Original frame * @param out Duplicate frame */ uvc_error_t uvc_duplicate_frame(uvc_frame_t *in, uvc_frame_t *out) { if (uvc_ensure_frame_size(out, in->data_bytes) < 0) return UVC_ERROR_NO_MEM; out->width = in->width; out->height = in->height; out->fourcc = in->fourcc; out->step = in->step; out->sequence = in->sequence; out->capture_time = in->capture_time; out->source = in->source; memcpy(out->data, in->data, in->data_bytes); return UVC_SUCCESS; }