【问题标题】:Rewriting JPEG images from RGB to HSL : how can i optimize this?将 JPEG 图像从 RGB 重写为 HSL:我该如何优化?
【发布时间】:2015-11-28 17:34:25
【问题描述】:

我在一个项目中,我必须制作一个从 JPEG 中绘制 ASCII 的程序。 程序会根据一个像素的 HSL 值选择一个字符:Hue, Saturation, Lightness 因此,由于 JPEG 文件是 RGB 格式,我将其转换为 HSL。 但我发现我的程序很慢,它减慢了我的整个虚拟机 lel 你知道我可以如何改进它以使其更快一点吗? 在我的文章中,我调用了将 RGB 转换为 HSL 的函数 在这里:

ma​​in.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);
    /* afficher 3 sous-pixels : 
    printf("\nBuffer case 1 : %d", buffer[0][0]);
    printf("\nBuffer case 1 : %d", buffer[0][0+1]);
    printf("\nBuffer case 1 : %d\n", buffer[0][0+2]);*/
    ecrire(&H, &W, &C, buffer, image);

    fclose(fichier);
    fclose(image);
    return 0;
}   

read.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;
}

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;

    float** bufferHSL;
    int n = 0; // parcoureurs pour écrire l'image

    int i = 0; // parcoureurs pour transformer en HSL
    int j = 0;

    float h = 0; // variables pour stocker le résultat HSL
    float s = 0;
    float l = 0;
    int r, g, b;
    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

    bufferHSL = (float **) malloc( (*H) *sizeof(long int*) );
    while (i < *H){ // lecture des lignes pour transformation HSL   
        j = 0;
        while (j < *W){
            /*printf("i : %d /t j : %d \n",i , j);
            fflush(stdout);*/
            bufferHSL[i] = (float*)malloc( (*W) *sizeof(long int*) );
            r = buffer[i][j];
            g = buffer[i][j+1];
            b = buffer[i][j+2];
            rgbToHsl(r, g, b, &h, &s, &l);

            bufferHSL[i][j] = h;
            bufferHSL[i][j+1] = s;
            bufferHSL[i][j+2] = l;
            j++;
            /*printf("TESTTEST\n");
            fflush(stdout);*/

        }
        i++;
    }


    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);

}

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 = 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;
    }


}

【问题讨论】:

    标签: c jpeg rgb libjpeg hsl


    【解决方案1】:

    您应该运行一些分析工具来准确查看哪些部分需要多长时间来执行和优化。

    如果不这样,快速浏览一下就会发现您在内部循环中多次调用malloc,这里:

    bufferHSL = (float **) malloc( (*H) *sizeof(long int*) );
    while (i < *H){ // lecture des lignes pour transformation HSL   
        j = 0;
        while (j < *W){
            bufferHSL[i] = (float*)malloc( (*W) *sizeof(long int*) );
    

    实际上,这似乎是错误的 - 您在每次 j &lt; *W 迭代时调用它,但每次 i 只需要一次。您正在泄漏分配,并且只在最后一次迭代中对其进行任何操作 - 您确定您的输出正确吗?

    作为第一步,我建议将该行移出内部 while 循环。

    还有一些其他微优化的空间,但我认为您不需要这样做。只要确保代码是正确的,然后找到慢的部分(通常是最里面的循环),然后专注于那个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多