fluttercandies / flutter_image_editor

Flutter plugin, support android/ios.Support crop, flip, rotate, color martix, mix image, add text. merge multi images.
Apache License 2.0
407 stars 124 forks source link

Contrast setting seems like not accurate. #105

Closed JayronKhong closed 1 year ago

JayronKhong commented 1 year ago

instead of this:

 factory ColorOption.contrast(double contrast) {
    final m = List<double>.from(defaultColorMatrix);
    m[0] = contrast;
    m[6] = contrast;
    m[12] = contrast;
    return ColorOption(matrix: m);
  }

should change to this:

  factory ColorOption.contrast(double contrast) {
    final m = List<double>.from(defaultColorMatrix);
    m[0] = contrast;
    m[6] = contrast;
    m[12] = contrast;
    m[4] = (1.0 - (1 + contrast)) / 2.0 * 255;
    m[9] = (1.0 - (1 + contrast)) / 2.0 * 255;
    m[14] = (1.0 - (1 + contrast)) / 2.0 * 255;
    return ColorOption(matrix: m);
  }
AlexV525 commented 1 year ago

Can you identify what spec the change is following?

JayronKhong commented 1 year ago

Hi.. for this file Screenshot 2023-01-17 at 3 36 03 PM

AlexV525 commented 1 year ago

I mean, what specification does the change follow to make the color accurate? Why the previous one is not enough?

JayronKhong commented 1 year ago

Previous code is more tend to brightness setting.

JayronKhong commented 1 year ago

I trying to convert image with contrast filter. But the result after converted is more tend to brightness instead of contrast.

AlexV525 commented 1 year ago

How can I tell your code is right? What underlying knowledge you're using when writing the changing code?

JayronKhong commented 1 year ago

maybe try on it, add contrast color option to the image.

factory ColorOption.scale( double rScale, double gScale, double bScale, double aScale, ) { final a = List.filled(20, 0); a[0] = rScale; a[6] = gScale; a[12] = bScale; a[18] = aScale; return ColorOption(matrix: a); }

factory ColorOption.brightness(double brightness) { return ColorOption.scale(brightness, brightness, brightness, 1); }

factory ColorOption.contrast(double contrast) { final m = List.from(defaultColorMatrix); m[0] = contrast; m[6] = contrast; m[12] = contrast; return ColorOption(matrix: m); }

from what i saw here, brightness and contrast almost similar.

both value from [0], [6], [12]

brightness matrix: bright, 0, 0, 0, 0, 0, bright, 0, 0, 0, 0, 0, bright, 0, 0, 0, 0, 0, 1, 0,

contrast matrix: contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0,

correct me if i'm wrong, thanks.

https://user-images.githubusercontent.com/50298326/212855093-6f3f8238-7b34-45a8-a681-c98454446a41.mp4

sample for above

JayronKhong commented 1 year ago

Hi @AlexV525 sorry, do you tested any issue for the contrast so far?

CaiJingLong commented 1 year ago

Modifying the code is simple.

But Alex means, is there your source for algorithm of contrast color matrix? For example, standard documents, scientific magazines or periodicals, or source code called by the device platform (Android iOS Windows, etc.).

In other words, where is the source connection of your contrast algorithm, rather than whether we use the naked eye to observe whether it is more effective than the current one.

ramtinq commented 1 year ago

well the solution provided here is wrong, because contrast = 1 should give us identity matrix, keeping the original image intact, but the solution does not.

@AlexV525 @CaiJingLong However the current contrast algorithm is not really changing the contrast. If you compare it to the ColorOption.brightness which uses ColorOption.scale, the resulting matrixes used in both factories are completely identical!

Based on this guide:

https://docs.rainmeter.net/tips/colormatrix-guide/

the correct solution is this:

  factory ColorOption.contrast(double contrast) {
    final m = List<double>.from(defaultColorMatrix);
    final double translate = 255.0 * (1 - contrast) / 2
    m[4] = translate;
    m[9] = translate;
    m[14] = translate;
    m[0] = contrast;
    m[6] = contrast;
    m[12] = contrast;
    return ColorOption(matrix: m);
  }
CaiJingLong commented 1 year ago

Closed by #107.

gunantosteven commented 3 months ago

Hi guys, I found the issue with iOS version. The result only black or white except this value 1.0. Could you check it ?