有人能明白为什么我这里有浮点异常吗?

标签 c rgb libjpeg hsl

我在谷歌上看到它是关于除以零的,但我不知道在哪里或仅在 d = 0 和 *l<=0.5 的情况下......(第 15 行) 该函数用于将 JPEG 文件中像素的 RGB 值转换为 HSL 值(色调、饱和度、亮度)

#include <stdio.h>
float rgbToHsl(int r1, int g1, int b1, float *h, float *s, float *l){

    float r = r1;
    float g = g1;
    float b = b1;
    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;
    //printf("r: %f, g : %f, b : %f",r,g,b);

return *h;



}

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


}

谢谢大家

编辑:按照您的要求添加了我的项目的其余部分:它是将 jpeg 文件的 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
    JSAMPARRAY buffer; //buffer où sera contenue l'image

    buffer = malloc(256*(sizeof(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);
        printf("Cara222\n");
        fflush(stdout);
    fclose(fichier);
    fclose(image);
    return 0;
}   

读取.c:

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

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

    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;

    int n = 0;
    JSAMPARRAY 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 jpeg a 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=(JSAMPARRAY) malloc( (*H) *sizeof(JSAMPARRAY)); // on alloue de la mémoire au buffer selon le nb de lignes de pixels qu'il va devoir prendre
                                            // éventuellement remplacer par unsigned char** et caster en JSAMPARRAY ou uchar**

    while (n < (*H) ) // tant que le compteur n'a pas dépassé l'image
    {
        buffer[n] = (unsigned char*) malloc( (*W)*(*C)*sizeof(char)); // on alloue à chaque ligne, la taille de la largeur
        /*printf("CARA44 n : %d\n", n);
        fflush(stdout);*/
        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;
}

写.c:

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



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

    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

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

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

    float h = 1; // variables pour stocker le résultat HSL
    float s = 0;
    float l = 0;
    float v = 2.83223244;
    int r, g, b;

    int e = 0; // variables de vérification
    int f = 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

    bufferHSL = (float *) malloc( (*H)*(*W) *(*C)*sizeof(long int) );
    verif = (char*)malloc((*H)*(*W) *(*C) *sizeof(char) );
    while (i < *H-3){ 
        j = 0;
        while (j < *W-3) { // lecture des lignes pour transformation HSL    
                //bufferHSL[j] = (float*)malloc( (*W) *sizeof(long int) );
                r = buffer[i][j];
                g = buffer[i][j+1];
                b = buffer[i][j+2];
                //printf("Valeur du buffer : %d\n", buffer[i][j]);
                //printf("r: %d, g : %d, b : %d\n",r,g,b);
                rgbToHsl(r, g, b, &h, &s, &l); // converting the red blue green pixels into HSL
                bufferHSL[k] = h;
                bufferHSL[k+1] = s;
                bufferHSL[k+2] = l;
                //printf("Valeur de hsl : h=%f s=%f l=%f \n", h,s,l);
                k=k+3;
                j=j+3;
        }
        i++;
    }
    printf("coucou");

    verif = convhsl2rgb(bufferHSL, *W, *H); //converting back the jpeg in HSL into rgb for verification.

    k = 0;

    while(e < *H){ //Double while for putting the unsign char* buffer into unsigned char** bc JSAMPARRAY is uchar **
        f = 0;
        while (f < *W){
            //printf("Valeur de hsl : %f et buffer : %d\n", bufferHSL[k], buffer[e][f]);
            buffer[e][f] = verif[k];
            f++;
            k++;
        }
        e++;

    }



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

}

就这些了

最佳答案

我认为你在函数最大值和最小值上犯了一个错误。在 else 子句中,有 2 个 if:s 而不是 if-else。当 b==c 时你就会遇到麻烦。更准确地说,您可能在没有显式 return 语句的情况下返回。

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;
        else
            return c;
    }
}

关于有人能明白为什么我这里有浮点异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34019102/

相关文章:

C++:openCV 错误

在windows上编译pyd文件但是 "This program cannot be run in DOS mode."

c++ - 错误 LNK2019 : unresolved external symbol (libJPEG compiling and linking)

c - 分配 3 维数组

无法在 ubuntu : Could not find platform independent libraries 上运行 gdb

编译共享库链接到其他.so

binary - 解释 PNG 像素数据

c - 输入重定向GDB

c - 返回指向结构的指针后出现段错误

c - FFMPEG使用sws_scale将YCbCr转换为RGB