~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/common/colorspaces.c

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-07-12 09:36:46 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110712093646-yp9dbxan44dmw15h
Tags: 0.9-1
* New upstream release.
* Remove all patches now upstream; only patch for
  -Wno-error=unused-but-set-variable remains.
* Bump Standards-Version to 3.9.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
127
127
    memcpy(tmp, matrix, sizeof(float)*9);
128
128
    if(mat3inv (matrix, tmp)) return 3;
129
129
    // also need to reverse gamma, to apply reverse before matrix multiplication:
130
 
    cmsToneCurve* rev_red   = cmsReverseToneCurveEx(0xffff, red_curve);
131
 
    cmsToneCurve* rev_green = cmsReverseToneCurveEx(0xffff, green_curve);
132
 
    cmsToneCurve* rev_blue  = cmsReverseToneCurveEx(0xffff, blue_curve);
 
130
    cmsToneCurve* rev_red   = cmsReverseToneCurveEx(0x8000, red_curve);
 
131
    cmsToneCurve* rev_green = cmsReverseToneCurveEx(0x8000, green_curve);
 
132
    cmsToneCurve* rev_blue  = cmsReverseToneCurveEx(0x8000, blue_curve);
133
133
    if(!rev_red || !rev_green || !rev_blue)
134
134
    {
135
135
      cmsFreeToneCurve(rev_red);
618
618
    if(sqlite3_step(stmt) == SQLITE_ROW)
619
619
    {
620
620
      params = sqlite3_column_blob(stmt, 0);
621
 
      strncpy(profile, params->iccprofile, 1024);
 
621
      g_strlcpy(profile, params->iccprofile, 1024);
622
622
    }
623
623
    sqlite3_finalize(stmt);
624
624
  }
625
625
  if(!overprofile && profile[0] == '\0')
626
 
    strncpy(profile, "sRGB", 1024);
627
 
  if(profile[0] == '\0')
628
 
    strncpy(profile, overprofile, 1024);
629
 
  g_free(overprofile);
 
626
  {
 
627
    g_strlcpy(profile, "sRGB", 1024);
 
628
  }
 
629
  else if(profile[0] == '\0')
 
630
  {
 
631
    g_strlcpy(profile, overprofile, 1024);
 
632
  }
 
633
 
 
634
  if(overprofile)
 
635
  {
 
636
    g_free(overprofile);
 
637
  }
630
638
 
631
639
  cmsHPROFILE output = NULL;
632
640
 
654
662
cmsHPROFILE
655
663
dt_colorspaces_create_cmatrix_profile(float cmatrix[3][4])
656
664
{
657
 
  cmsCIExyY D65;
658
 
  float x[3], y[3];
659
665
  float mat[3][3];
660
666
  // sRGB D65, the linear part:
661
667
  const float rgb_to_xyz[3][3] =
670
676
      mat[c][j] = 0;
671
677
      for(int k=0; k<3; k++) mat[c][j] += rgb_to_xyz[c][k]*cmatrix[k][j];
672
678
    }
 
679
  return dt_colorspaces_create_xyzmatrix_profile(mat);
 
680
}
 
681
 
 
682
cmsHPROFILE
 
683
dt_colorspaces_create_xyzimatrix_profile(float mat[3][3])
 
684
{
 
685
  // mat: xyz -> cam
 
686
  float imat[3][3];
 
687
  mat3inv ((float *)imat, (float *)mat);
 
688
  return dt_colorspaces_create_xyzmatrix_profile(imat);
 
689
}
 
690
 
 
691
cmsHPROFILE
 
692
dt_colorspaces_create_xyzmatrix_profile(float mat[3][3])
 
693
{
 
694
  // mat: cam -> xyz
 
695
  cmsCIExyY D65;
 
696
  float x[3], y[3];
673
697
  for(int k=0; k<3; k++)
674
698
  {
675
699
    const float norm = mat[0][k] + mat[1][k] + mat[2][k];
742
766
    for(e=makermodel; c<maker+strlen(maker) && *c != ' '; c++,e++) *e = *c;
743
767
    // separate with space
744
768
    *(e++) = ' ';
745
 
    // and continue with model:
746
 
    snprintf(e, size - (d-maker), "%s", model);
 
769
    // and continue with model.
 
770
    // replace MAXXUM with DYNAX for wb presets.
 
771
    if(!strcmp(maker, "MINOLTA") && !strncmp(model, "MAXXUM", 6))
 
772
      snprintf(e, size - (d-maker), "DYNAX %s", model+7);
 
773
    else snprintf(e, size - (d-maker), "%s", model);
747
774
  }
748
775
  // strip trailing spaces
749
776
  e = makermodel + strlen(makermodel) - 1;
761
788
  (*modelo)++;
762
789
}
763
790
 
764
 
void rgb2hsl(float r,float g,float b,float *h,float *s,float *l)
 
791
void rgb2hsl(const float rgb[3],float *h,float *s,float *l)
765
792
{
 
793
  const float r=rgb[0], g=rgb[1], b=rgb[2];
766
794
  float pmax=fmax(r,fmax(g,b));
767
795
  float pmin=fmin(r,fmin(g,b));
768
796
  float delta=(pmax-pmin);
769
797
 
770
 
  *h=*s=*l=0;
771
 
  *l=(pmin+pmax)/2.0;
 
798
  float hv=0,sv=0,lv=(pmin+pmax)/2.0;
772
799
 
773
800
  if(pmax!=pmin)
774
801
  {
775
 
    *s=*l<0.5?delta/(pmax+pmin):delta/(2.0-pmax-pmin);
 
802
    sv=lv<0.5?delta/(pmax+pmin):delta/(2.0-pmax-pmin);
776
803
 
777
 
    if(pmax==r) *h=(g-b)/delta;
778
 
    if(pmax==g) *h=2.0+(b-r)/delta;
779
 
    if(pmax==b) *h=4.0+(r-g)/delta;
780
 
    *h/=6.0;
781
 
    if(*h<0.0) *h+=1.0;
782
 
    else if(*h>1.0) *h-=1.0;
 
804
    if(pmax==r) hv=(g-b)/delta;
 
805
    else if(pmax==g) hv=2.0+(b-r)/delta;
 
806
    else if(pmax==b) hv=4.0+(r-g)/delta;
 
807
    hv/=6.0;
 
808
    if(hv<0.0) hv+=1.0;
 
809
    else if(hv>1.0) hv-=1.0;
783
810
  }
 
811
  *h=hv;
 
812
  *s=sv;
 
813
  *l=lv;
784
814
}
785
815
 
786
 
void hue2rgb(float m1,float m2,float hue,float *channel)
 
816
static inline float hue2rgb(float m1,float m2,float hue)
787
817
{
788
818
  if(hue<0.0) hue+=1.0;
789
819
  else if(hue>1.0) hue-=1.0;
790
820
 
791
 
  if( (6.0*hue) < 1.0) *channel=(m1+(m2-m1)*hue*6.0);
792
 
  else if((2.0*hue) < 1.0) *channel=m2;
793
 
  else if((3.0*hue) < 2.0) *channel=(m1+(m2-m1)*((2.0/3.0)-hue)*6.0);
794
 
  else *channel=m1;
 
821
  if( hue < 1.0/6.0) return (m1+(m2-m1)*hue*6.0);
 
822
  else if(hue < 1.0/2.0) return m2;
 
823
  else if(hue < 2.0/3.0) return (m1+(m2-m1)*((2.0/3.0)-hue)*6.0);
 
824
  else return m1;
795
825
}
796
826
 
797
 
void hsl2rgb(float *r,float *g,float *b,float h,float s,float l)
 
827
void hsl2rgb(float rgb[3],float h,float s,float l)
798
828
{
799
829
  float m1,m2;
800
 
  *r=*g=*b=l;
801
 
  if( s==0) return;
 
830
  if( s==0) {
 
831
    rgb[0]=rgb[1]=rgb[2]=l;
 
832
    return;
 
833
  }
802
834
  m2=l<0.5?l*(1.0+s):l+s-l*s;
803
835
  m1=(2.0*l-m2);
804
 
  hue2rgb(m1,m2,h +(1.0/3.0), r);
805
 
  hue2rgb(m1,m2,h, g);
806
 
  hue2rgb(m1,m2,h - (1.0/3.0), b);
 
836
  rgb[0] = hue2rgb(m1,m2,h + (1.0/3.0));
 
837
  rgb[1] = hue2rgb(m1,m2,h);
 
838
  rgb[2] = hue2rgb(m1,m2,h - (1.0/3.0));
807
839
 
808
840
}
 
841
 
 
842
// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-space on;