├── README.md └── imageconvert ├── main.cpp ├── Makefile ├── imageconvert.h └── imageconvert.cpp /README.md: -------------------------------------------------------------------------------- 1 | # imageconvert 2 | imageconvert source code 3 | -------------------------------------------------------------------------------- /imageconvert/main.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: main.cpp 3 | > Author:maobin 4 | > Mail: 5 | > Created Time: Wed 13 Dec 2017 08:37:29 PM CST 6 | ************************************************************************/ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "imageconvert.h" 14 | #include 15 | using namespace std; 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | ImageConvert *imageconvert = new ImageConvert; 20 | if (imageconvert->Init(argc,argv) != 0) 21 | { 22 | delete imageconvert; 23 | return -1; 24 | } 25 | imageconvert->Convert(); 26 | 27 | delete imageconvert; 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /imageconvert/Makefile: -------------------------------------------------------------------------------- 1 | CXX=$(CROSS_COMPILE)g++ 2 | AR=$(CROSS_COMPILE)ar 3 | 4 | #CXXFLAGS= -g -I/usr/include -I./utils/ -I../src -I/usr/include/libcamhal/ `pkg-config --cflags gstreamer-1.0 glib-2.0 gstreamer-video-1.0 gtk+-3.0` 5 | #LDFLAGS= -L/usr/lib64/ -lcamhal -lpthread `pkg-config --libs gstreamer-1.0 gobject-2.0 glib-2.0 gstreamer-video-1.0 gtk+-3.0` 6 | 7 | CXXFLAGS= -I/usr/local/include -L/usr/local/lib `pkg-config --cflags --libs opencv ` -g -std=c++11 8 | LDFLAGS= -I/usr/local/include -L/usr/local/lib ` pkg-config --cflags --libs opencv ` -g -std=c++11 9 | 10 | casedir=. 11 | 12 | srcobjs=$(patsubst %.cpp, %.o, $(shell ls $(casedir)/*.cpp)) 13 | 14 | mainobj=main.o 15 | 16 | testbin=imageconvert 17 | 18 | all: $(testbin) 19 | 20 | %.o: %.cpp 21 | $(CXX) $(CXXFLAGS) -o $@ -c $< 22 | 23 | $(testbin): $(mainobj) $(srcobjs) 24 | $(CXX) -o $@ $^ $(LDFLAGS) 25 | .PHONY : clean 26 | clean: 27 | rm -f $(mainobj) $(srcobjs) $(testbin) 28 | -------------------------------------------------------------------------------- /imageconvert/imageconvert.h: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: imageconvert.h 3 | > Author:maobin 4 | > Mail: 5 | > Created Time: Wed 13 Dec 2017 08:39:14 PM CST 6 | ************************************************************************/ 7 | 8 | #ifndef _IMAGECONVERT_H 9 | #define _IMAGECONVERT_H 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #define BYTE unsigned char 17 | #define ULONG unsigned long 18 | using namespace std; 19 | using namespace cv; 20 | 21 | enum MEDIA_FORMAT 22 | { 23 | MEDIA_FORMAT_UNKNOWN, 24 | MEDIA_FORMAT_NV12, 25 | MEDIA_FORMAT_NV16, 26 | MEDIA_FORMAT_UYVY, 27 | MEDIA_FORMAT_YUY2, 28 | MEDIA_FORMAT_XRGB, 29 | MEDIA_FORMAT_XBGR, 30 | MEDIA_FORMAT_I420, 31 | MEDIA_FORMAT_ARGB, 32 | MEDIA_FORMAT_BGRA, 33 | MEDIA_FORMAT_BGR24, 34 | MEDIA_FORMAT_RGB565 35 | }; 36 | 37 | 38 | #define DEBUG_LEVEL 1 39 | enum DebugLevel 40 | { 41 | INFO, 42 | ERROR, 43 | RESULT, 44 | DEBUG 45 | }; 46 | class ImageConvert 47 | { 48 | 49 | public: 50 | ImageConvert(); 51 | ~ImageConvert(); 52 | void Usage(); 53 | void PrintInfo(); 54 | int Init(int argc, char *argv[]); 55 | int Spilt_parameters(const char *arg, char *key, char *value); 56 | int Convert(); 57 | private: 58 | typedef void (*func_convert_format)(uchar *buff, int stride, int w, int h, Mat &dst); 59 | static inline BYTE Clip(int clr); 60 | static inline void ConvertYCrCbToRGB( 61 | BYTE y, 62 | BYTE cr, 63 | BYTE cb, 64 | BYTE &r, 65 | BYTE &g, 66 | BYTE &b 67 | ); 68 | static void convert_nv122bgr(uchar *buff, int stride, int w, int h, Mat &dst); 69 | static void convert_nv162bgr(uchar *buff, int stride, int w, int h, Mat &dst); 70 | static void convert_yuy22bgr(uchar *buff, int stride, int w, int h, Mat &dst); 71 | static void convert_uyvy2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 72 | static void convert_i4202bgr(uchar *buff, int stride, int w, int h, Mat &dst); 73 | static void convert_xbgr2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 74 | static void convert_xrgb2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 75 | static void convert_argb2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 76 | static void convert_bgra2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 77 | static void convert_bgr2bgr(uchar *buff, int stride, int w, int h, Mat &dst); 78 | static void convert_rgb162bgr(uchar *buff, int stride, int w, int h, Mat &dst); 79 | private: 80 | ULONG ReadRawData(const char *filename, uchar **pnv12); 81 | ULONG DataLength(const char *filename); 82 | MEDIA_FORMAT GetFormatFromString(char *strRawDumpFormat); 83 | private: 84 | int checkParameters(); 85 | void debug(char *msg,DebugLevel level); 86 | 87 | private: 88 | int m_width; 89 | int m_height; 90 | int m_count; 91 | int m_frame; 92 | int m_oneimagesize; 93 | ULONG m_dataLength; 94 | char m_format[16]; 95 | char m_filename[256]; 96 | 97 | 98 | }; 99 | #endif 100 | -------------------------------------------------------------------------------- /imageconvert/imageconvert.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************* 2 | > File Name: imageconvert.cpp 3 | > Author:maobin 4 | > Mail: 5 | > Created Time: Wed 13 Dec 2017 04:47:58 PM CST 6 | ************************************************************************/ 7 | 8 | #include "imageconvert.h" 9 | #define ULONG unsigned long 10 | ImageConvert::ImageConvert():m_width(0),m_height(0),m_count(0),m_oneimagesize(0),m_dataLength(0),m_frame(0) 11 | { 12 | memset(m_filename,0,sizeof(m_filename)); 13 | memset(m_format,0,sizeof(m_format)); 14 | cout << "ImageConvert:" << __FUNCTION__ << __LINE__ <> 5) + (pSrcBGR[x2+1]&0x7) << 3) << 2; // G 595 | pDstRGB[x3 + 2] = (pSrcBGR[x2+1]&0xF8); // R 596 | } 597 | } 598 | } 599 | 600 | inline BYTE ImageConvert::Clip(int clr) 601 | { 602 | return (BYTE) (clr < 0 ? 0 : (clr > 255 ? 255 : clr)); 603 | } 604 | 605 | inline void ImageConvert::ConvertYCrCbToRGB( 606 | BYTE y, 607 | BYTE cr, 608 | BYTE cb, 609 | BYTE &r, 610 | BYTE &g, 611 | BYTE &b 612 | ) 613 | { 614 | int c = (int)y - 16; 615 | int d = (int) cb - 128; 616 | int e = (int) cr - 128; 617 | 618 | r = Clip((298 * c + 409 * e + 128) >> 8); 619 | g = Clip((298 * c - 100 * d - 208 * e + 128) >> 8); 620 | b = Clip((298 * c + 516 * d + 128) >> 8); 621 | } 622 | 623 | ULONG ImageConvert::ReadRawData(const char *filename, uchar **pnv12) 624 | { 625 | FILE *fp = NULL; 626 | 627 | fp = fopen(filename, "rb"); 628 | 629 | if (fp == NULL) 630 | { 631 | cout << "fail to open file " << endl; 632 | return -1; 633 | } 634 | 635 | 636 | 637 | fseek(fp, m_oneimagesize * m_frame, SEEK_SET); 638 | ULONG datalen = ftell(fp); 639 | cout << "datalen:" << datalen << endl; 640 | *pnv12 = (uchar *)malloc((size_t) m_oneimagesize); 641 | 642 | size_t readlen = fread(*pnv12, 1, m_oneimagesize, fp); 643 | 644 | fclose(fp); 645 | 646 | if (readlen != m_oneimagesize) 647 | { 648 | cout << "readlength:"<< readlen <<" != imagesize" << m_oneimagesize << endl; 649 | return -2; 650 | } 651 | 652 | return readlen; 653 | } 654 | 655 | ULONG ImageConvert::DataLength(const char *filename) 656 | { 657 | FILE *fp = NULL; 658 | 659 | fp = fopen(filename, "rb"); 660 | 661 | if (fp == NULL) 662 | { 663 | cout << "fail to open file " << endl; 664 | return -1; 665 | } 666 | 667 | fseek(fp, 0L, SEEK_END); 668 | ULONG datalen = ftell(fp); 669 | cout << "datalen:" << datalen << endl; 670 | fseek(fp, 0L, SEEK_SET); 671 | fclose(fp); 672 | return datalen; 673 | } 674 | MEDIA_FORMAT ImageConvert:: GetFormatFromString(char *strRawDumpFormat) 675 | { 676 | MEDIA_FORMAT RawDumpFormat = MEDIA_FORMAT_UNKNOWN; 677 | 678 | if (strcmp(strRawDumpFormat, "UYVY") == 0 || strcmp(strRawDumpFormat, "uyvy") == 0) 679 | { 680 | RawDumpFormat = MEDIA_FORMAT_UYVY; 681 | } 682 | else if (strcmp(strRawDumpFormat, "YUY2") == 0 || strcmp(strRawDumpFormat, "yuy2") == 0 || strcmp(strRawDumpFormat, "YUYV") == 0 || strcmp(strRawDumpFormat, "yuyv") == 0) 683 | { 684 | RawDumpFormat = MEDIA_FORMAT_YUY2; 685 | } 686 | else if (strcmp(strRawDumpFormat, "NV12") == 0 || strcmp(strRawDumpFormat, "nv12") == 0) 687 | { 688 | RawDumpFormat = MEDIA_FORMAT_NV12; 689 | } 690 | else if (strcmp(strRawDumpFormat, "NV16") == 0 || strcmp(strRawDumpFormat, "nv16") == 0) 691 | { 692 | RawDumpFormat = MEDIA_FORMAT_NV16; 693 | } 694 | else if (strcmp(strRawDumpFormat, "XRGB") == 0 || strcmp(strRawDumpFormat, "RGBx") == 0) 695 | { 696 | RawDumpFormat = MEDIA_FORMAT_XRGB; 697 | } 698 | else if (strcmp(strRawDumpFormat, "XBGR") == 0 || strcmp(strRawDumpFormat, "BGRx") == 0) 699 | { 700 | RawDumpFormat = MEDIA_FORMAT_XBGR; 701 | } 702 | else if (strcmp(strRawDumpFormat, "I420") == 0 || strcmp(strRawDumpFormat, "i420") == 0) 703 | { 704 | RawDumpFormat = MEDIA_FORMAT_I420; 705 | } 706 | else if (strcmp(strRawDumpFormat, "ARGB") == 0) 707 | { 708 | RawDumpFormat = MEDIA_FORMAT_ARGB; 709 | } 710 | else if (strcmp(strRawDumpFormat, "BGRA") == 0) 711 | { 712 | RawDumpFormat = MEDIA_FORMAT_BGRA; 713 | } 714 | else if (strcmp(strRawDumpFormat, "BGR") == 0 || 715 | strcmp(strRawDumpFormat, "BGR888") == 0 || 716 | strcmp(strRawDumpFormat, "BGR24") == 0) 717 | { 718 | RawDumpFormat = MEDIA_FORMAT_BGR24; 719 | } 720 | else if (strcmp(strRawDumpFormat, "RGB16") == 0 || 721 | strcmp(strRawDumpFormat, "RGB565") == 0 ) 722 | { 723 | RawDumpFormat = MEDIA_FORMAT_RGB565; 724 | } 725 | return RawDumpFormat; 726 | } 727 | 728 | void ImageConvert::debug(char *msg,DebugLevel level) 729 | { 730 | 731 | if (DEBUG_LEVEL == 1 ) 732 | { 733 | cout << "function:" <<__FUNCTION__<<"line:" << __LINE__<< level << ":" <<"msg:"<< msg << endl; 734 | } 735 | 736 | } 737 | int ImageConvert::checkParameters() 738 | { 739 | if (m_filename[0] == 0) 740 | { 741 | cout << "please type filename...." << endl; 742 | return -1; 743 | } 744 | 745 | if (m_format[0] == 0) 746 | { 747 | cout << "please type format ..." << endl; 748 | return -1; 749 | } 750 | if (m_width == 0) 751 | { 752 | cout <<"please type width..." << endl; 753 | return -1; 754 | } 755 | if (m_height == 0) 756 | { 757 | cout <<"pleae type height .." << endl; 758 | return -1; 759 | } 760 | 761 | return 0; 762 | } 763 | --------------------------------------------------------------------------------