void ResizeImagePeriodicMirror(const cv::Mat& src_img, const int h_off, const int w_off, cv::Mat* dst_img) { const int h_src = src_img.rows; const int w_src = src_img.cols; const int h_dst = dst_img->rows; const int w_dst = dst_img->cols; const int num_channels = src_img.channels(); CHECK_EQ(num_channels, dst_img->channels()) << "number of channels of source and destimation images have to be equal"; CHECK_NE(dst_img, &src_img) << "doesn't support in place calculation"; for (int h = 0; h < h_dst; ++h) { uchar* ptr_dst = dst_img->ptr<uchar>(h); const uchar* ptr_src = src_img.ptr<uchar>( (floor_div(h-h_off, h_src))%2 == 0 ? positive_mod(h-h_off, h_src) : h_src-1-positive_mod(h-h_off, h_src)); int index_dst = 0; for (int w = 0; w < w_dst; ++w) { int index_src = positive_mod(w-w_off, w_src); index_src = (floor_div(w-w_off, w_src))%2 == 0 ? index_src : w_src-1-index_src; index_src *= num_channels; for (int c = 0; c < num_channels; ++c) { ptr_dst[index_dst++] = ptr_src[index_src++]; } } } }
// method 5: very similar to method 4 // Swap nums[n - 2*k,...,n - k - 1] with nums[n - k,...,n - 1]. void rotate(vector<int> &nums, int k){ int n = nums.size(); for(; k = k % n; n -= k){ // nums[n - 2*k,...,n - k - 1] are in their correct positions now. // Need to rotate the elements of nums[0,...,n - k - 1] to the right // by k % n steps for(int i = 0; i < k; ++i){ int left = positive_mod(n - 2*k + i, n); int right = positive_mod(n - k + i, n); swap(nums[left], nums[right]); } } }
// day_of_week - // Returns the day of week. // Taken from PHP source code. static int day_of_week ( int year, int month, int day, int iso ) { int c1, y1, m1, dow ; c1 = century_value ( year / 100 ) ; y1 = positive_mod ( year, 100 ) ; m1 = IS_LEAP ( year ) ? leap_year_table [ month ] : nonleap_year_table [ month ] ; dow = positive_mod ( ( c1 + y1 + m1 + ( y1 / 4 ) + day ), 7) ; if ( iso ) { if ( ! dow ) dow = 7 ; } return ( dow ) ; }
void ResizeImagePeriodic(const cv::Mat& src_img, const int h_off, const int w_off, cv::Mat* dst_img) { const int h_src = src_img.rows; const int w_src = src_img.cols; const int h_dst = dst_img->rows; const int w_dst = dst_img->cols; const int num_channels = src_img.channels(); CHECK_EQ(num_channels, dst_img->channels()) << "number of channels of source and destimation images have to be equal"; CHECK_NE(dst_img, &src_img) << "doesn't support in place calculation"; const int cvwidth_src = w_src * num_channels; for (int h = 0; h < h_dst; ++h) { uchar* ptr_dst = dst_img->ptr<uchar>(h); const uchar* ptr_src = src_img.ptr<uchar>(positive_mod(h-h_off, h_src)); int index_dst = 0; int index_src = positive_mod(-num_channels*w_off, cvwidth_src); for (int w = 0; w < w_dst; ++w) { for (int c = 0; c < num_channels; ++c) { ptr_dst[index_dst++] = ptr_src[positive_mod(index_src++, cvwidth_src)]; } } } }
static int century_value ( century ) { return ( 6 - ( positive_mod ( century, 4 ) * 2 ) ) ; }
void* SSRVideoStreamWriter::NewFrame(unsigned int* flags) { // increment the frame counter GLInjectHeader *header = GetGLInjectHeader(); ++header->frame_counter; std::atomic_thread_fence(std::memory_order_release); // get capture parameters std::atomic_thread_fence(std::memory_order_acquire); *flags = header->capture_flags; if(!(*flags & GLINJECT_FLAG_CAPTURE_ENABLED)) return NULL; // check the timestamp and maybe limit the fps unsigned int target_fps = header->capture_target_fps; int64_t timestamp = hrt_time_micro(); if(target_fps > 0) { int64_t interval = 1000000 / target_fps; if(*flags & GLINJECT_FLAG_LIMIT_FPS) { if(timestamp < m_next_frame_time) { usleep(m_next_frame_time - timestamp); timestamp = hrt_time_micro(); } } else { if(timestamp < m_next_frame_time - interval) return NULL; } m_next_frame_time = std::max(m_next_frame_time + interval, timestamp); } // make sure that at least one frame is available unsigned int read_pos = header->ring_buffer_read_pos; unsigned int write_pos = header->ring_buffer_write_pos; unsigned int frames_used = positive_mod((int) write_pos - (int) read_pos, GLINJECT_RING_BUFFER_SIZE * 2); if(frames_used >= GLINJECT_RING_BUFFER_SIZE) return NULL; // write frame info GLInjectFrameInfo *frameinfo = GetGLInjectFrameInfo(write_pos % GLINJECT_RING_BUFFER_SIZE); frameinfo->timestamp = timestamp; frameinfo->width = m_width; frameinfo->height = m_height; frameinfo->stride = m_stride; // prepare the frame file FrameData &fd = m_frame_data[write_pos % GLINJECT_RING_BUFFER_SIZE]; size_t required_size = (size_t) abs(m_stride) * (size_t) m_height; if(required_size > fd.m_mmap_size_frame) { // calculate new size required_size = (required_size + required_size / 4 + m_page_size - 1) / m_page_size * m_page_size; // unmap frame file if(fd.m_mmap_ptr_frame != MAP_FAILED) { munmap(fd.m_mmap_ptr_frame, fd.m_mmap_size_frame); fd.m_mmap_ptr_frame = MAP_FAILED; fd.m_mmap_size_frame = 0; } // resize frame file if(ftruncate(fd.m_fd_frame, required_size) == -1) { GLINJECT_PRINT("Error: Can't resize video frame file!"); throw SSRStreamException(); } // map frame file fd.m_mmap_ptr_frame = mmap(NULL, required_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd.m_fd_frame, 0); if(fd.m_mmap_ptr_frame == MAP_FAILED) { GLINJECT_PRINT("Error: Can't memory-map video frame file!"); throw SSRStreamException(); } fd.m_mmap_size_frame = required_size; } return fd.m_mmap_ptr_frame; }