Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将JPEG从RGB转换为HSL_C_Rgb_Libjpeg_Hsl - Fatal编程技术网

在C语言中将JPEG从RGB转换为HSL

在C语言中将JPEG从RGB转换为HSL,c,rgb,libjpeg,hsl,C,Rgb,Libjpeg,Hsl,我正在尝试将JPEG从RGB转换为HSL。我制作了一个读写函数,用于打开文件(我使用libjpeg函数),并从这张图片的HSL值(色调、饱和度、亮度)绘制ASCII图 所以我找到了一个将RGB转换为HSL的伪代码: 我把它转换成C。 我的问题是:现在如何将每个像素逐个从RGB转换为HSL?如果我在“写入”中使用这个RGB_to_HSL函数,并将其应用于缓冲区数组的每个框,它会起作用吗 main.c: #include <stdio.h> #include <stdlib.h&g

我正在尝试将JPEG从RGB转换为HSL。我制作了一个读写函数,用于打开文件(我使用libjpeg函数),并从这张图片的HSL值(色调、饱和度、亮度)绘制ASCII图 所以我找到了一个将RGB转换为HSL的伪代码: 我把它转换成C。 我的问题是:现在如何将每个像素逐个从RGB转换为HSL?如果我在“写入”中使用这个RGB_to_HSL函数,并将其应用于缓冲区数组的每个框,它会起作用吗

main.c

#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
#include "fonctions.h"


int main (int argc, char** argv){


    int H;
    int W;
    int C;
    FILE *fichier = NULL; //file pour l'image entrée
    FILE *image = NULL; //file pou l'image à la sortie
    unsigned char **buffer; //buffer où sera contenue l'image

    buffer = malloc(256*(sizeof(unsigned char*)));

    if (argv[1] == NULL)
        fichier = fopen("cara.jpg", "r");
    else
        fichier = fopen(argv[1], "r");
    image = fopen("cara_image_cree.jpg", "wb");

    if (fichier == NULL)
        printf("Probleme lecture");

    printf("Cara Delevingne\n");
    buffer = lire(fichier, &H, &W, &C);
    ecrire(&H, &W, &C, buffer, image);

    fclose(fichier);
    fclose(image);
    return 0;
}   
#包括
#包括
#包括
#包括“fonctions.h”
int main(int argc,字符**argv){
int-H;
int W;
INTC;
FILE*fichier=NULL;//文件倒l'image entrée
FILE*image=NULL;//文件pou l'imageála出击
无符号字符**缓冲区;//缓冲区oùsera contenue l'image
buffer=malloc(256*(sizeof(unsigned char*));
如果(argv[1]==NULL)
fichier=fopen(“cara.jpg”,“r”);
其他的
fichier=fopen(argv[1],“r”);
image=fopen(“cara_image_cree.jpg”、“wb”);
if(fichier==NULL)
printf(“问题讲座”);
printf(“Cara Delevingne”);
缓冲区=里尔(菲希尔、H、W、C);
ecrire(H、W、C、缓冲区、图像);
fclose(fichier);
fclose(图像);
返回0;
}   
读取.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <jpeglib.h>
#include <jerror.h>

unsigned char** lire (FILE* file, int *H, int *W, int *C){

struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

int n = 0;
unsigned char** buffer; // buffer qui va contenir l'image

/*printf("SHITSHITSHITSHITDEBUG\n");
fflush(stdout);*/
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo); // Initialisation de la structure

jpeg_stdio_src(&cinfo,file);  // file est de type FILE * (descripteur de fichier
                              // sur le fichier jpega decompresser)
jpeg_read_header(&cinfo,TRUE);// lecture des infos sur l'image jpeg


jpeg_start_decompress(&cinfo);// lancement du processus de decompression


*H = cinfo.output_height; // on récupère la hauteur
*W = cinfo.output_width; // on récupère la largeur
*C = cinfo.output_components; // on regarde si l'image est en couleurs ou N&B


buffer=malloc( (*H) *sizeof(unsigned char*) ); // on alloue de la mémoire au buffer selon le nb de lignes de pixels qu'il va devoir prendre

while (n < *H) // tant que le compteur n'a pas dépassé l'image
 {
    buffer[n] = (unsigned char*) malloc( (*W) * (*C) *sizeof(unsigned char *) ); // on alloue à chaque ligne, la taille de la largeur

     jpeg_read_scanlines(&cinfo,buffer+n,1); // lecture des n lignes suivantes de l'image
                                          // dans le buffer (de type unsigned char *)
     n++;
}

jpeg_finish_decompress(&cinfo);

jpeg_destroy_decompress(&cinfo);

return buffer;
}
#包括
#包括
#包括
#包括
#包括
无符号字符**lire(文件*FILE,int*H,int*W,int*C){
结构jpeg\u解压缩\u结构cinfo;
结构jpeg\u错误\u管理器jerr;
int n=0;
无符号字符**buffer;//buffer qui va contenir l'image
/*printf(“SHITSHITSHITSHITDEBUG\n”);
fflush(stdout)*/
cinfo.err=jpeg\u std\u error(&jerr);
jpeg_create_decompresse(&cinfo);//初始化结构
jpeg_stdio_src(&cinfo,file);//file est de type file*(descripteur de fichier)
//菲希尔河畔(JPEG A解压缩器)
jpeg_read_header(&cinfo,TRUE);//图像上的信息讲座jpeg
jpeg_start_decompress(&cinfo);//解压过程的图像
*H=cinfo.output_height;//在récupère la auteur上
*W=cinfo.output_width;//在récupère la largeur上
*C=cinfo.output_components;//在N&B中的regarde si l'image上
buffer=malloc((*H)*sizeof(unsigned char*);//关于mémoire au buffer selon le nb de alignes de pixels qu'il va devoir prendre
而(n<*H)//n'a pas dépassél'image
{
buffer[n]=(无符号字符*)malloc((*W)*(*C)*sizeof(无符号字符*);//在大尾巴上的一条直线上
jpeg_read_scanlines(&cinfo,buffer+n,1);//图像对齐教程
//dans le缓冲区(de类型无符号字符*)
n++;
}
jpeg_finish_解压缩(&cinfo);
jpeg\u destroy\u解压缩(&cinfo);
返回缓冲区;
}
write.c

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <jpeglib.h>
#include <jerror.h>

void ecrire (int *H, int *W, int *C, unsigned char **buffer, FILE *file){

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

int n = 0;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo); // Initialisation de la structure

jpeg_stdio_dest(&cinfo,file);  // file est de type FILE * (descripteur de fichier
                              // sur le fichier jpeg compressé final)
cinfo.image_width= *W;          // nombre de ligne de l'image
cinfo.image_height= *H;         // nombre de pixel par ligne
cinfo.input_components = *C;      // 3 pour une image couleur, 1 pour une N&B
cinfo.in_color_space= JCS_RGB;
                              // JCS_GRAYSCALE pour une image N&B
jpeg_set_defaults(&cinfo);    // initialisation des paramètres de compression
jpeg_start_compress(&cinfo,TRUE); // lancement du processus de decompression


while (n < *H)
{
    jpeg_write_scanlines(&cinfo,buffer+n,1); // écriture des n lignes suivantes de l'image
                                          // stockées dans le buffer (de type unsigned char *)
     n++;
}

jpeg_finish_compress(&cinfo);

jpeg_destroy_compress(&cinfo);

}
#包括
#包括
#包括
#包括
#包括
void ecrire(int*H、int*W、int*C、无符号字符**缓冲区、文件*FILE){
结构jpeg\u压缩\u结构cinfo;
结构jpeg\u错误\u管理器jerr;
int n=0;
cinfo.err=jpeg\u std\u error(&jerr);
jpeg_create_compress(&cinfo);//结构的初始化
jpeg_stdio_dest(&cinfo,file);//file est de type file*(descripteur de fichier
//sur le fichier jpeg压缩(最终版)
cinfo.image_width=*W;//图像的名称
图像信息:高度=*H;//像素
cinfo.input_components=*C;//3倒une图像库,1倒une N&B
cinfo.in_color_space=JCS_RGB;
//JCS_灰度图像N&B
jpeg_set_defaults(&cinfo);//压缩参数的初始化
jpeg_start_compress(&cinfo,TRUE);//解压过程中的图像
而(n<*H)
{
jpeg_write_scanlines(&cinfo,buffer+n,1);//n对齐图像的标准
//stockées dans le缓冲区(de类型无符号字符*)
n++;
}
jpeg\u完成\u压缩(&cinfo);
jpeg\u destroy\u compress(&cinfo);
}
RGB_to_HSL.c(我现在没有在任何地方使用它!我不知道我必须在哪里使用它):

void rgbToHsl(int r,int g,int b,int*h,int*s,int*l){
r/=255;g/=255;b/=255;
int max=最大值(r,g,b);
int min=最小值(r,g,b);
*l=(最大+最小)/2;
如果(最大=最小)
*h=*s=0;//消色差
否则{
int d=最大-最小值;
*s=*l>0.5?d/(2-最大-最小):d/(最大+最小);
如果(最大==r)
*h=(g-b)/d+(gb){
如果(a>c)
返回a;
其他的
返回c;
}
否则{
如果(b>c)
返回b;
如果(c>b)
返回c;
}
}
最小整数(整数a、整数b、整数c){
if(a
请原谅我用法语评论这是因为这是为了上大学,我的老师想让我们评论我们的节目(正常)


谢谢你们的帮助。

我不是所有方面的专家,但似乎在解压后,
buffer
中有一组扫描线,其中
buffer[I]
是第I条扫描线(基于零)。扫描线包含各个像素

因为每个像素都是独立于算法中的其他像素进行转换的,所以现在只需将像素转换为HSL:循环扫描线,循环扫描线的像素,转换每个像素,将像素放回扫描线

完成后,您可以将图像写回。我不知道
void rgbToHsl(int r, int g, int b, int *h, int *s, int *l){
    r/=255; g /= 255; b/=255;
    int max = maximum(r, g, b);
    int min = minimum(r, g, b);
    *l = (max + min)/2;

    if (max == min) 
        *h = *s = 0; // achromatique
    else{
        int d = max - min;
        *s = *l > 0.5 ? d / (2 - max - min) : d / (max + min);
        if (max == r)
            *h = (g-b) / d + (g < b ? 6 : 0);
        if (max == g)
            *h = (b-r) / d + 2;
        if (max == b)
            *h = (r-g) / d + 4;

            /*case r: *h = r;
            case g: *h = g;
            case b: *h = b; */
        }
        *h /= 6;





    }

int maximum (int a, int b, int c){
    if (a > b){
        if (a > c)
            return a;
        else
            return c;
    }
    else{
        if (b > c)
            return b;
        if (c > b)
            return c;
    }
}

int minimum (int a, int b, int c){
    if (a < b){
        if (a < c)
            return a;
        else
            return c;
    }
    else{
        if (b < c)
            return b;
        if (c < b)
            return c;
    }


}