title: In Flutter, how to manipulate pixel by pixel of an ui.Image without using the Image package?
body: <p>in Flutter what's the correct way to change pixel data of an image with ui.Image type without using the Image package ?  I was able to update the pixels by using the Image package but I don't want to convert the image multiple times. So I'm trying to explore the possibility to update Uint8List data. Below is the code snippet. However, I got &quot;Exception: Invalid image data&quot; when I tried to update the image with manipulated Uint8List. Wonder what I did wrong ? Appreciate any feedback.</p>
  <pre><code>int clamp(int x, int a, int b) {
      return (x &lt; a)
          ? a
          : (x &gt; b)
              ? b
              : x;
    }
  
  int getColorY(int a, int r, int g, int b ) =&gt;
        (clamp(a, 0, 255) &lt;&lt; 24) |
        (clamp(r, 0, 255) &lt;&lt; 16) |
        (clamp(g, 0, 255) &lt;&lt; 8) |
        (clamp(b, 0, 255));
  
   Future&lt;ui.Image&gt; setImageData(ui.Image uiX) async 
  
      int w = uiX.width;
      int h = uiX.height;
  
      //get byteData
  
      final rgbaImageData =
             await uiX.toByteData(format: ui.ImageByteFormat.png);
  
      // convert to Uint32
  
      Uint32List words = Uint32List.view(
          rgbaImageData.buffer,
          rgbaImageData.offsetInBytes,
          rgbaImageData.lengthInBytes ~/ Uint32List.bytesPerElement);
     
      int a = 0;
      int r = 0;
      int g = 0;
      int b = 0;
  
      for (int idx = 0; idx &lt; words.length; idx++) {
        Color color = Color(words[idx]);
  
        if (color.red &gt; 128) {
           a = 0;
        } else {
          r = 128;
          g = 135;
          b = 110;
     
        }
        words[idx] = getColorY(a, r, g, b);
      }
      //convert Uint32List to Uint8List 
  
      Uint8List bytes = words.buffer.asUint8List();
     
      final Completer&lt;ui.Image&gt; imageCompleter = new Completer();
      ui.decodeImageFromList(bytes, (ui.Image img) {
        imageCompleter.complete(img);
      });
      return imageCompleter.future;
    }
  </code></pre>