File: pfs_transform_colorspace.cpp

package info (click to toggle)
pfstools 2.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,548 kB
  • sloc: cpp: 26,364; javascript: 3,814; ansic: 999; sh: 180; python: 65; makefile: 47
file content (133 lines) | stat: -rw-r--r-- 4,661 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * @brief Transform between color spaces using pfs library in MATLAB
 * 
 * This file is a part of PFSTOOLS package.
 * ---------------------------------------------------------------------- 
 * Copyright (C) 2003,2004 Rafal Mantiuk and Grzegorz Krawczyk
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * ---------------------------------------------------------------------- 
 *
 * @author Rafal Mantiuk, <mantiuk@mpi-sb.mpg.de>
 *
 * $Id: pfs_transform_colorspace.cpp,v 1.10 2014/02/16 19:02:30 rafm Exp $
 */

#include "compatibility.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

#include <sstream>

#include "mex.h"
#include "mex_utils.h"

#define SCRIPT_NAME "pfs_transform_colorspace"
#define error mexErrMsgTxt

#include <pfs.h>

#include <config.h>

static pfs::ColorSpace findColorSpace( const char *name );

void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{     
  /* Check for proper number of arguments. */
  if( (nrhs != 5 || 
	  !mxIsChar( prhs[0] ) ||
	  !( mxIsDouble( prhs[1] ) || mxIsSingle( prhs[1] ) ) ||
	  !( mxIsDouble( prhs[2] ) || mxIsSingle( prhs[2] ) ) ||
	  !( mxIsDouble( prhs[3] ) || mxIsSingle( prhs[3] ) ) ||
	  !mxIsChar( prhs[4] )) &&
	  (nrhs != 3 || 
	  !mxIsChar( prhs[0] ) ||
	  !( mxIsDouble( prhs[1] ) || mxIsSingle( prhs[1] ) ) || 
	  mxGetNumberOfDimensions( prhs[1] ) != 3 ||
	  !mxIsChar( prhs[2] ))	  
	  )
	    error(SCRIPT_NAME ": Wrong number or type of input parameters!");

  if( nlhs != 3 && nlhs != 1 )
      error(SCRIPT_NAME ": Wrong number of output parameters!");
      
  
  try {
        pfs::ColorSpace inCS = findColorSpace( get_mex_string( prhs[0] ) );
        pfs::ColorSpace outCS = findColorSpace( get_mex_string( prhs[nrhs-1] ) );

        const mwSize *in_dim = mxGetDimensions( prhs[1] );
        const int rows = (int)in_dim[0], cols = (int)in_dim[1];

//		printf( "%d x %d\n", rows, cols );
        
        pfs::Array2DImpl c1Buf( cols, rows ), c2Buf( cols, rows ), c3Buf( cols, rows );
       	
		if( nrhs == 5 ) {
			copy_mex_to_pfsarray( prhs[1], &c1Buf );
			copy_mex_to_pfsarray( prhs[2], &c2Buf );
			copy_mex_to_pfsarray( prhs[3], &c3Buf );
		} else {
			copy_mex_to_pfsarray( prhs[1], &c1Buf, &c2Buf, &c3Buf );
		}

        pfs::transformColorSpace( inCS, &c1Buf, &c2Buf, &c3Buf, outCS, &c1Buf, &c2Buf, &c3Buf );

		if( nlhs == 3 ) {
			plhs[0] = mxCreateNumericMatrix( rows, cols, mxDEFAULT_ARRAY_CLASS, mxREAL );
			plhs[1] = mxCreateNumericMatrix( rows, cols, mxDEFAULT_ARRAY_CLASS, mxREAL );
			plhs[2] = mxCreateNumericMatrix( rows, cols, mxDEFAULT_ARRAY_CLASS, mxREAL );

			copy_pfsarray_to_mex( &c1Buf, plhs[0] );
			copy_pfsarray_to_mex( &c2Buf, plhs[1] );
			copy_pfsarray_to_mex( &c3Buf, plhs[2] );
		} else if( nlhs == 1 ) {
                  mwSize dims[] = { (mwSize)rows, (mwSize)cols, 3 };
			plhs[0] = mxCreateNumericArray( 3, dims, mxDEFAULT_ARRAY_CLASS, mxREAL );

			copy_pfsarray_to_mex( &c1Buf, &c2Buf, &c3Buf, plhs[0] );
		}

    }
    catch( pfs::Exception ex )
    {
        std::stringstream error_message;

        error_message << SCRIPT_NAME << ": " << ex.getMessage();

        error( error_message.str().c_str() );      
    }    
}

static pfs::ColorSpace findColorSpace( const char *name )
{
    if( !strcasecmp( name, "XYZ" ) ) return pfs::CS_XYZ;
    else if( !strcasecmp( name, "RGB" ) ) return pfs::CS_RGB;
    else if( !strcasecmp( name, "SRGB" ) ) return pfs::CS_SRGB;
    else if( !strcasecmp( name, "YUV" ) ) return pfs::CS_YUV;
    else if( !strcasecmp( name, "YXY" ) ) return pfs::CS_Yxy;
    else if( !strcasecmp( name, "RGB2020" ) ) return pfs::CS_RGB2020;
    else if( !strcasecmp( name, "PQYCbCr2020" ) ) return pfs::CS_PQYCbCr2020;
    else if( !strcasecmp( name, "HLGYCbCr2020") ) return pfs::CS_HLGYCbCr2020;
    else if( !strcasecmp( name, "YCbCr709" ) ) return pfs::CS_YCbCr709;

    throw pfs::Exception( "Not recognized color space" );
}