Path: blob/master/modules/imgcodecs/src/grfmt_pxm.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "utils.hpp"44#include "grfmt_pxm.hpp"45#include <iostream>4647#ifdef HAVE_IMGCODEC_PXM4849namespace cv50{5152///////////////////////// P?M reader //////////////////////////////5354static int ReadNumber(RLByteStream& strm, int maxdigits = 0)55{56int code;57int64 val = 0;58int digits = 0;5960code = strm.getByte();6162while (!isdigit(code))63{64if (code == '#' )65{66do67{68code = strm.getByte();69}70while (code != '\n' && code != '\r');71code = strm.getByte();72}73else if (isspace(code))74{75while (isspace(code))76code = strm.getByte();77}78else79{80#if 181CV_Error_(Error::StsError, ("PXM: Unexpected code in ReadNumber(): 0x%x (%d)", code, code));82#else83code = strm.getByte();84#endif85}86}8788do89{90val = val*10 + (code - '0');91CV_Assert(val <= INT_MAX && "PXM: ReadNumber(): result is too large");92digits++;93if (maxdigits != 0 && digits >= maxdigits) break;94code = strm.getByte();95}96while (isdigit(code));9798return (int)val;99}100101102PxMDecoder::PxMDecoder()103{104m_offset = -1;105m_buf_supported = true;106m_bpp = 0;107m_binary = false;108m_maxval = 0;109}110111112PxMDecoder::~PxMDecoder()113{114close();115}116117size_t PxMDecoder::signatureLength() const118{119return 3;120}121122bool PxMDecoder::checkSignature( const String& signature ) const123{124return signature.size() >= 3 && signature[0] == 'P' &&125'1' <= signature[1] && signature[1] <= '6' &&126isspace(signature[2]);127}128129ImageDecoder PxMDecoder::newDecoder() const130{131return makePtr<PxMDecoder>();132}133134void PxMDecoder::close()135{136m_strm.close();137}138139140bool PxMDecoder::readHeader()141{142bool result = false;143144if( !m_buf.empty() )145{146if( !m_strm.open(m_buf) )147return false;148}149else if( !m_strm.open( m_filename ))150return false;151152CV_TRY153{154int code = m_strm.getByte();155if( code != 'P' )156CV_THROW (RBS_BAD_HEADER);157158code = m_strm.getByte();159switch( code )160{161case '1': case '4': m_bpp = 1; break;162case '2': case '5': m_bpp = 8; break;163case '3': case '6': m_bpp = 24; break;164default: CV_THROW (RBS_BAD_HEADER);165}166167m_binary = code >= '4';168m_type = m_bpp > 8 ? CV_8UC3 : CV_8UC1;169170m_width = ReadNumber(m_strm);171m_height = ReadNumber(m_strm);172173m_maxval = m_bpp == 1 ? 1 : ReadNumber(m_strm);174if( m_maxval > 65535 )175CV_THROW (RBS_BAD_HEADER);176177//if( m_maxval > 255 ) m_binary = false; nonsense178if( m_maxval > 255 )179m_type = CV_MAKETYPE(CV_16U, CV_MAT_CN(m_type));180181if( m_width > 0 && m_height > 0 && m_maxval > 0 && m_maxval < (1 << 16))182{183m_offset = m_strm.getPos();184result = true;185}186}187CV_CATCH (cv::Exception, e)188{189CV_UNUSED(e);190CV_RETHROW();191}192CV_CATCH_ALL193{194std::cerr << "PXM::readHeader(): unknown C++ exception" << std::endl << std::flush;195CV_RETHROW();196}197198if( !result )199{200m_offset = -1;201m_width = m_height = -1;202m_strm.close();203}204return result;205}206207208bool PxMDecoder::readData( Mat& img )209{210bool color = img.channels() > 1;211uchar* data = img.ptr();212PaletteEntry palette[256];213bool result = false;214const int bit_depth = CV_ELEM_SIZE1(m_type)*8;215const int src_pitch = divUp(m_width*m_bpp*(bit_depth/8), 8);216int nch = CV_MAT_CN(m_type);217int width3 = m_width*nch;218219if( m_offset < 0 || !m_strm.isOpened())220return false;221222uchar gray_palette[256] = {0};223224// create LUT for converting colors225if( bit_depth == 8 )226{227CV_Assert(m_maxval < 256 && m_maxval > 0);228229for (int i = 0; i <= m_maxval; i++)230gray_palette[i] = (uchar)((i*255/m_maxval)^(m_bpp == 1 ? 255 : 0));231232FillGrayPalette( palette, m_bpp==1 ? 1 : 8 , m_bpp == 1 );233}234235CV_TRY236{237m_strm.setPos( m_offset );238239switch( m_bpp )240{241////////////////////////// 1 BPP /////////////////////////242case 1:243CV_Assert(CV_MAT_DEPTH(m_type) == CV_8U);244if( !m_binary )245{246AutoBuffer<uchar> _src(m_width);247uchar* src = _src.data();248249for (int y = 0; y < m_height; y++, data += img.step)250{251for (int x = 0; x < m_width; x++)252src[x] = ReadNumber(m_strm, 1) != 0;253254if( color )255FillColorRow8( data, src, m_width, palette );256else257FillGrayRow8( data, src, m_width, gray_palette );258}259}260else261{262AutoBuffer<uchar> _src(src_pitch);263uchar* src = _src.data();264265for (int y = 0; y < m_height; y++, data += img.step)266{267m_strm.getBytes( src, src_pitch );268269if( color )270FillColorRow1( data, src, m_width, palette );271else272FillGrayRow1( data, src, m_width, gray_palette );273}274}275result = true;276break;277278////////////////////////// 8 BPP /////////////////////////279case 8:280case 24:281{282AutoBuffer<uchar> _src(std::max<size_t>(width3*2, src_pitch));283uchar* src = _src.data();284285for (int y = 0; y < m_height; y++, data += img.step)286{287if( !m_binary )288{289for (int x = 0; x < width3; x++)290{291int code = ReadNumber(m_strm);292if( (unsigned)code > (unsigned)m_maxval ) code = m_maxval;293if( bit_depth == 8 )294src[x] = gray_palette[code];295else296((ushort *)src)[x] = (ushort)code;297}298}299else300{301m_strm.getBytes( src, src_pitch );302if( bit_depth == 16 && !isBigEndian() )303{304for (int x = 0; x < width3; x++)305{306uchar v = src[x * 2];307src[x * 2] = src[x * 2 + 1];308src[x * 2 + 1] = v;309}310}311}312313if( img.depth() == CV_8U && bit_depth == 16 )314{315for (int x = 0; x < width3; x++)316{317int v = ((ushort *)src)[x];318src[x] = (uchar)(v >> 8);319}320}321322if( m_bpp == 8 ) // image has one channel323{324if( color )325{326if( img.depth() == CV_8U ) {327uchar *d = data, *s = src, *end = src + m_width;328for( ; s < end; d += 3, s++)329d[0] = d[1] = d[2] = *s;330} else {331ushort *d = (ushort *)data, *s = (ushort *)src, *end = ((ushort *)src) + m_width;332for( ; s < end; s++, d += 3)333d[0] = d[1] = d[2] = *s;334}335}336else337memcpy(data, src, img.elemSize1()*m_width);338}339else340{341if( color )342{343if( img.depth() == CV_8U )344icvCvt_RGB2BGR_8u_C3R( src, 0, data, 0, cvSize(m_width,1) );345else346icvCvt_RGB2BGR_16u_C3R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1) );347}348else if( img.depth() == CV_8U )349icvCvt_BGR2Gray_8u_C3C1R( src, 0, data, 0, cvSize(m_width,1), 2 );350else351icvCvt_BGRA2Gray_16u_CnC1R( (ushort *)src, 0, (ushort *)data, 0, cvSize(m_width,1), 3, 2 );352}353}354result = true;355break;356}357default:358CV_Error(Error::StsError, "m_bpp is not supported");359}360}361CV_CATCH (cv::Exception, e)362{363CV_UNUSED(e);364CV_RETHROW();365}366CV_CATCH_ALL367{368std::cerr << "PXM::readData(): unknown exception" << std::endl << std::flush;369CV_RETHROW();370}371372return result;373}374375376//////////////////////////////////////////////////////////////////////////////////////////377378PxMEncoder::PxMEncoder(PxMMode mode) :379mode_(mode)380{381switch (mode)382{383case PXM_TYPE_AUTO: m_description = "Portable image format - auto (*.pnm)"; break;384case PXM_TYPE_PBM: m_description = "Portable image format - monochrome (*.pbm)"; break;385case PXM_TYPE_PGM: m_description = "Portable image format - gray (*.pgm)"; break;386case PXM_TYPE_PPM: m_description = "Portable image format - color (*.ppm)"; break;387default:388CV_Error(Error::StsInternal, "");389}390m_buf_supported = true;391}392393PxMEncoder::~PxMEncoder()394{395}396397bool PxMEncoder::isFormatSupported(int depth) const398{399if (mode_ == PXM_TYPE_PBM)400return depth == CV_8U;401return depth == CV_8U || depth == CV_16U;402}403404bool PxMEncoder::write(const Mat& img, const std::vector<int>& params)405{406bool isBinary = true;407408int width = img.cols, height = img.rows;409int _channels = img.channels(), depth = (int)img.elemSize1()*8;410int channels = _channels > 1 ? 3 : 1;411int fileStep = width*(int)img.elemSize();412int x, y;413414for( size_t i = 0; i < params.size(); i += 2 )415{416if( params[i] == IMWRITE_PXM_BINARY )417isBinary = params[i+1] != 0;418}419420int mode = mode_;421if (mode == PXM_TYPE_AUTO)422{423mode = img.channels() == 1 ? PXM_TYPE_PGM : PXM_TYPE_PPM;424}425426if (mode == PXM_TYPE_PGM && img.channels() > 1)427{428CV_Error(Error::StsBadArg, "Portable bitmap(.pgm) expects gray image");429}430if (mode == PXM_TYPE_PPM && img.channels() != 3)431{432CV_Error(Error::StsBadArg, "Portable bitmap(.ppm) expects BGR image");433}434if (mode == PXM_TYPE_PBM && img.type() != CV_8UC1)435{436CV_Error(Error::StsBadArg, "For portable bitmap(.pbm) type must be CV_8UC1");437}438439WLByteStream strm;440441if( m_buf )442{443if( !strm.open(*m_buf) )444return false;445int t = CV_MAKETYPE(img.depth(), channels);446m_buf->reserve( alignSize(256 + (isBinary ? fileStep*height :447((t == CV_8UC1 ? 4 : t == CV_8UC3 ? 4*3+2 :448t == CV_16UC1 ? 6 : 6*3+2)*width+1)*height), 256));449}450else if( !strm.open(m_filename) )451return false;452453int lineLength;454int bufferSize = 128; // buffer that should fit a header455456if( isBinary )457lineLength = width * (int)img.elemSize();458else459lineLength = (6 * channels + (channels > 1 ? 2 : 0)) * width + 32;460461if( bufferSize < lineLength )462bufferSize = lineLength;463464AutoBuffer<char> _buffer(bufferSize);465char* buffer = _buffer.data();466467// write header;468const int code = ((mode == PXM_TYPE_PBM) ? 1 : (mode == PXM_TYPE_PGM) ? 2 : 3)469+ (isBinary ? 3 : 0);470const char* comment = "# Generated by OpenCV " CV_VERSION "\n";471472int header_sz = sprintf(buffer, "P%c\n%s%d %d\n",473(char)('0' + code), comment,474width, height);475CV_Assert(header_sz > 0);476if (mode != PXM_TYPE_PBM)477{478int sz = sprintf(&buffer[header_sz], "%d\n", (1 << depth) - 1);479CV_Assert(sz > 0);480header_sz += sz;481}482483strm.putBytes(buffer, header_sz);484485for( y = 0; y < height; y++ )486{487const uchar* const data = img.ptr(y);488if( isBinary )489{490if (mode == PXM_TYPE_PBM)491{492char* ptr = buffer;493int bcount = 7;494char byte = 0;495for (x = 0; x < width; x++)496{497if (bcount == 0)498{499if (data[x] == 0)500byte = (byte) | 1;501*ptr++ = byte;502bcount = 7;503byte = 0;504}505else506{507if (data[x] == 0)508byte = (byte) | (1 << bcount);509bcount--;510}511}512if (bcount != 7)513{514*ptr++ = byte;515}516strm.putBytes(buffer, (int)(ptr - buffer));517continue;518}519520if( _channels == 3 )521{522if( depth == 8 )523icvCvt_BGR2RGB_8u_C3R( (const uchar*)data, 0,524(uchar*)buffer, 0, cvSize(width,1) );525else526icvCvt_BGR2RGB_16u_C3R( (const ushort*)data, 0,527(ushort*)buffer, 0, cvSize(width,1) );528}529530// swap endianness if necessary531if( depth == 16 && !isBigEndian() )532{533if( _channels == 1 )534memcpy( buffer, data, fileStep );535for( x = 0; x < width*channels*2; x += 2 )536{537uchar v = buffer[x];538buffer[x] = buffer[x + 1];539buffer[x + 1] = v;540}541}542543strm.putBytes( (channels > 1 || depth > 8) ? buffer : (const char*)data, fileStep);544}545else546{547char* ptr = buffer;548if (mode == PXM_TYPE_PBM)549{550CV_Assert(channels == 1);551CV_Assert(depth == 8);552for (x = 0; x < width; x++)553{554ptr[0] = data[x] ? '0' : '1';555ptr += 1;556}557}558else559{560if( channels > 1 )561{562if( depth == 8 )563{564for( x = 0; x < width*channels; x += channels )565{566sprintf( ptr, "% 4d", data[x + 2] );567ptr += 4;568sprintf( ptr, "% 4d", data[x + 1] );569ptr += 4;570sprintf( ptr, "% 4d", data[x] );571ptr += 4;572*ptr++ = ' ';573*ptr++ = ' ';574}575}576else577{578for( x = 0; x < width*channels; x += channels )579{580sprintf( ptr, "% 6d", ((const ushort *)data)[x + 2] );581ptr += 6;582sprintf( ptr, "% 6d", ((const ushort *)data)[x + 1] );583ptr += 6;584sprintf( ptr, "% 6d", ((const ushort *)data)[x] );585ptr += 6;586*ptr++ = ' ';587*ptr++ = ' ';588}589}590}591else592{593if( depth == 8 )594{595for( x = 0; x < width; x++ )596{597sprintf( ptr, "% 4d", data[x] );598ptr += 4;599}600}601else602{603for( x = 0; x < width; x++ )604{605sprintf( ptr, "% 6d", ((const ushort *)data)[x] );606ptr += 6;607}608}609}610}611612*ptr++ = '\n';613614strm.putBytes( buffer, (int)(ptr - buffer) );615}616}617618strm.close();619return true;620}621622}623624#endif // HAVE_IMGCODEC_PXM625626627