📄 lcolor.c
字号:
/*----------------------------------------------------------------------------
// StirMark Benchmark - lcolor.c
//
// Contents: Colour space conversion routines
//
// Purpose:
//
// Created: C. Rey, G. Do雛r, J.-L. Dugelay and G. Csurka, Eur閏om, January 2002
//
// Modified:
//
// History:
//
// Copyright (c) 2000-2002, Microsoft Research Ltd , Institut National
// de Recherche en Informatique et Automatique (INRIA), Institut Eur閏om
// and the Integrated Publication and Information Systems Institute at
// GMD - Forschungszentrum Informationstechnik GmbH (GMD-IPSI).
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted for non-commercial research and academic
// use only, provided that the following conditions are met:
//
// - Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. Each
// individual file must retain its own copyright notice.
//
// - Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions, the following disclaimer and the
// list of contributors in the documentation and/or other materials
// provided with the distribution.
//
// - Modification of the program or portion of it is allowed provided
// that the modified files carry prominent notices stating where and
// when they have been changed. If you do modify this program you
// should send to the contributors a general description of the changes
// and send them a copy of your changes at their request. By sending
// any changes to this program to the contributors, you are granting a
// license on such changes under the same terms and conditions as
// provided in this license agreement. However, the contributors are
// under no obligation to accept your changes.
//
// - All non-commercial advertising materials mentioning features or use
// of this software must display the following acknowledgement:
//
// This product includes software developed by Microsoft Research
// Ltd, Institut National de Recherche en Informatique et Automatique
// (INRIA), Institut Eur閏om and the Integrated Publication and
// Information Systems Institute at GMD - Forschungszentrum
// Informationstechnik GmbH (GMD-IPSI).
//
// - Neither name of Microsoft Research Ltd, INRIA, Eur閏om and GMD-IPSI
// nor the names of their contributors may be used to endorse or
// promote products derived from this software without specific prior
// written permission.
//
// - If you use StirMark Benchmark for your research, please cite:
//
// Fabien A. P. Petitcolas, Martin Steinebach, Fr閐閞ic Raynal, Jana
// Dittmann, Caroline Fontaine, Nazim Fat鑣. A public automated
// web-based evaluation service for watermarking schemes: StirMark
// Benchmark. In Ping Wah Wong and Edward J. Delp, editors,
// proceedings of electronic imaging, security and watermarking of
// multimedia contents III, vol. 4314, San Jose, California, U.S.A.,
// 20-26 January 2001. The Society for imaging science and
// technology (I.S.&T.) and the international Society for optical
// engineering (SPIE). ISSN 0277-786X.
//
// and
//
// Fabien A. P. Petitcolas. Watermarking schemes
// evaluation. I.E.E.E. Signal Processing, vol. 17, no. 5,
// pp. 58-64, September 2000.
//
// THIS SOFTWARE IS NOT INTENDED FOR ANY COMMERCIAL APPLICATION AND IS
// PROVIDED BY MICROSOFT RESEARCH LTD, INRIA, EUR蒀OM, GMD-IPSI AND
// CONTRIBUTORS 'AS IS', WITH ALL FAULTS AND ANY EXPRESS OR IMPLIED
// REPRESENTATIONS OR WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE, TITLE OR NONINFRINGEMENT OF INTELLECTUAL
// PROPERTY ARE DISCLAIMED. IN NO EVENT SHALL MICROSOFT RESEARCH LTD,
// INRIA, EUR蒀OM, GMD-IPSI OR THEIR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// THE USE OF THIS SOFTWARE FOR CIRCUMVENTING WITHOUT AUTHORITY ANY
// EFFECTIVE TECHNOLOGICAL MEASURES DESIGNED TO PROTECT ANY COPYRIGHTS OR
// ANY RIGHTS RELATED TO COPYRIGHT AS PROVIDED BY LAW OR THE SUI GENERIS
// RIGHT PROVIDED BY SOME COUNTRIES IS STRICTLY PROHIBITED.
//
// $Header: /home/cvs/StirmarkBench/StirMark_Bench/SignalProcessing/SelfSimilarities/lcolor.c,v 1.2 2002/04/19 10:23:58 petitcolas Exp $
//----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "SelfSimilarities.h"
#include "lib_kadv_codec.h"
/*--------------------------------------------------------------------------------*
| This function converts the image from the RBG to the specified color space. |
*--------------------------------------------------------------------------------*/
void convertRGB2Channel(image *R, image *G, image *B, image **channel1, image **channel2,
image **channel3, int width, int height, int color_space){
int i;
if (color_space == SS_RGB){
if((*channel1 = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
if((*channel2 = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
if((*channel3 = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
for (i=0;i<width*height;i++){
(*channel1)[i] = R[i];
(*channel2)[i] = G[i];
(*channel3)[i] = B[i];
}
}
else if (color_space == SS_YUV){
printf("Color space mapping: RGB -> YUV\n");
ConvertRGB2YUV(R, G, B, channel1, channel2, channel3, width, height);
}
else if (color_space == SS_HSV) {
printf("Color space mapping: RGB -> HSV\n");
ConvertRGB2HSV(R, G, B, channel1, channel2, channel3, width, height);
}
else if (color_space == SS_LAB) {
printf("Color space mapping: RGB -> Lab\n");
ConvertRGB2Lab(R, G, B, channel1, channel2, channel3, width, height);
}
else if (color_space == SS_XYZ) {
printf("Color space mapping: RGB -> XYZ\n");
ConvertRGB2XYZ(R, G, B, channel1, channel2, channel3, width, height);
}
}
/*--------------------------------------------------------------------------------*
| This function converts the image from the specified to the RGB color space. |
*--------------------------------------------------------------------------------*/
void convertChannel2RGB(image *channel1, image *channel2, image *channel3, image **R,
image **G, image **B, int width, int height, int color_space){
int i;
if (color_space == SS_RGB){
if((*R = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
if((*G = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
if((*B = (image *)malloc(sizeof(image) * width * height))==NULL) {
fprintf(stderr,"ERROR: Memory allocation problem!\n");
exit(1);
}
for (i=0;i<width*height;i++){
(*R)[i] = channel1[i];
(*G)[i] = channel2[i];
(*B)[i] = channel3[i];
}
}
else if (color_space == SS_YUV){
printf("Color space mapping: YUV -> RGB\n");
ConvertYUV2RGB(channel1, channel2, channel3, R, G, B, width, height);
}
else if (color_space == SS_HSV){
printf("Color space mapping: HSV -> RGB\n");
ConvertHSV2RGB(channel1, channel2, channel3, R, G, B, width, height);
}
else if (color_space == SS_LAB){
printf("Color space mapping: Lab -> RGB\n");
ConvertLab2RGB(channel1, channel2, channel3, R, G, B, width, height);
}
else if (color_space == SS_XYZ){
printf("Color space mapping: XYZ -> RGB\n");
ConvertXYZ2RGB(channel1, channel2, channel3, R, G, B, width, height);
}
}
/*---------------------------------------------------------------------------
// RGB <=> YUV transform
*/
void ConvertRGB2YUV(image *R, image *G, image *B, image **Y, image **U, image **V, int width, int height){
int i;
(*Y) = (image *) malloc(sizeof(image)*width*height);
(*U) = (image *) malloc(sizeof(image)*width*height);
(*V) = (image *) malloc(sizeof(image)*width*height);
for (i=0;i<width*height;i++){
(*Y)[i] = (image) RoundInt( (double)(RGB_TO_YUV_MATRIX[0][0] * R[i])
+ (double)(RGB_TO_YUV_MATRIX[0][1] * G[i])
+ (double)(RGB_TO_YUV_MATRIX[0][2] * B[i]) );
(*U)[i] = (image) RoundInt( (double)(RGB_TO_YUV_MATRIX[1][0] * R[i])
+ (double)(RGB_TO_YUV_MATRIX[1][1] * G[i])
+ (double)(RGB_TO_YUV_MATRIX[1][2] * B[i]) );
(*V)[i] = (image) RoundInt( (double)(RGB_TO_YUV_MATRIX[2][0] * R[i])
+ (double)(RGB_TO_YUV_MATRIX[2][1] * G[i])
+ (double)(RGB_TO_YUV_MATRIX[2][2] * B[i]) );
}
}
void ConvertYUV2RGB(image *Y, image *U, image *V, image **R, image **G, image **B, int width, int height){
int i;
(*R) = (image *) malloc(sizeof(image)*width*height);
(*G) = (image *) malloc(sizeof(image)*width*height);
(*B) = (image *) malloc(sizeof(image)*width*height);
for (i=0;i<width*height;i++){
(*R)[i] = (image) RoundInt( (double)(YUV_TO_RGB_MATRIX[0][0] * Y[i])
+ (double)(YUV_TO_RGB_MATRIX[0][1] * U[i])
+ (double)(YUV_TO_RGB_MATRIX[0][2] * V[i]) );
(*G)[i] = (image) RoundInt( (double)(YUV_TO_RGB_MATRIX[1][0] * Y[i])
+ (double)(YUV_TO_RGB_MATRIX[1][1] * U[i])
+ (double)(YUV_TO_RGB_MATRIX[1][2] * V[i]) );
(*B)[i] = (image) RoundInt( (double)(YUV_TO_RGB_MATRIX[2][0] * Y[i])
+ (double)(YUV_TO_RGB_MATRIX[2][1] * U[i])
+ (double)(YUV_TO_RGB_MATRIX[2][2] * V[i]) );
}
}
/*---------------------------------------------------------------------------
// RGB <=> HSV transform
// From Alvy Ray Smith, Color Gamut Transform
*/
void ConvertRGB2HSV(image *R, image *G, image *B, image **H, image **S, image **V, int width, int height){
int i;
image min, max, s;
float r, g, b;
float h = 0.0;
(*H)=(image *)malloc(sizeof(image)*width*height);
(*V)=(image *)malloc(sizeof(image)*width*height);
(*S)=(image *)malloc(sizeof(image)*width*height);
for (i=0;i<width*height;i++){
max = R[i];
min = R[i];
if (G[i] > max)
max = G[i];
else if (G[i] < min)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -