00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (C) 2010-2012 Ken Tossell 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the author nor other contributors may be 00018 * used to endorse or promote products derived from this software 00019 * without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00038 #include "libuvc.h" 00039 #include "libuvc_internal.h" 00040 00042 uvc_error_t uvc_ensure_frame_size(uvc_frame_t *frame, size_t need_bytes) { 00043 if (frame->library_owns_data) { 00044 if (!frame->data || frame->data_bytes != need_bytes) { 00045 frame->data_bytes = need_bytes; 00046 frame->data = realloc(frame->data, frame->data_bytes); 00047 } 00048 if (!frame->data) 00049 return UVC_ERROR_NO_MEM; 00050 return UVC_SUCCESS; 00051 } else { 00052 if (!frame->data || frame->data_bytes < need_bytes) 00053 return UVC_ERROR_NO_MEM; 00054 return UVC_SUCCESS; 00055 } 00056 } 00057 00064 uvc_frame_t *uvc_allocate_frame(size_t data_bytes) { 00065 uvc_frame_t *frame = malloc(sizeof(*frame)); 00066 00067 if (!frame) 00068 return NULL; 00069 00070 memset(frame, 0, sizeof(*frame)); 00071 00072 frame->library_owns_data = 1; 00073 00074 if (data_bytes > 0) { 00075 frame->data_bytes = data_bytes; 00076 frame->data = malloc(data_bytes); 00077 00078 if (!frame->data) { 00079 free(frame); 00080 return NULL; 00081 } 00082 } 00083 00084 return frame; 00085 } 00086 00092 void uvc_free_frame(uvc_frame_t *frame) { 00093 if (frame->data_bytes > 0 && frame->library_owns_data) 00094 free(frame->data); 00095 00096 free(frame); 00097 } 00098 00099 static inline unsigned char sat(int i) { 00100 return (unsigned char)( i >= 255 ? 255 : (i < 0 ? 0 : i)); 00101 } 00102 00109 uvc_error_t uvc_duplicate_frame(uvc_frame_t *in, uvc_frame_t *out) { 00110 if (uvc_ensure_frame_size(out, in->data_bytes) < 0) 00111 return UVC_ERROR_NO_MEM; 00112 00113 out->width = in->width; 00114 out->height = in->height; 00115 out->fourcc = in->fourcc; 00116 out->step = in->step; 00117 out->sequence = in->sequence; 00118 out->capture_time = in->capture_time; 00119 out->source = in->source; 00120 00121 memcpy(out->data, in->data, in->data_bytes); 00122 00123 return UVC_SUCCESS; 00124 }