unsigned char * copy_yuv2rgb(ClFrame &in_frame, ClFrame &out_frame) { PERF_START("process_yuv"); unsigned char *tmp_img = out_frame.get(); char *current = (char *)in_frame.get(); int x, y; y = 0; while(y < m_height) { x = 0; while(x < m_width) { //char Y = current[0]; //char U = current[1]; //char Y2 = current[2]; //char V = current[3]; yuv2rgb(tmp_img, 0, current[0], 128, 16, current[1], current[3]); yuv2rgb(tmp_img, 4, current[2], 128, 16, current[1], current[3]); tmp_img = tmp_img + 8; current = current + 4; x = x + 2; } current = ((char *)in_frame.get()) + y * m_width * 2; y++; } PERF_END("process_yuv"); }
static void update_image_pixels_by_addr(const void *start) { unsigned char *data = (unsigned char *)start; unsigned char *pixels = image.pixels; int width = opt.width; /* width, height应该归于video buffer结构体管理 */ int height = opt.height; unsigned char Y, Cr, Cb; int r, g, b; int x, y; int p1, p2, p3, p4; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { p1 = y * width * 2 + x * 2; Y = data[p1]; if (x % 2 == 0) { p2 = y * width * 2 + (x * 2 + 1); p3 = y * width * 2 + (x * 2 + 3); } else { p2 = y * width * 2 + (x * 2 - 1); p3 = y * width * 2 + (x * 2 + 1); } Cb = data[p2]; Cr = data[p3]; yuv2rgb(Y, Cb, Cr, &r, &g, &b); p4 = y * width * 4 + x * 4; pixels[p4] = b; pixels[p4 + 1] = g; pixels[p4 + 2] = r; pixels[p4 + 3] = 255; } } }
void generate_colors(int static_component,double dist_treshold){ yuv c; double dist; randomize_yuv(-1,&c); for(int i=0;i<num_colors;i++){ // attempt to create a specific contrast between the colors not sure how well it works if(!i ){ randomize_yuv(static_component,&c); }else{ randomize_yuv(static_component,&c); int cnt=0; while(dist_treshold > (dist=min_dist(c,i))){ assert(++cnt < STUCK_TRESHOLD); randomize_yuv(static_component,&c); } } yuv_colors[i].y=c.y; yuv_colors[i].u=c.u; yuv_colors[i].v=c.v; } // convert the colors to rgb for(int i=0;i<num_colors;i++){ yuv2rgb(&colors[i],yuv_colors[i]); } }
void LUT3D::set_yuv(int y, int u, int v, Color c) { if(c >= MAX_COLORS) { printf("Error: color out of bounds yuv!\n"); return; } int b, g, r; yuv2rgb(y, u, v, r, g, b); LOOKUP_BGR(b,g,r) = c; }
rgb get_color(int n,int bright){ assert(color_initialized); assert(n >= 0 && n < num_colors); rgb c; yuv c1; c1=yuv_colors[n]; if(bright) c1.y+=0.5; yuv2rgb(&c,c1); return c; }
void inputimgyuv(char *fname,unsigned char *img,int ny,int nx) { FILE *fimg; int iy,ix,i,j,imagesize,loc; unsigned char *tmp; imagesize = ny*nx; tmp=(unsigned char *)malloc(imagesize*sizeof(unsigned char)); fimg=fopen(fname,"rb"); if (!fimg) { printf("unable to read %s\n",fname); exit(-1); } fread(tmp, sizeof(unsigned char), imagesize, fimg); for (i=0,j=0;i<imagesize;i++,j+=3) img[j] = tmp[i]; fread(tmp, sizeof(unsigned char), imagesize/4, fimg); i = 0; for (iy=0;iy<ny;iy+=2) { for (ix=0;ix<nx;ix+=2) { loc = LOC(iy,ix,1,nx,3); img[loc] = tmp[i]; img[loc+3] = tmp[i]; loc += 3*nx; img[loc] = tmp[i]; img[loc+3] = tmp[i]; i++; } } fread(tmp, sizeof(unsigned char), imagesize/4, fimg); i=0; for (iy=0;iy<ny;iy+=2) { for (ix=0;ix<nx;ix+=2) { loc = LOC(iy,ix,2,nx,3); img[loc] = tmp[i]; img[loc+3] = tmp[i]; loc += 3*nx; img[loc] = tmp[i]; img[loc+3] = tmp[i]; i++; } } free(tmp); fclose (fimg); yuv2rgb(img,ny*nx*3); }
static void yuyv_to_rgb(rgb_ptr buffer_yuyv, rgb_ptr buffer_rgb, int width, int height) { rgb_ptr pixel_16; // for YUYV rgb_ptr pixel_24; // for RGB int y0, u0, v0, y1; char r, g, b; if ( buffer_yuyv == NULL || buffer_rgb == NULL) return; pixel_16 = (rgb_ptr)buffer_yuyv;//width * height * 2 pixel_24 = buffer_rgb;//width * height * 3 int i = 0, j = 0; while ((i + BPP_YUY2) < height * width * BPP_YUY2) { y0 = pixel_16[i]; u0 = pixel_16[i+1]; y1 = pixel_16[i+2]; v0 = pixel_16[i+3]; yuv2rgb(y0, u0, v0, &r, &g, &b); // 1st pixel pixel_24[j] = r; pixel_24[j + 1] = g; pixel_24[j + 2] = b; j+=BPP_RGB24; yuv2rgb(y1, u0, v0, &r, &g, &b);// 2nd pixel pixel_24[j] = r; pixel_24[j + 1] = g; pixel_24[j + 2] = b; j+=BPP_RGB24; i += BPP_YUY2_PIXEL; } }
static PyObject *py_yuv2rgb(PyObject *self, PyObject *args,PyObject *kwds) { pyimgObject *yuv, *rgb=NULL; int downby2 = 0; static char *kwlist[] = {"yuvimg", "rgbimg", "downby2", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|Oi", kwlist, &yuv, &rgb, &downby2)) { PyErr_SetString(PyExc_Exception, "Bad arguments."); return NULL; } if(!rgb) { rgb = (pyimgObject *) pyimgType.tp_alloc(&pyimgType, 0); rgb->img.width = yuv->img.width; rgb->img.height = yuv->img.height; rgb->img.code = V4L2_PIX_FMT_RGB24; if(downby2) { rgb->img.width /= 2; rgb->img.height /= 2; } __pyimg_alloc(rgb); } yuv2rgb(&(yuv->img), (unsigned char*) rgb->img.start, downby2); return (PyObject *) rgb; }
void run_cpu_color_test(PPM_IMG img_in) { StopWatchInterface *timer=NULL; printf("Starting CPU processing...\n"); sdkCreateTimer(&timer); sdkStartTimer(&timer); img_obuf_yuv_cpu = rgb2yuv(img_in); //Start RGB 2 YUV sdkStopTimer(&timer); printf("RGB to YUV conversion time: %f (ms)\n", sdkGetTimerValue(&timer)); sdkDeleteTimer(&timer); sdkCreateTimer(&timer); sdkStartTimer(&timer); img_obuf_rgb_cpu = yuv2rgb(img_obuf_yuv_cpu); //Start YUV 2 RGB sdkStopTimer(&timer); printf("YUV to RGB conversion time: %f (ms)\n", sdkGetTimerValue(&timer)); sdkDeleteTimer(&timer); write_yuv(img_obuf_yuv_cpu, "out_yuv.yuv"); write_ppm(img_obuf_rgb_cpu, "out_rgb.ppm"); }
void VCDSubDumpPNG( uint8_t *p_image, decoder_t *p_dec, uint32_t i_height, uint32_t i_width, const char *filename, png_text *text_ptr, int i_text_count ) { decoder_sys_t *p_sys = p_dec->p_sys; uint8_t *p = p_image; uint8_t *image_data = malloc(RGB_SIZE * i_height * i_width ); uint8_t *q = image_data; unsigned int i_row; /* scanline row number */ unsigned int i_column; /* scanline column number */ uint8_t rgb_palette[PALETTE_SIZE * RGB_SIZE]; int i; dbg_print( (DECODE_DBG_CALL), "%s", filename); if (NULL == image_data) return; /* Convert palette YUV into RGB. */ for (i=0; i<PALETTE_SIZE; i++) { ogt_yuvt_t *p_yuv = &(p_sys->p_palette[i]); uint8_t *p_rgb_out = &(rgb_palette[i*RGB_SIZE]); yuv2rgb( p_yuv, p_rgb_out ); } /* Convert palette entries into linear RGB array. */ for ( i_row=0; i_row < i_height; i_row ++ ) { for ( i_column=0; i_column<i_width; i_column++ ) { uint8_t *p_rgb = &rgb_palette[ ((*p)&PALETTE_SIZE_MASK)*RGB_SIZE ]; *q++ = p_rgb[0]; *q++ = p_rgb[1]; *q++ = p_rgb[2]; p++; } } write_png( filename, i_height, i_width, image_data, text_ptr, i_text_count ); free(image_data); }
static void update_rgb_pixels (const void *start) { unsigned char *data = (unsigned char *) start; unsigned char *pixels = screen.rgb.pixels; int width = screen.rgb.width; int height = screen.rgb.height; unsigned char Y, Cr, Cb; int r, g, b; int x, y; int p1, p2, p3, p4; for (y = 0; y < height; y++) { for (x = 0; x < width; x++) { p1 = y * width * 2 + x * 2; Y = data[p1]; if (x % 2 == 0) { p2 = y * width * 2 + (x * 2 + 1); p3 = y * width * 2 + (x * 2 + 3); } else { p2 = y * width * 2 + (x * 2 - 1); p3 = y * width * 2 + (x * 2 + 1); } Cb = data[p2]; Cr = data[p3]; yuv2rgb (Y, Cb, Cr, &r, &g, &b); p4 = y * width * 4 + x * 4; pixels[p4] = r; pixels[p4 + 1] = g; pixels[p4 + 2] = b; pixels[p4 + 3] = 255; } } }
int main(int argc, char * argv[]) { char readpath[BUFSZ]; char writepath[BUFSZ]; strcpy(readpath, argv[1]); strcpy(writepath, argv[2]); /* read YUV pixel pic to mem */ readyuv(readpath); /* convert YUV to RGB */ printf("converting %s to %s \n",readpath,writepath); yuv2rgb(pBmpBuf); printf("DONE\n"); /* data persistence */ writergb(writepath,bmpBuf); return 0; }
void huc6261_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr) { int vpos = m_screen->vpos(); int hpos = m_screen->hpos(); int h = m_last_h; int v = m_last_v; UINT32 *bitmap_line = &m_bmp->pix32(v); while ( h != hpos || v != vpos ) { if ( m_pixel_clock == 0 ) { g_profiler.start( PROFILER_VIDEO ); /* Get next pixel information */ m_pixel_data = m_huc6270_b->next_pixel( machine().driver_data()->generic_space(), 0, 0xffff ); g_profiler.stop(); } bitmap_line[ h ] = yuv2rgb( ( ( m_palette[m_pixel_data] & 0xff00 ) << 8 ) | ( ( m_palette[m_pixel_data] & 0xf0 ) << 8 ) | ( ( m_palette[m_pixel_data] & 0x0f ) << 4 ) ); m_pixel_clock = ( m_pixel_clock + 1 ) % m_pixels_per_clock; h = ( h + 1 ) % HUC6261_WPF; switch( h ) { case HUC6261_HSYNC_START: /* Start of HSync */ m_huc6270_a->hsync_changed( 0 ); m_huc6270_b->hsync_changed( 0 ); // if ( v == 0 ) // { // /* Check if the screen should be resized */ // m_height = HUC6261_LPF - ( m_blur ? 1 : 0 ); // if ( m_height != video_screen_get_height( m_screen ) ) // { // rectangle visible_area; // // /* TODO: Set proper visible area parameters */ // visible_area.min_x = 64; // visible_area.min_y = 18; // visible_area.max_x = 64 + 1024 + 64 - 1; // visible_area.max_y = 18 + 242 - 1; // // video_screen_configure( m_screen, HUC6261_WPF, m_height, &visible_area, HZ_TO_ATTOSECONDS( device->clock / ( HUC6261_WPF * m_height ) ) ); // } // } break; case 0: /* End of HSync */ m_huc6270_a->hsync_changed( 1 ); m_huc6270_b->hsync_changed( 1 ); m_pixel_clock = 0; v = ( v + 1 ) % m_height; bitmap_line = &m_bmp->pix32(v); break; case HUC6261_HSYNC_START + 30: /* End/Start of VSync */ if ( v>= m_height - 4 ) { int vsync = ( v >= m_height - 4 && v < m_height - 1 ) ? 0 : 1; m_huc6270_a->vsync_changed( vsync ); m_huc6270_b->vsync_changed( vsync ); } break; } } m_last_h = h; m_last_v = v; /* Reschedule timer */ if ( m_last_h < HUC6261_HSYNC_START ) { /* Next event is start of HSync signal */ v = m_last_v; h = HUC6261_HSYNC_START; } else if ( ( m_last_v == m_height - 4 || m_last_v == m_height - 1 ) && m_last_h < HUC6261_HSYNC_START + 30 ) { /* Next event is start/end of VSync signal */ v = m_last_v; h = HUC6261_HSYNC_START + 30; } else { /* Next event is end of HSync signal */ v = ( m_last_v + 1 ) % m_height; h = 0; } /* Ask our slave device for time until next possible event */ { UINT16 next_event_clocks = HUC6261_WPF; //m_get_time_til_next_event( 0, 0xffff ); int event_hpos, event_vpos; /* Adjust for pixel clocks per pixel */ next_event_clocks *= m_pixels_per_clock; /* Adjust for clocks left to go for current pixel */ next_event_clocks += ( m_pixels_per_clock - ( m_pixel_clock + 1 ) ); event_hpos = hpos + next_event_clocks; event_vpos = vpos; while ( event_hpos > HUC6261_WPF ) { event_vpos += 1; event_hpos -= HUC6261_WPF; } if ( event_vpos < v || ( event_vpos == v && event_hpos <= h ) ) { if ( event_vpos > vpos || ( event_vpos == vpos && event_hpos > hpos ) ) { v = event_vpos; h = event_hpos; } } } m_timer->adjust( m_screen->time_until_pos( v, h ) ); }
int cDvbSubtitleConverter::ExtractSegment(const uchar *Data, int Length, int64_t Pts) { cBitStream bs(Data, Length * 8); if (Length > 5 && bs.GetBits(8) == 0x0F) { // sync byte int segmentType = bs.GetBits(8); if (segmentType == STUFFING_SEGMENT) return -1; int pageId = bs.GetBits(16); int segmentLength = bs.GetBits(16); if (!bs.SetLength(bs.Index() + segmentLength * 8)) return -1; cDvbSubtitlePage *page = NULL; LOCK_THREAD; for (cDvbSubtitlePage *sp = pages->First(); sp; sp = pages->Next(sp)) { if (sp->PageId() == pageId) { page = sp; break; } } if (!page) { page = new cDvbSubtitlePage(pageId); pages->Add(page); dbgpages("Create SubtitlePage %d (total pages = %d)\n", pageId, pages->Count()); } if (Pts) page->SetPts(Pts); switch (segmentType) { case PAGE_COMPOSITION_SEGMENT: { dbgsegments("PAGE_COMPOSITION_SEGMENT\n"); int pageTimeout = bs.GetBits(8); int pageVersion = bs.GetBits(4); if (pageVersion == page->Version()) break; // no update page->SetVersion(pageVersion); page->SetTimeout(pageTimeout); page->SetState(bs.GetBits(2)); bs.SkipBits(2); // reserved dbgpages("Update page id %d version %d pts %"PRId64" timeout %d state %d\n", pageId, page->Version(), page->Pts(), page->Timeout(), page->State()); while (!bs.IsEOF()) { cSubtitleRegion *region = page->GetRegionById(bs.GetBits(8), true); bs.SkipBits(8); // reserved region->SetHorizontalAddress(bs.GetBits(16)); region->SetVerticalAddress(bs.GetBits(16)); } break; } case REGION_COMPOSITION_SEGMENT: { dbgsegments("REGION_COMPOSITION_SEGMENT\n"); cSubtitleRegion *region = page->GetRegionById(bs.GetBits(8)); if (!region) break; int regionVersion = bs.GetBits(4); if (regionVersion == region->Version()) break; // no update region->SetVersion(regionVersion); bool regionFillFlag = bs.GetBit(); bs.SkipBits(3); // reserved int regionWidth = bs.GetBits(16); if (regionWidth < 1) regionWidth = 1; int regionHeight = bs.GetBits(16); if (regionHeight < 1) regionHeight = 1; region->SetSize(regionWidth, regionHeight); region->SetLevel(bs.GetBits(3)); region->SetDepth(bs.GetBits(3)); bs.SkipBits(2); // reserved region->SetClutId(bs.GetBits(8)); dbgregions("Region pageId %d id %d version %d fill %d width %d height %d level %d depth %d clutId %d\n", pageId, region->RegionId(), region->Version(), regionFillFlag, regionWidth, regionHeight, region->Level(), region->Depth(), region->ClutId()); int region8bitPixelCode = bs.GetBits(8); int region4bitPixelCode = bs.GetBits(4); int region2bitPixelCode = bs.GetBits(2); bs.SkipBits(2); // reserved if (regionFillFlag) { switch (region->Bpp()) { case 2: region->FillRegion(region8bitPixelCode); break; case 4: region->FillRegion(region4bitPixelCode); break; case 8: region->FillRegion(region2bitPixelCode); break; default: dbgregions("unknown bpp %d (%s %d)\n", region->Bpp(), __FUNCTION__, __LINE__); } } while (!bs.IsEOF()) { cSubtitleObject *object = region->GetObjectById(bs.GetBits(16), true); int objectType = bs.GetBits(2); object->SetCodingMethod(objectType); object->SetProviderFlag(bs.GetBits(2)); int objectHorizontalPosition = bs.GetBits(12); bs.SkipBits(4); // reserved int objectVerticalPosition = bs.GetBits(12); object->SetPosition(objectHorizontalPosition, objectVerticalPosition); if (objectType == 0x01 || objectType == 0x02) { object->SetForegroundPixelCode(bs.GetBits(8)); object->SetBackgroundPixelCode(bs.GetBits(8)); } } break; } case CLUT_DEFINITION_SEGMENT: { dbgsegments("CLUT_DEFINITION_SEGMENT\n"); cSubtitleClut *clut = page->GetClutById(bs.GetBits(8), true); int clutVersion = bs.GetBits(4); if (clutVersion == clut->Version()) break; // no update clut->SetVersion(clutVersion); bs.SkipBits(4); // reserved dbgcluts("Clut pageId %d id %d version %d\n", pageId, clut->ClutId(), clut->Version()); while (!bs.IsEOF()) { uchar clutEntryId = bs.GetBits(8); bool entryClut2Flag = bs.GetBit(); bool entryClut4Flag = bs.GetBit(); bool entryClut8Flag = bs.GetBit(); bs.SkipBits(4); // reserved uchar yval; uchar crval; uchar cbval; uchar tval; if (bs.GetBit()) { // full_range_flag yval = bs.GetBits(8); crval = bs.GetBits(8); cbval = bs.GetBits(8); tval = bs.GetBits(8); } else { yval = bs.GetBits(6) << 2; crval = bs.GetBits(4) << 4; cbval = bs.GetBits(4) << 4; tval = bs.GetBits(2) << 6; } tColor value = 0; if (yval) { value = yuv2rgb(yval, cbval, crval); value |= ((10 - (clutEntryId ? Setup.SubtitleFgTransparency : Setup.SubtitleBgTransparency)) * (255 - tval) / 10) << 24; } dbgcluts("%2d %d %d %d %08X\n", clutEntryId, entryClut2Flag ? 2 : 0, entryClut4Flag ? 4 : 0, entryClut8Flag ? 8 : 0, value); if (entryClut2Flag) clut->SetColor(2, clutEntryId, value); if (entryClut4Flag) clut->SetColor(4, clutEntryId, value); if (entryClut8Flag) clut->SetColor(8, clutEntryId, value); } dbgcluts("\n"); break; } case OBJECT_DATA_SEGMENT: { dbgsegments("OBJECT_DATA_SEGMENT\n"); cSubtitleObject *object = page->GetObjectById(bs.GetBits(16)); if (!object) break; int objectVersion = bs.GetBits(4); if (objectVersion == object->Version()) break; // no update object->SetVersion(objectVersion); int codingMethod = bs.GetBits(2); object->SetNonModifyingColorFlag(bs.GetBit()); bs.SkipBit(); // reserved dbgobjects("Object pageId %d id %d version %d method %d modify %d\n", pageId, object->ObjectId(), object->Version(), object->CodingMethod(), object->NonModifyingColorFlag()); if (codingMethod == 0) { // coding of pixels int topFieldLength = bs.GetBits(16); int bottomFieldLength = bs.GetBits(16); object->DecodeSubBlock(bs.GetData(), topFieldLength, true); if (bottomFieldLength) object->DecodeSubBlock(bs.GetData() + topFieldLength, bottomFieldLength, false); else object->DecodeSubBlock(bs.GetData(), topFieldLength, false); bs.WordAlign(); } else if (codingMethod == 1) { // coded as a string of characters int numberOfCodes = bs.GetBits(8); object->DecodeCharacterString(bs.GetData(), numberOfCodes); } #ifdef FINISHPAGE_HACK FinishPage(page); // flush to OSD right away #endif break; } case DISPLAY_DEFINITION_SEGMENT: { dbgsegments("DISPLAY_DEFINITION_SEGMENT\n"); int version = bs.GetBits(4); if (version != ddsVersionNumber) { bool displayWindowFlag = bs.GetBit(); windowHorizontalOffset = 0; windowVerticalOffset = 0; bs.SkipBits(3); // reserved displayWidth = windowWidth = bs.GetBits(16) + 1; displayHeight = windowHeight = bs.GetBits(16) + 1; if (displayWindowFlag) { windowHorizontalOffset = bs.GetBits(16); // displayWindowHorizontalPositionMinimum windowWidth = bs.GetBits(16) - windowHorizontalOffset + 1; // displayWindowHorizontalPositionMaximum windowVerticalOffset = bs.GetBits(16); // displayWindowVerticalPositionMinimum windowHeight = bs.GetBits(16) - windowVerticalOffset + 1; // displayWindowVerticalPositionMaximum } SetOsdData(); SetupChanged(); ddsVersionNumber = version; } break; } case DISPARITY_SIGNALING_SEGMENT: { dbgsegments("DISPARITY_SIGNALING_SEGMENT\n"); bs.SkipBits(4); // dss_version_number bool disparity_shift_update_sequence_page_flag = bs.GetBit(); bs.SkipBits(3); // reserved bs.SkipBits(8); // page_default_disparity_shift if (disparity_shift_update_sequence_page_flag) { bs.SkipBits(8); // disparity_shift_update_sequence_length bs.SkipBits(24); // interval_duration[23..0] int division_period_count = bs.GetBits(8); for (int i = 0; i < division_period_count; ++i) { bs.SkipBits(8); // interval_count bs.SkipBits(8); // disparity_shift_update_integer_part } } while (!bs.IsEOF()) { bs.SkipBits(8); // region_id bool disparity_shift_update_sequence_region_flag = bs.GetBit(); bs.SkipBits(5); // reserved int number_of_subregions_minus_1 = bs.GetBits(2); for (int i = 0; i <= number_of_subregions_minus_1; ++i) { if (number_of_subregions_minus_1 > 0) { bs.SkipBits(16); // subregion_horizontal_position bs.SkipBits(16); // subregion_width } bs.SkipBits(8); // subregion_disparity_shift_integer_part bs.SkipBits(4); // subregion_disparity_shift_fractional_part bs.SkipBits(4); // reserved if (disparity_shift_update_sequence_region_flag) { bs.SkipBits(8); // disparity_shift_update_sequence_length bs.SkipBits(24); // interval_duration[23..0] int division_period_count = bs.GetBits(8); for (int i = 0; i < division_period_count; ++i) { bs.SkipBits(8); // interval_count bs.SkipBits(8); // disparity_shift_update_integer_part } } } } break; } case END_OF_DISPLAY_SET_SEGMENT: { dbgsegments("END_OF_DISPLAY_SET_SEGMENT\n"); FinishPage(page); break; } default: dbgsegments("*** unknown segment type: %02X\n", segmentType); } return bs.Length() / 8; } return -1; }
static void * decode_and_render(FFmpegInfo *pInfo, int nframe) { if (!pInfo) return NULL; LOGI("lock native window ..."); ANativeWindow_acquire(pInfo->window); /* acquire window's information: width/height/format */ AVPixelFormat pix_fmt; int win_format = ANativeWindow_getFormat(pInfo->window); int win_width = ANativeWindow_getWidth(pInfo->window); int win_height = ANativeWindow_getHeight(pInfo->window); if (win_format == WINDOW_FORMAT_RGBA_8888) pix_fmt = AV_PIX_FMT_RGBA; else if (win_format == WINDOW_FORMAT_RGB_565) pix_fmt = AV_PIX_FMT_RGB565; else { LOGE("unsupport window format"); ANativeWindow_release(pInfo->window); return NULL; } LOGI("alloc decoded buffer w-%d, h-%d, f-%d", win_width, win_height, win_format); if (!pInfo->win_data || pInfo->win_width != win_width || pInfo->win_height != win_height || pInfo->win_format != win_format ) { /* release old data */ if (pInfo->win_data) { av_free(pInfo->win_data[0]); pInfo->win_data[0] = {NULL}; } /* alloc decoded buffer */ int ret = av_image_alloc(pInfo->win_data, pInfo->win_linesize, win_width, win_height, pix_fmt, 1); if (ret < 0) { LOGE("Could not allocate raw video buffer"); ANativeWindow_release(pInfo->window); return NULL; } pInfo->win_bufsize = ret; pInfo->win_format = win_format; pInfo->win_width = win_width; pInfo->win_height = win_height; } /* read video frame and decode */ int num_frames = 0; LOGI("read video frame and decode"); pInfo->video_timestamp = -1; av_seek_frame(pInfo->fmt_ctx, pInfo->video_stream_idx, pInfo->video_timestamp, AVSEEK_FLAG_BACKWARD); pInfo->video_timestamp = 0; while (av_read_frame(pInfo->fmt_ctx, &pInfo->pkt) >= 0) { if (num_frames >= nframe) { break; } AVPacket orig_pkt = pInfo->pkt; /* process video stream */ if (pInfo->pkt.stream_index == pInfo->video_stream_idx) { num_frames ++; AVCodecContext* dec_ctx = pInfo->video_dec_ctx; AVFrame* frame = pInfo->frame; pInfo->video_timestamp = frame->pts; LOGI("pkt.flags = %d, frame.pts=%d", pInfo->pkt.flags, frame->pts); /* decode video */ int got_frame = 0; int ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, &pInfo->pkt); if (ret < 0) { LOGE("Error decoding video frame"); break; } /* decode success */ if (got_frame) { /* copy video data from aligned buffer into unaligned */ av_image_copy(pInfo->video_dst_data, pInfo->video_dst_linesize, (const uint8_t **)(frame->data), frame->linesize, dec_ctx->pix_fmt, dec_ctx->width, dec_ctx->height); /* convert to required format */ int ret = yuv2rgb(dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt, pInfo->video_dst_linesize, pInfo->video_dst_data, pInfo->win_width, pInfo->win_height, pix_fmt, pInfo->win_linesize, pInfo->win_data); /* do paint */ LOGI("do paint on surface, ret=%d, decoded w=%d, h=%d, fmt=from %d to %d", ret, dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt, pix_fmt); struct ANativeWindow_Buffer data; ANativeWindow_lock(pInfo->window, &data, NULL); memcpy(data.bits, pInfo->win_data[0], pInfo->win_bufsize); ANativeWindow_unlockAndPost(pInfo->window); } /* seek to next key frame */ av_seek_frame(pInfo->fmt_ctx, pInfo->video_stream_idx, pInfo->video_timestamp, AVSEEK_FLAG_BACKWARD); } av_free_packet(&orig_pkt); } //av_free_packet(&pkt); ANativeWindow_release(pInfo->window); LOGI("decode OK, number=%d", num_frames); return NULL; }
int main(int argc, char const *argv[]) { FILE*fp,*image; unsigned char** red,**green,**blue,**Y,**U,**V,*image_contents_rgb, *image_contents_yuv, *image_contents_rgbt; int i,j,x,y,row_index, col_index,ERROR; Header1 header1; Header2 header2; /* ----------------------------- Task 1 -------------------------------------- */ int array[8][8] = {{48,39,40,68,60,38,50,121}, {149,82,79,101,113,106,27,62}, {58,63,77,69,124,107,74,125}, {80,97,74,54,59,71,91,66}, {18,34,33,46,64,61,32,37}, {149,108,80,106,116,61,73,92}, {211,233,159,88,107,158,161,109}, {212,104,40,44,71,136,113,66}}; printf("\n\n *************************** Activity 1: SAMPLE BLOCK *************** \n"); dct(array); /*---------------------------- Task2 ----------------------------------------- */ srand(time(NULL)); printf("\n\n *************************** Activity 2: RANDOM BLOCK **************** \n\n"); for(i =0; i<block_size; ++i) { for(j=0; j<block_size; ++j) { array[i][j] = rand()%150; } } dct(array); printf("\n"); /*------------------------------ Task3 ------------------------------------------- */ if(argc!= 2) /* syntax error check*/ { printf("\n The format is ./quantizer [file-name]"); return -1; } if((fp = fopen(argv[1],"rb"))== NULL) /* open the .bmp file for reading*/ { printf("\n Error opening the file specified. Please check if the file exists !!\n"); fclose(fp); return -1; } if(fread(&header1,sizeof(header1),1,fp)!=1) /* Read the primary header from the bmp file */ { printf("\n Error reading header1 \n"); fclose(fp); return -1; } if(header1.type!= 19778) /* check if its a valid bmp file*/ { printf("\n Not a bmp file. Exiting !! \n"); fclose(fp); return -1; } if(fread(&header2,sizeof(header2),1,fp)!=1 ) { printf("\n Error reading header 2"); fclose(fp); return -1; } image_contents_rgb = (unsigned char*)malloc(sizeof(char) * header2.imagesize); image_contents_rgbt = (unsigned char*)malloc(sizeof(char) * header2.imagesize); /*allocate memory to store image data*/ image_contents_yuv = (unsigned char*)malloc(sizeof(char) * header2.imagesize); fseek(fp,header1.offset,SEEK_SET); if((ERROR=fread(image_contents_rgb,header2.imagesize,1,fp))!=1) { printf("\nError reading contents\n"); free(image_contents_rgb); fclose(fp); return -1; } fclose(fp); red = (unsigned char**)malloc(sizeof(char*) * header2.height); green = (unsigned char**)malloc(sizeof(char*) * header2.height); blue = (unsigned char**)malloc(sizeof(char*) * header2.height); Y = (unsigned char**)malloc(sizeof(char*) * header2.height); U = (unsigned char**)malloc(sizeof(char*) * header2.height); V = (unsigned char**)malloc(sizeof(char*) * header2.height); for(i=0 ; i<header2.height ;i++) { red[i] = (unsigned char*)malloc( sizeof(char) * header2.width); green[i]= (unsigned char*)malloc( sizeof(char) * header2.width); blue[i]= (unsigned char*)malloc( sizeof(char) * header2.width); Y[i] = (unsigned char*)malloc( sizeof(char) * header2.width); U[i] = (unsigned char*)malloc( sizeof(char) * header2.width); V[i] = (unsigned char*)malloc( sizeof(char) * header2.width); } vector2matrix(red,green,blue,image_contents_rgb,header2.height,header2.width); /* call to store image contents as matrix */ printf("\n\n *********************** Activity 3: RGB Image Block *********************\n"); printf(" Enter the starting row index and coloumn index respectively: "); scanf("%d %d",&row_index,&col_index); for(i = 0, x = row_index; i < block_size && x<row_index+8; ++i, ++x) { for(j= 0, y = col_index; j < block_size && y<col_index+8; ++j, ++y) { if(x<512 && y<512) array[i][j] = blue[x][y]; else array[i][j] = 0; } } dct(array); rgb2yuv(red,green,blue,Y,U,V,header2.height,header2.width); /* Downsampling*/ printf("\n\n *********************** Activity 4: YUV Image Block *********************\n"); printf(" Enter the starting row index and coloumn index respectively: "); scanf("%d %d",&row_index,&col_index); for(i = 0, x = row_index; i < block_size && x<row_index+8; ++i, ++x) { for(j= 0, y = col_index; j < block_size && y<col_index+8; ++j, ++y) { if(x<512 && y<512) array[i][j] = V[x][y]; else array[i][j] = 0; } } dct(array); yuv2rgb(red,green,blue,Y,U,V,header2.height,header2.width); /* convert back to RGB*/ matrix2vector_rgb(red,green,blue,image_contents_rgbt,header2.height,header2.width); /* convert back from matrix to vector*/ matrix2vector_yuv(Y,U,V,image_contents_yuv,header2.height,header2.width); if((image = fopen("Output_image","wb")) == NULL) { printf("\n ERROR opening the file to write quantized image !!\n"); fclose(image); return -1; } if(fwrite(&header1,sizeof(header1),1,image)!= 1) { printf("\n ERROR writing header 1 into destination file !!\n"); fclose(image); return -1; } if(fwrite(&header2,sizeof(header2),1,image)!= 1) { printf("\n ERROR writing header 2 into destination file !! \n"); fclose(image); return -1; } fseek(image, header1.offset, SEEK_SET); if(fwrite(image_contents_rgbt,header2.imagesize,1,image)!=1) /* Change the vector to write into the bmp file here*/ { printf("\n ERROR writing image contents into destination file \n"); fclose(image); return -1; } free(red); free(green); free(blue); free(Y); free(U); free(V); free(image_contents_rgb); free(image_contents_yuv); fclose(image); return 0; }
/************************************************************************************************************************************ void update_yuv(unsigned char** Y, unsigned char** U, unsigned char** V, int height, int width) { FILE* fp; int outer_r, inner_r, outer_c, inner_c; if((fp = fopen("Down_Sampled_YUV","rb")) == NULL) { printf("\n In update_yuv() Error opening DCT_output file !"); exit(0); } for(outer_r =0; outer_r < height; outer_r += 8) { for(outer_c = 0; outer_c < width; outer_c += 8) { for(inner_r = outer_r; inner_r < outer_r+8; inner_r++) { for(inner_c = outer_c; inner_c < outer_c+8; inner_c++) { fscanf(fp,"%d\t",Y[inner_r][inner_c]); } } } } for(outer_r =0; outer_r < height; outer_r += 8) { for(outer_c = 0; outer_c < width; outer_c += 8) { for(inner_r = outer_r; inner_r < outer_r+8; inner_r++) { for(inner_c = outer_c; inner_c < outer_c+8; inner_c++) { fscanf(fp,"%d\t",U[inner_r][inner_c]); } } } } for(outer_r =0; outer_r < height; outer_r += 8) { for(outer_c = 0; outer_c < width; outer_c += 8) { for(inner_r = outer_r; inner_r < outer_r+8; inner_r++) { for(inner_c = outer_c; inner_c < outer_c+8; inner_c++) { fscanf(fp,"%d\t",V[inner_r][inner_c]); } } } } } ***************************************************************************************************************************************/ int main(int argc, char const *argv[]) { FILE*fp, *image_rgb, *image_yuv, *down_sampled, *DCT, *image; unsigned char** red, **green, **blue, **Y, **U, **V, *image_contents_rgb, *image_contents_yuv, *image_contents_rgbt; int i,ERROR; Header1 header1; Header2 header2; if((image_rgb = fopen("Image_Contents_RGB","wb")) == NULL) /*For storing RGB contents*/ { printf("\n Error creating Image_Contents_RGB file !!"); return -1; } if((image_yuv = fopen("Image_Contents_YUV","wb")) == NULL) /* For storing YUV contents*/ { printf("\n Error creating Image_Contents_YUV file !!"); return -1; } if((down_sampled = fopen("Down_Sampled_YUV","wb")) == NULL) /* For storing down sampled YUV contents*/ { printf("\n Error creating Down_Sampled_YUV file !!"); return -1; } if((DCT = fopen("DCT_Output","wb")) == NULL) /* For storing DCT contents*/ { printf("\n Error creating Down_Sampled_YUV file !!"); return -1; } if(argc!= 2) /* syntax error check*/ { printf("\n The format is ./quantizer [file-name]"); return -1; } if((fp = fopen(argv[1],"rb"))== NULL) /* open the .bmp file for reading*/ { printf("\n Error opening the file specified. Please check if the file exists !!\n"); fclose(fp); return -1; } if(fread(&header1,sizeof(header1),1,fp)!=1) /* Read the primary header from the bmp file */ { printf("\n Error reading header1 \n"); fclose(fp); return -1; } if(header1.type!= 19778) /* check if its a valid bmp file*/ { printf("\n Not a bmp file. Exiting !! \n"); fclose(fp); return -1; } if(fread(&header2,sizeof(header2),1,fp)!=1 ) { printf("\n Error reading header 2"); fclose(fp); return -1; } image_contents_rgb = (unsigned char*)malloc(sizeof(char) * header2.imagesize); image_contents_rgbt = (unsigned char*)malloc(sizeof(char) * header2.imagesize); /*allocate memory to store image data*/ image_contents_yuv = (unsigned char*)malloc(sizeof(char) * header2.imagesize); fseek(fp,header1.offset,SEEK_SET); /* To assign file pointer to start of image byte*/ if((ERROR = fread(image_contents_rgb,header2.imagesize,1,fp))!=1) { printf("\nError reading contents\n"); free(image_contents_rgb); fclose(fp); return -1; } fclose(fp); red = (unsigned char**)malloc(sizeof(char*) * header2.height); green = (unsigned char**)malloc(sizeof(char*) * header2.height); blue = (unsigned char**)malloc(sizeof(char*) * header2.height); Y = (unsigned char**)malloc(sizeof(char*) * header2.height); U = (unsigned char**)malloc(sizeof(char*) * header2.height); V = (unsigned char**)malloc(sizeof(char*) * header2.height); for(i=0 ; i<header2.height ;i++) { red[i] = (unsigned char*)malloc( sizeof(char) * header2.width); green[i]= (unsigned char*)malloc( sizeof(char) * header2.width); blue[i]= (unsigned char*)malloc( sizeof(char) * header2.width); Y[i] = (unsigned char*)malloc( sizeof(char) * header2.width); U[i] = (unsigned char*)malloc( sizeof(char) * header2.width); V[i] = (unsigned char*)malloc( sizeof(char) * header2.width); } vector2matrix(red, green, blue, image_contents_rgb, header2.height, header2.width); /* Store image contents as matrix */ FileWrite(red, green, blue, image_rgb, header2.height, header2.width); /* Optional step*/ rgb2yuv(red,green,blue,Y,U,V,header2.height,header2.width); /* RGB to YUV conversion*/ FileWrite(Y, U, V, image_yuv, header2.height, header2.width); /* Writing YUV into file*/ downsampling(Y, U, V, header2.height, header2.width); /* 4:2:0 Downsampling and update YUV */ FileWrite(Y, U, V, down_sampled, header2.height, header2.width); /* Write downsampled YUV into file*/ dct(DCT, header2.height, header2.width); /* Perform dct and store the result into a file*/ printf("\n"); yuv2rgb(red,green,blue,Y,U,V,header2.height,header2.width); /* Optional step*/ matrix2vector_rgb(red,green,blue,image_contents_rgbt,header2.height,header2.width); /* convert back from matrix to vector*/ matrix2vector_yuv(Y,U,V,image_contents_yuv,header2.height,header2.width); if((image = fopen("Output_image","wb")) == NULL) { printf("\n ERROR opening the file to write quantized image !!\n"); fclose(image); return -1; } if(fwrite(&header1,sizeof(header1),1,image)!= 1) { printf("\n ERROR writing header 1 into destination file !!\n"); fclose(image); return -1; } if(fwrite(&header2,sizeof(header2),1,image)!= 1) { printf("\n ERROR writing header 2 into destination file !! \n"); fclose(image); return -1; } fseek(image, header1.offset, SEEK_SET); if(fwrite(image_contents_rgbt,header2.imagesize,1,image)!=1) /* Change the vector to write into the bmp file here*/ { printf("\n ERROR writing image contents into destination file \n"); fclose(image); return -1; } free(red); free(green); free(blue); free(Y); free(U); free(V); fclose(image); return 0; }
// method, which performs the block-matching for a given channel int block_matching (png_img* img, png_img* tmp, unsigned int const b_size, unsigned int const b_step, unsigned int const sigma, unsigned int const h_search, unsigned int const v_search, double const th_2d, double const tau_match, unsigned int const channel, unsigned int block_marking, list_t* list) { int i, j, k, l; block_t ref_block; block_t cmp_block; double d; // block distance group_t group = 0; // group, which holds a set of similar blocks int count = 0; // variable to add increasing numbers to the file names // allocate block memory if (new_block_struct(b_size, &ref_block) != 0) { return 1; } if (new_block_struct(b_size, &cmp_block) != 0) { return 1; } // compare blocks according to the sliding-window manner for (j=0; j<img->height; j=j+b_step) { for (i=0; i<img->width; i=i+b_step) { // obtain refernce block if (!exceeds_image_dimensions(img->width, img->height, b_size, i, j)) { if (get_block (img, channel, &ref_block, i-(b_size/2), j-(b_size/2)) != 0) { return 1; } if (append_block (&group, &ref_block, 0.0) != 0) { return 1; } if (block_marking) { png_copy_values (tmp, img); mark_search_window (tmp, &ref_block, h_search, v_search); mark_ref_block (tmp, &ref_block); } for (l=0; l<img->height; l=l+b_step) { for (k=0; k<img->width; k=k+b_step) { // obtain block to compare to reference block if (!(exceeds_image_dimensions(img->width, img->height, b_size, k, l)) && !(exceeds_search_window(img->width, img->height, b_size, h_search, v_search, i, j, k, l)) && !((i==k) && (j==l))) { // must be different from reference block if (get_block (img, channel, &cmp_block, k-(b_size/2), l-(b_size/2)) != 0) { return 1; } // compare blocks for similarity d = get_block_distance (&ref_block, &cmp_block, sigma, th_2d); // decide whether block similarity is sufficient if (d < tau_match*255) { if (append_block (&group, &cmp_block, d) != 0) { return 1; } if (block_marking) { mark_cmp_block (tmp, &cmp_block); } } } } } // add group of similar blocks to list if (append_group (list, &group) != 0) { return 1; } // write output image with marked group in it if (block_marking) { yuv2rgb(tmp); if (png_write(tmp, "img/rgb/grp/", "group", ++count) != 0) { return 1; } } group = 0; } } } return 0; }
Image ColorConvert::uyvy2rgb(Image im) { return yuv2rgb(uyvy2yuv(im)); }
int main_loop(void *data) { int ret=0; int i,j,k,l,m,n,o; unsigned int YUVMacroPix; unsigned char *pix8 = (unsigned char *)&YUVMacroPix; Pix *pix2; char *pix; if ((pix2= malloc(sizeof(Pix)))==NULL) { printf("couldn't allocate memory for: pix2\n"); ret=1; return(ret); } //fprintf(stderr,"Thread started...\n"); /* ImageSurf=SDL_CreateRGBSurface(SDL_SWSURFACE, overlay->w, overlay->h, 24, 0x00ff0000,0x0000ff00,0x000000ff,0); */ while (videoIn->signalquit) { currtime = SDL_GetTicks(); if (currtime - lasttime > 0) { frmrate = 1000/(currtime - lasttime); } lasttime = currtime; // sprintf(capt,"Frame Rate: %d",frmrate); // SDL_WM_SetCaption(capt, NULL); if (uvcGrab(videoIn) < 0) { printf("Error grabbing=> Frame Rate is %d\n",frmrate); videoIn->signalquit=0; ret = 2; } SDL_LockYUVOverlay(overlay); memcpy(p, videoIn->framebuffer, videoIn->width * (videoIn->height) * 2); SDL_UnlockYUVOverlay(overlay); SDL_DisplayYUVOverlay(overlay, &drect); /*capture Image*/ if (videoIn->capImage){ if((pim= malloc((pscreen->w)*(pscreen->h)*3))==NULL){/*24 bits -> 3bytes 32 bits ->4 bytes*/ printf("Couldn't allocate memory for: pim\n"); videoIn->signalquit=0; ret = 3; } //char *ppmheader = "P6\n# Generated by guvcview\n320 240\n255\n"; //FILE * out = fopen("Yimage.ppm", "wb"); //saving as ppm //fprintf(out, ppmheader); k=overlay->h; //printf("overlay->h is %i\n",overlay->h); //printf("and pitches[0] is %i\n",overlay->pitches[0]); for(j=0;j<(overlay->h);j++){ l=j*overlay->pitches[0];/*must add lines already writen=*/ /*pitches is the overlay number */ /*off bytes in a line (2*width) */ m=(k*3*overlay->pitches[0])>>1;/*must add lines already writen= */ /*for this case (rgb) every pixel */ /*as 3 bytes (3*width=3*pitches/2) */ /* >>1 = (/2) divide by 2 (?faster?)*/ for (i=0;i<((overlay->pitches[0])>>2);i++){ /*>>2 = (/4)*/ /*iterate every 4 bytes (32 bits)*/ /*Y-U-V-Y1 =>2 pixel (4 bytes) */ n=i<<2;/*<<2 = (*4) multiply by 4 (?faster?)*/ pix8[0] = p[n+l]; pix8[1] = p[n+1+l]; pix8[2] = p[n+2+l]; pix8[3] = p[n+3+l]; /*get RGB data*/ pix2=yuv2rgb(YUVMacroPix,0,pix2); /*In BitMaps lines are upside down and*/ /*pixel format is bgr */ o=i*6; /*first pixel*/ pim[o+m]=pix2->b; pim[o+1+m]=pix2->g; pim[o+2+m]=pix2->r; /*second pixel*/ pim[o+3+m]=pix2->b1; pim[o+4+m]=pix2->g1; pim[o+5+m]=pix2->r1; } k--; } /* SDL_LockSurface(ImageSurf); memcpy(pix, pim,(pscreen->w)*(pscreen->h)*3); //24 bits -> 3bytes 32 bits ->4 bytes SDL_UnlockSurface(ImageSurf);*/ if(SaveBPM(videoIn->ImageFName, width, height, 24, pim)) { fprintf (stderr,"Error: Couldn't capture Image to %s \n", videoIn->ImageFName); } else { printf ("Capture Image to %s \n",videoIn->ImageFName); } free(pim); videoIn->capImage=FALSE; } /*capture AVI */ if (videoIn->capAVI && videoIn->signalquit){ long framesize; switch (AVIFormat) { case 1: framesize=(pscreen->w)*(pscreen->h)*2; /*YUY2 -> 2 bytes per pixel */ if (AVI_write_frame (AviOut, p, framesize) < 0) printf ("write error on avi out \n"); break; case 2: framesize=(pscreen->w)*(pscreen->h)*3; /*DIB 24/32 -> 3/4 bytes per pixel*/ if((pim= malloc(framesize))==NULL){ printf("Couldn't allocate memory for: pim\n"); videoIn->signalquit=0; ret = 4; } k=overlay->h; for(j=0;j<(overlay->h);j++){ l=j*overlay->pitches[0];/*must add lines already writen=*/ /*pitches is the overlay number */ /*off bytes in a line (2*width) */ m=(k*3*overlay->pitches[0])>>1;/*must add lines already writen= */ /*for this case (rgb) every pixel */ /*as 3 bytes (3*width=3*pitches/2) */ /* >>1 = (/2) divide by 2 (?faster?)*/ for (i=0;i<((overlay->pitches[0])>>2);i++){ /*>>2 = (/4)*/ /*iterate every 4 bytes (32 bits)*/ /*Y-U-V-Y1 =>2 pixel (4 bytes) */ n=i<<2;/*<<2 = (*4) multiply by 4 (?faster?)*/ pix8[0] = p[n+l]; pix8[1] = p[n+1+l]; pix8[2] = p[n+2+l]; pix8[3] = p[n+3+l]; /*get RGB data*/ pix2=yuv2rgb(YUVMacroPix,0,pix2); /*In BitMaps lines are upside down and*/ /*pixel format is bgr */ o=i*6; /*first pixel*/ pim[o+m]=pix2->b; pim[o+1+m]=pix2->g; pim[o+2+m]=pix2->r; /*second pixel*/ pim[o+3+m]=pix2->b1; pim[o+4+m]=pix2->g1; pim[o+5+m]=pix2->r1; } k--; } if (AVI_write_frame (AviOut, pim, framesize) < 0) printf ("write error on avi out \n"); free(pim); break; } framecount++; } SDL_Delay(SDL_WAIT_TIME); }
static void process_image(const void *p, int size) { int i, newi, newsize=0; struct timespec frame_time; int y_temp, y2_temp, u_temp, v_temp; unsigned char *pptr = (unsigned char *)p; // record when process was called clock_gettime(CLOCK_REALTIME, &frame_time); framecnt++; printf("frame %d: ", framecnt); // This just dumps the frame to a file now, but you could replace with whatever image // processing you wish. // if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_GREY) { printf("Dump graymap as-is size %d\n", size); dump_pgm(p, size, framecnt, &frame_time); } else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) { #if defined(COLOR_CONVERT) printf("Dump YUYV converted to RGB size %d\n", size); // Pixels are YU and YV alternating, so YUYV which is 4 bytes // We want RGB, so RGBRGB which is 6 bytes // for(i=0, newi=0; i<size; i=i+4, newi=newi+6) { y_temp=(int)pptr[i]; u_temp=(int)pptr[i+1]; y2_temp=(int)pptr[i+2]; v_temp=(int)pptr[i+3]; yuv2rgb(y_temp, u_temp, v_temp, &bigbuffer[newi], &bigbuffer[newi+1], &bigbuffer[newi+2]); yuv2rgb(y2_temp, u_temp, v_temp, &bigbuffer[newi+3], &bigbuffer[newi+4], &bigbuffer[newi+5]); } dump_ppm(bigbuffer, ((size*6)/4), framecnt, &frame_time); #else printf("Dump YUYV converted to YY size %d\n", size); // Pixels are YU and YV alternating, so YUYV which is 4 bytes // We want Y, so YY which is 2 bytes // for(i=0, newi=0; i<size; i=i+4, newi=newi+2) { // Y1=first byte and Y2=third byte bigbuffer[newi]=pptr[i]; bigbuffer[newi+1]=pptr[i+2]; } dump_pgm(bigbuffer, (size/2), framecnt, &frame_time); #endif } else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) { printf("Dump RGB as-is size %d\n", size); dump_ppm(p, size, framecnt, &frame_time); } else { printf("ERROR - unknown dump format\n"); } fflush(stderr); //fprintf(stderr, "."); fflush(stdout); }
Image ColorConvert::apply(Image im, string from, string to) { // check for the trivial case assert(from != to, "color conversion from %s to %s is pointless\n", from.c_str(), to.c_str()); // unsupported destination color spaces if (to == "yuyv" || to == "uyvy") { panic("Unsupported destination color space: %s\n", to.c_str()); } // direct conversions that don't have to go via rgb if (from == "yuyv" && to == "yuv") { return yuyv2yuv(im); } else if (from == "uyvy" && to == "yuv") { return uyvy2yuv(im); } else if (from == "xyz" && to == "lab") { return xyz2lab(im); } else if (from == "lab" && to == "xyz") { return lab2xyz(im); } else if (from == "argb" && to == "xyz") { return argb2xyz(im); } else if (from == "xyz" && to == "argb") { return xyz2argb(im); } else if (from != "rgb" && to != "rgb") { // conversions that go through rgb Image halfway = apply(im, from, "rgb"); return apply(halfway, "rgb", to); } else if (from == "rgb") { // from rgb if (to == "hsv" || to == "hsl" || to == "hsb") { return rgb2hsv(im); } else if (to == "yuv") { return rgb2yuv(im); } else if (to == "xyz") { return rgb2xyz(im); } else if (to == "y" || to == "gray" || to == "grayscale" || to == "luminance") { return rgb2y(im); } else if (to == "lab") { return rgb2lab(im); } else if (to == "argb") { return rgb2argb(im); } else { panic("Unknown color space %s\n", to.c_str()); } } else { //(to == "rgb") if (from == "hsv" || from == "hsl" || from == "hsb") { return hsv2rgb(im); } else if (from == "yuv") { return yuv2rgb(im); } else if (from == "xyz") { return xyz2rgb(im); } else if (from == "y" || from == "gray" || from == "grayscale" || from == "luminance") { return y2rgb(im); } else if (from == "lab") { return lab2rgb(im); } else if (from == "uyvy") { return uyvy2rgb(im); } else if (from == "yuyv") { return yuyv2rgb(im); } else if (from == "argb") { return argb2rgb(im); } else { panic("Unknown color space %s\n", from.c_str()); } } // keep the compiler happy return Image(); }
static int spca50x_process_thumbnail (CameraPrivateLibrary *lib, /* context */ uint8_t ** data, unsigned int *len, /* return these items */ uint8_t * buf, uint32_t file_size, int index) /* input these items */ { uint32_t alloc_size, true_size, w, h, hdrlen; uint8_t *tmp, *rgb_p, *yuv_p; uint8_t *p2 = lib->flash_toc + index*2*32; if (lib->bridge == BRIDGE_SPCA500) { /* for dsc 350 cams */ w = 80; /* Always seems to be 80 x 60 for the DSC-350 cams */ h = 60; } else { /* thumbnails are generated from dc coefficients and are * therefore w/8 x h/8 * Since the dimensions of the thumbnail in the TOC are * always [0,0], Use the TOC entry for the full image. */ w = ((p2[0x0c] & 0xff) + (p2[0x0d] & 0xff) * 0x100) / 8; h = ((p2[0x0e] & 0xff) + (p2[0x0f] & 0xff) * 0x100) / 8; } /* Allow for a long header; get the true length later. */ hdrlen = 15; alloc_size = w * h * 3 + hdrlen; tmp = malloc (alloc_size); if (!tmp) return GP_ERROR_NO_MEMORY; /* Write the header and get its length. Then, verify that * we allocated enough memory. * This should never fail; it would be nice to have an error * code like GP_ERROR_CAMLIB_INTERNAL for cases like this. */ hdrlen = snprintf((char*)tmp, alloc_size, "P6 %d %d 255\n", w, h); true_size = w * h * 3 + hdrlen; if ( true_size > alloc_size ) { free (tmp); return GP_ERROR; } yuv_p = buf; rgb_p = tmp + hdrlen; while (yuv_p < buf + file_size) { uint32_t u, v, y, y2; uint32_t r, g, b; y = yuv_p[0]; y2 = yuv_p[1]; u = yuv_p[2]; v = yuv_p[3]; CHECK (yuv2rgb (y, u, v, &r, &g, &b)); *rgb_p++ = r; *rgb_p++ = g; *rgb_p++ = b; CHECK (yuv2rgb (y2, u, v, &r, &g, &b)); *rgb_p++ = r; *rgb_p++ = g; *rgb_p++ = b; yuv_p += 4; } free (buf); *data = tmp; *len = true_size; return GP_OK; } /* spca50x_process_thumbnail */
Image ColorConvert::yuyv2rgb(Image im) { return yuv2rgb(yuyv2yuv(im)); }
int bm3d (char* const infile, // name of input file char* const kind, // kind of shrinkage (ht, wnr, avg) int const block_size, // size of internal processed blocks int const block_step, // step size between blocks int const sigma, // standard deviation of noise int const max_blocks, // maximum number of block in one 3D array int const h_search, // horizontal width of search window int const v_search, // vertical width of search window double const th_2d, // threshold for the 2D transformation double const tau_match, // match value for block-matching double const th_3d, // threshold for the 3D transformtaion int const block_marking) { // indicates the action of block marking png_img img; // noisy input image png_img org; // temporary image for marking the blocks FILE* log = 0; // log-file for all kinds of messages char logfile[30]; // name of the log-file including path char path[30]; // universally used path-name char prefix[20]; // universally used prefix-name char pure_name[30]; int h_search_true; // true value of horizontal search window size after validation int v_search_true; // true value of vertical search window size after validation list_t y_list = 0; // list of groups of the y-channel list_t u_list = 0; // list of groups of the u-channel list_t v_list = 0; // list of groups of the v-channel clock_t start, end; // time variables for counting durations double time; // ---------------------------------------------------------------------- // OPEN LOG-FILE FOR WRITING // ---------------------------------------------------------------------- // obtain filename without path and extension if (exclude_extension(infile, pure_name) != 0) { return 1; } sprintf (logfile, "log/log_%s_%s[%d].txt", pure_name, kind, sigma); log = fopen (logfile, "a"); if (log == NULL) { generate_error ("Unable to open log-file for writing ..."); return 1; } // ---------------------------------------------------------------------- // INPUT READING AND VALIDATION // ---------------------------------------------------------------------- // read input image if (png_read(&img, infile) != 0) { return 1; } // read temporary image if (png_read(&org, infile) != 0) { return 1; } // control color type if (img.color != PNG_COLOR_TYPE_RGB) { generate_error ("Wrong color type..."); return 1; } // control number of channels if (img.channels != 3) { generate_error ("Wrong number of channels..."); return 1; } // ---------------------------------------------------------------------- // PARAMETER VALIDATION // ---------------------------------------------------------------------- // verify kind of shrinkage if (strcmp(kind, "none") && strcmp(kind, "avg") && strcmp(kind, "ht") && strcmp(kind, "wnr")) { generate_error ("Unknown kind of shrinkage..."); return 1; } // verify block size if ((block_size!=7) && (block_size!=9) && (block_size!=11) && (block_size!=13)) { generate_error ("Wrong value for block size...\nValid values: 7, 9, 11, 13"); return 1; } // verify block step if ((block_step<5) || (block_step>15)) { generate_error ("Block step is out of valid Range...\nValid range: 5..15"); return 1; } // control search window dimensions h_search_true = ((h_search > img.width) || (h_search <= 0)) ? img.width : h_search; v_search_true = ((v_search > img.height) || (v_search <= 0)) ? img.height : v_search; // ---------------------------------------------------------------------- // PRINTING OF STATUS INFORMATION // ---------------------------------------------------------------------- fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "[INFO] ... image dimensions: %dx%d\n", img.width, img.height); fprintf (log, "[INFO] ... kind of shrinkage: %s\n", kind); fprintf (log, "[INFO] ... block size: %d\n", block_size); fprintf (log, "[INFO] ... block step: %d\n", block_step); fprintf (log, "[INFO] ... sigma: %d\n", sigma); fprintf (log, "[INFO] ... maximum number of blocks: %d\n", max_blocks); fprintf (log, "[INFO] ... horizontal search window size: %d\n", h_search_true); fprintf (log, "[INFO] ... vertical search window size: %d\n", v_search_true); fprintf (log, "[INFO] ... threshold 2D: %f\n", th_2d); fprintf (log, "[INFO] ... tau-match 2D: %f\n", tau_match); fprintf (log, "[INFO] ... threshold 3D: %f\n", th_3d); fprintf (log, "[INFO] ... block marking: %s\n\n", block_marking ? "yes" : "no"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); rgb2yuv (&img); // write output image if (png_write(&img, "img/yuv/", "noisy_yuv", 0) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of input image to YUV...\n\n"); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_before"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_before"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_before"); printf ("[INFO] ... end of printing arrays...\n\n"); fprintf (log, "[INFO] ... printed every intput channel as array of values...\n\n"); // ---------------------------------------------------------------------- // BLOCK-MATCHING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of block-matching...\n"); printf ("[INFO] ... ... luminance channel...\n"); start = clock(); if (block_matching(&img, &org, block_size, block_step, sigma, h_search_true, v_search_true, th_2d, tau_match, 0, block_marking, &y_list) != 0) { return 1; } printf ("[INFO] ... end of block-matching...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... block-matching accomplished...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n", time); fprintf (log, "[INFO] ... ... number of groups in list: %d\n\n", list_length(&y_list)); // print recognized groups to file sprintf (path, "grp/org/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } // trim groups to maximal number of blocks printf ("[INFO] ... trimming groups to maximum size...\n\n"); if (trim_list(&y_list, max_blocks) != 0) { return 1; } fprintf (log, "[INFO] ... trimmed groups to maximum size of blocks...\n\n"); // obtain the pixel values from the u- and v-channel of the image printf ("[INFO] ... extracting blocks from chrominance channels...\n"); printf ("[INFO] ... ... chrominance channel 1...\n"); if (get_chrom(&img, &y_list, &u_list, 1)) { return 1; } printf ("[INFO] ... ... chrominance channel 2...\n\n"); if (get_chrom(&img, &y_list, &v_list, 2)) { return 1; } fprintf (log, "[INFO] ... extracted values from chrominance channels...\n\n"); // print trimmed groups to file sprintf (path, "grp/trm/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/trm/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // IMAGE-DENOISING // ---------------------------------------------------------------------- printf ("[INFO] ... launch of shrinkage...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (shrinkage(kind, &y_list, sigma, th_3d, 0) != 0) { return 1; } // printf ("[INFO] ... ... chrominance channel 1...\n"); // if (shrinkage(kind, &u_list, sigma, th_3d, 1) != 0) { // return 1; // } // printf ("[INFO] ... ... chrominance channel 2...\n"); // if (shrinkage(kind, &v_list, sigma, th_3d, 1) != 0) { // return 1; // } printf ("[INFO] ... end of shrinkage...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished shrinkage...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); sprintf (path, "grp/est/%s/y/", kind); if (print_list(y_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/u/", kind); if (print_list(u_list, path, "group") != 0) { return 1; } sprintf (path, "grp/est/%s/v/", kind); if (print_list(v_list, path, "group") != 0) { return 1; } // ---------------------------------------------------------------------- // AGGREGATION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of aggregation...\n"); start = clock(); printf ("[INFO] ... ... luminance channel...\n"); if (aggregate(kind, &img, &y_list, 0) != 0) { return 1; } // printf ("[INFO] ... chrominance channel 1...\n"); // if (aggregate(kind, &img, &u_list, 1) != 0) { // return 1; // } // printf ("[INFO] ... chrominance channel 2...\n"); // if (aggregate(kind, &img, &v_list, 2) != 0) { // return 1; // } printf ("[INFO] ... end of aggregation...\n\n"); end = clock(); time = (end - start) / (double)CLOCKS_PER_SEC; fprintf (log, "[INFO] ... accomplished aggregation...\n"); fprintf (log, "[INFO] ... ... elapsed time: %f\n\n", time); // ---------------------------------------------------------------------- // IMAGE-TO-ARRAY CONVERSION // ---------------------------------------------------------------------- printf ("[INFO] ... launch of printing image as three separate value arrays...\n"); printf ("[INFO] ... ... luminance channel...\n"); img2array (&img, 0, "img/", "y_channel_after"); printf ("[INFO] ... ... chrominance channel 1...\n"); img2array (&img, 1, "img/", "u_channel_after"); printf ("[INFO] ... ... chrominance channel 2...\n"); img2array (&img, 2, "img/", "v_channel_after"); printf ("[INFO] ... end of printing arrays...\n\n"); // ---------------------------------------------------------------------- // COLORSPACE CONVERSION & WRITEBACK // ---------------------------------------------------------------------- printf ("[INFO] ... launch of color conversion...\n"); yuv2rgb (&img); sprintf (prefix, "denoised_rgb_%s_%s", pure_name, kind); // write output image if (png_write(&img, "img/rgb/", prefix, sigma) != 0) { return 1; } printf ("[INFO] ... end of color conversion...\n\n"); fprintf (log, "[INFO] ... converted colorspace of output image to RGB...\n\n"); fprintf (log, "[INFO] ... PSNR after denoising: %fdB\n", get_snr(&org, &img)); fprintf (log, "-------------------------------------------------------------------------\n"); fprintf (log, "-------------------------------------------------------------------------\n\n\n"); // ---------------------------------------------------------------------- // FREEING DYNAMICALY ALLOCATED MEMORY // ---------------------------------------------------------------------- free_list (&y_list); free_list (&u_list); free_list (&v_list); png_free_mem (&img); png_free_mem (&org); fclose (log); return 0; }