6

ピクセルあたり 8 ビットのグレースケール イメージのデータ配列を含む、組み込みシステム用の C 言語ソース コードがあります。私はソフトウェアの文書化を担当しており、このソース コードを JPEG (画像) ファイルに変換したいと考えています。

コードサンプルは次のとおりです。

const unsigned char grayscale_image[] = {
0, 0, 0, 0, 0, 0, 0, 74, 106, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 
159, 159, 159, 159, 159, 159, 159, 159, 159, 159, 146, 93, 39, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
//...
};
const unsigned int height = 41;
const unsigned int width = 20;

ここに私の質問があります: (はい、複数形)

  1. このソース ファイルを JPEG に変換するには、どのアプリケーションをお勧めしますか?
  2. GIMP またはペイントはデータの CSV ファイルをインポートできますか?
  3. このカスタム アプリケーションを作成する場合、JPEG 用に存在する Java ライブラリは何ですか?
  4. このタスクを達成するために、C# にはどのようなライブラリが存在しますか?

次のリソースを自由に使用できます: MS Visio 2010、Gimp、ペイント、Java、Eclipse、MS Visual Studio 2010 Professional、wxWidgets、wxFrameBuilder、Cygwin。
C#、Java、C、または C++ でカスタム アプリケーションを作成できます。

アドバイスありがとうございます。

4

3 に答える 3

2

javaの使用に関する問題は、バイトをintとして取得することです。javaには符号なしバイトがないため、読み込み中に127を超える値をキャプチャするにはintに変換する必要があります。

int height=41;
int width=20;
int[] data = {...};

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for ( int x = 0; x < width; x++ ) {
  for ( int y = 0; y < height; y++ ) {
  // fix this based on your rastering order
  final int c = data[ y * width + x ];
  // all of the components set to the same will be gray
  bi.setRGB(x,y,new Color(c,c,c).getRGB() );
  }
}
File out = new File("image.jpg");
ImageIO.write(bi, "jpg", out);
于 2011-08-16T17:05:19.297 に答える
1

質問 4 に答えることができ、C# でこれを行うためのコードを提供できます。とてもシンプルです...

int width = 20, height = 41;
byte[] grayscale_image = {0, 0, 0, ...};
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
int x = 0;
int y = 0;
foreach (int i in grayscale_image)
{
    bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(i, i, i));
    x++;
    if (x >= 41)
    {
        x = 0;
        y++;
    }
}
bitmap.Save("output.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

ビットマップの最適化手法 (ビットマップ メモリのロックなど) を調べれば、このコードを最適化できる場合もあります。

編集:ビットロックの代替(はるかに高速である必要があります)...

注: Bitmap オブジェクトを作成するときに使用される PixelFormat について 100% 確信があるわけではありません。

int width = 20, height = 41;
byte[] grayscale_image = {0, 0, 0, ...};
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height, PixelFormat.Format8bppIndexed);

System.Drawing.Imaging.BitmapData bmpData = bitmap.LockBits(
                     new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                     ImageLockMode.WriteOnly, bitmap.PixelFormat);

System.Runtime.InteropServices.Marshal.Copy(bytes, 0, bmpData.Scan0, bytes.Length);

bitmap.UnlockBits(bmpData);

return bitmap;
于 2011-08-16T15:55:19.933 に答える
0

Java で ImageIO クラスを使用できます。

BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GREY);

Graphics2D g2 = bi.createGraphics();

//loop through and draw the pixels here   

File out = new File("Myimage.jpg");
ImageIO.write(bi, "jpg", out);
于 2011-08-16T15:50:30.950 に答える