public static void Main() { Debug.Print(Resources.GetString(Resources.StringResources.String1)); Netduino.Foundation.Network.Initializer.NetworkConnected += Initializer_NetworkConnected; Netduino.Foundation.Network.Initializer.InitializeNetwork(); var led = new OutputPort(Pins.ONBOARD_LED, false); Debug.Print("InitializeNetwork()"); var nano = new NanoJPEG(); nano.njDecode(new byte[4]); var img = nano.njGetImage(); while (true) { led.Write(true); Thread.Sleep(BlinkRate); led.Write(false); Thread.Sleep(BlinkRate); } }
void JpegTest() { Console.WriteLine("JpgTest"); var jpgData = LoadResource("house.jpg"); Console.WriteLine($"Loaded {jpgData.Length} bytes"); var nanoJpeg = new NanoJPEG(); nanoJpeg.njDecode(jpgData); Console.WriteLine("Jpeg decoded"); var jpg = nanoJpeg.GetImage(); Console.WriteLine($"Jpeg decoded is {jpg.Length} bytes"); Console.WriteLine($"Width {nanoJpeg.Width}"); Console.WriteLine($"Height {nanoJpeg.Height}"); graphics.Clear(); int x = 0; int y = 0; byte r, g, b; for (int i = 0; i < jpg.Length; i += 3) { r = jpg[i]; g = jpg[i + 1]; b = jpg[i + 2]; display.DrawPixel(x, y, r, g, b); x++; if (x % nanoJpeg.Width == 0) { y++; x = 0; } } Console.WriteLine("Jpeg show"); display.Show(); }
void JpegTest(byte[] data) { var nanoJpeg = new NanoJPEG(); nanoJpeg.njDecode(data); Console.WriteLine("Jpg decoded"); var jpg = nanoJpeg.GetImage(); Console.WriteLine($"Jpeg decoded is {jpg.Length} bytes"); Console.WriteLine($"Width {nanoJpeg.Width}"); Console.WriteLine($"Height {nanoJpeg.Height}"); graphics.Clear(); int x = 0; int y = 0; byte r, g, b; for (int i = 0; i < jpg.Length; i += 3) { r = jpg[i]; g = jpg[i + 1]; b = jpg[i + 2]; display.DrawPixel(x, y, r, g, b); x++; if (x % 240 == 0) { y++; x = 0; } if (y >= 135) { break; } } Console.WriteLine("Jpeg show"); display.Show(); }
protected void ConstructRawTexture(string id, BinaryReader br, int size) { if (_rawTextureCache.ContainsKey(id) == false) { byte[] data = br.ReadBytes(size); NanoJPEG nanoJPEG = new NanoJPEG(); nanoJPEG.njDecode(data); byte[] rawPixels = nanoJPEG.njGetImage(); if (rawPixels != null && rawPixels.Length > 0 && rawPixels.Length == nanoJPEG.njGetWidth() * nanoJPEG.njGetHeight() * 3) { RawTexture texture = new RawTexture(); texture.ImgData = rawPixels; texture.Width = nanoJPEG.njGetWidth(); texture.Height = nanoJPEG.njGetHeight(); _rawTextureCache.Add(id, texture); } } }
/// <summary> /// Decodes the image from the specified stream and sets the data to image. /// </summary> /// <param name="Image">The image, where the data should be set to. Cannot be null (Nothing in Visual Basic).</param> /// <param name="Stream">The stream, where the image should be decoded from. Cannot be null (Nothing in Visual Basic).</param> public void Decode(ExtendedImage Image, Stream Stream) { // Initialize the input. byte[] Input = Stream is MemoryStream ? ((MemoryStream)Stream).ToArray() : null; // Initialize a new instance of the NanoJPEG class. NanoJPEG NanoJPEG = new NanoJPEG(); // Check if the input is invalid. if (Input == null) { // Initialize a new instance of the MemoryStream class. using (MemoryStream MemoryStream = new MemoryStream()) { // Initialize the buffer. byte[] Buffer = new byte[16 * 1024]; // Initialize the number of bytes read. int Read; // Read bytes and check if reading was successful. while ((Read = Stream.Read(Buffer, 0, Buffer.Length)) > 0) { // Write the read bytes. MemoryStream.Write(Buffer, 0, Read); } // Set the input. Input = MemoryStream.ToArray(); } } // Decode the image. if (NanoJPEG.njDecode(Input) == nj_result_t.NJ_OK) { // Initialize the status indicating color. bool IsColor = NanoJPEG.njIsColor(); // Initialize the RGB-formatted image.. byte[] Output = NanoJPEG.njGetImage(); // Initialzie the iterator. int i = Output.Length - 3, Height = NanoJPEG.njGetHeight(), Width = NanoJPEG.njGetWidth(); // Initialize NanoJPEG to dispose of copies. NanoJPEG.njInit(); // Check if the image has color. if (IsColor) { // Resize the RGB-formatted image to accommendate RGBA-formatting. Array.Resize(ref Output, Output.Length * 4 / 3); // Iterate through each pixel in the RGB-formatting. for (int j = Output.Length - 4; i >= 0; i -= 3, j -= 4) { // Change the pixel from RGB-formatting to RGBA-formatting. Buffer.BlockCopy(Output, i, Output, j, 3); // Set the alpha channel for the pixel. Output[j + 3] = 0; } } else { // Resize the Grayscale image to accommendate RGBA-formatting. Array.Resize(ref Output, Output.Length * 4); // Iterate through each pixel in the RGB-formatting. for (int j = Output.Length - 4; i >= 0; i -= 3, j -= 4) { // Change the pixel from Grayscale to RGBA-formatting. Output[j] = Output[j + 1] = Output[j + 2] = Output[i]; // Set the alpha channel for the pixel. Output[j + 3] = 0; } } // Return the RGBA-formatted image. Image.SetPixels(Width, Height, Output); } }