import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.*;
import java.io.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Video implements ActionListener
{
static int width=480;
static int height=368;
static JFrame frame = new JFrame();
static JButton button = new JButton("Submit");
static BufferedImage img = new BufferedImage((int) (width), (int) (height), BufferedImage.TYPE_INT_RGB);
static BufferedImage img1[] = new BufferedImage[60];
static {
for (int i = 0; i < img1.length; i++) {
img1[i] = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
}
public static void main(String[] args)
{
Video V1 = new Video();
String fileName = args[0];
try {
File file = new File(fileName);
InputStream is = new FileInputStream(file);
long len = file.length();
byte[] bytes = new byte[(int)len];
int offset = 0;
int numRead = 0;
int ind =0;
int[][] pixarray=new int[height+100][width+100];
int[][] red=new int[width*2][height*2];
int[][] green=new int[width*2][height*2];
int[][] blue=new int[width*2][height*2];
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
for(int frames=0;frames<60;frames++)
{
ind=height*width*frames*3;
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind+height*width];
byte b = bytes[ind+height*width*2];
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
pixarray[y][x]=pix;
red[y][x] = (pix >>> 16) & 0xff;
green[y][x] = (pix >>> 8) & 0xff;
blue[y][x] = pix & 0xff;
img1[frames].setRGB(x,y,pix);
ind++;
}
}
}
}
catch (IOException e) {
e.printStackTrace();
}
JLabel label = new JLabel(new ImageIcon(img1[50]));
frame.setLayout(new FlowLayout());
//frame.setSize(200,100);
frame.setVisible(true);
// Button button = new Button("Submit");
// frame.add(button);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.getContentPane().add(button, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
button.addActionListener(V1);
}
public void actionPerformed(ActionEvent e) {
System.out.println("1");
for(int i=0;i<img1.length;i++)
{
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(img1.length);
}
}
}
Is my code. My img1 is an array of frames. in this instance my video has 60 frames.
I wanna add each frame to my panel with a time gap. i am unable to do it because every time i add it to the panel in my ActionEvent its acting weird. help me out please.
Use a Swing Timer (not a TimerTask). When the Timer fires the code will execute in the EDT so you can safely reset the icon of your JLabel. So I would first create ImageIcons from your BufferedImages and store the Icons in your array.
Read the secton from the Swing tutorial on How to Use Timers for more information. You may also want to check out the section on Concurreny to understand why executing code in the EDT is important.
You can create a TimerTask
class VideoTask extends TimerTask {
private Frame frame;
private int frameId;
public void run() {
frame.drawImage(....);
frameId++;
}
}
And in the action listener for the button - schedule the task:
VideoTask videoTask = new VideoTask(frame);
videoTask.schedule(..);
Related
I have a video to ASCII animation converter and the pre-processing and compiling I do outside is fast but when I start displaying text it lags a lot in the beginning I thought it was that bad to calculate text size but using system.out.prinln was superfast and even if the carriage return works (which it doesn't) I'd like to have a sperate GUI/CLI panel for it
here is sample code that turns an image into ASCII
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class Main {
static short width;
static short height;
public static BufferedImage mytoBufferedImage(Image img){
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage buffImage = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D bGr = buffImage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return buffImage;
}
public static BufferedImage scaleApple(BufferedImage image){
return mytoBufferedImage(
image.getScaledInstance(width, height, BufferedImage.SCALE_FAST));
}
public static void main(String[] args) throws IOException, FontFormatException, InterruptedException {
width = 256;
height = 192;
BufferedImage Frame;
ArrayList<String> FrameBuffer = new ArrayList<>();
StringBuilder ASCIIFrame;
ArrayList<Byte> Bright;
final String[] Rendered;
byte R;
byte G;
byte B;
Frame = mytoBufferedImage(new ImageIcon("example.jpg").getImage());
Bright = new ArrayList<>();
int[] data =((DataBufferInt)
scaleApple(Frame)
.getRaster()
.getDataBuffer())
.getData();
for (int datum : data) {
Color c = new Color(datum);
R = (byte) c.getRed();
G = (byte) c.getGreen();
B = (byte) c.getBlue();
Bright.add((byte) Math.round(Math.sqrt( 0.299*((R&0xFF)^2) + 0.587*((G&0xFF)^2) + 0.114*((B&0xFF)^2))));
}
ASCIIFrame = new StringBuilder();
// j+(width*i) is cords
for (int i = 0; i < Main.height; i++) {
for (int j = 0; j < Main.width; j++) {
byte bright = Bright.get((j + (width * i)));
if ((bright <= 16) && (bright >= 12)) {
ASCIIFrame.append('█');
ASCIIFrame.append('█');
}else if ((bright <= 12) && (bright >= 8)) {
ASCIIFrame.append('▓');
ASCIIFrame.append('▓');
}else if ((bright <= 8) && (bright >= 4)) {
ASCIIFrame.append('▒');
ASCIIFrame.append('▒');
}else {
ASCIIFrame.append(' ');
ASCIIFrame.append(' ');
}
}
ASCIIFrame.append('\n');
}
FrameBuffer.add(ASCIIFrame.toString());
Rendered = FrameBuffer.toArray(String[]::new);
JTextPane render = new JTextPane();
render.setDoubleBuffered(false);
InputStream fontIS = new FileInputStream("JetBrainsMonoNL-Regular.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontIS).deriveFont(3f);
render.setFont(font);
StyledDocument doc = render.getStyledDocument();
SimpleAttributeSet center = new SimpleAttributeSet();
StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
doc.setParagraphAttributes(0, doc.getLength(), center, false);
JFrame Jframe = new JFrame();
JPanel MainPanel = new JPanel();
MainPanel.add(render);
render.setBackground(new Color(255,255,255));
Jframe.setTitle("Ascii");
Jframe.setSize(730, 645);
Jframe.setLocationRelativeTo(null);
Jframe.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
Jframe.setContentPane(MainPanel);
Jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Jframe.setVisible(true);
for (String Rendered_frame: Rendered){
System.out.println(Rendered_frame); // FAST
render.setText(Rendered_frame); // 99%GPU usage on rtx 3060
Thread.sleep(300);// timer methode in actual code
}
}
}
I tried carriage return, to make it easy to see System.out.println('\r'+Rendered_frame); and it didn't work
and Making A buffered Image and drawing on it with Graphics2D then displaying it on a Jlabel, is inaccurate, the display flickers and chops even with double Buffered set to true
I am currently in the middle of a project in which I have to grayscale, blue filter, red filter, green filter, and fade the edges of a picture of a cat at the press of a JButton. grayscale and color filtering the picture was easy, but I can't seem to figure out how to fade the image's edges to black.
What I mean by this, is I have this picture of the cat I need to change:
and I need to change it into something like this one:
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class GUIKittenPicJC {
public static class KittenChanger extends JPanel {
BufferedImage img = null;
File Kitten = null;
ImageIcon imagetransformed = null;
JButton grayscale = new JButton("Grayscale Image");
JButton rgbB = new JButton("Blue filter this image");
JButton rgbR = new JButton("Red filter this image");
JButton rgbG = new JButton("Green filter this image");
JButton fader = new JButton("Fade this image");
{
try
{
Kitten = new File("C:\\Users\\Jarrod\\Desktop\\Lab 3\\Lab 3\\kitten.bmp");
img = ImageIO.read(Kitten);
}
catch(IOException e)
{
System.out.println(e);
}
ImageIcon image = new ImageIcon(img);
JLabel imageLabel = new JLabel(image);
add(imageLabel);
grayscale.addActionListener(e->{
imagetransformed = new ImageIcon(Grayscale(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbB.addActionListener(e->{
imagetransformed = new ImageIcon(Bluify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbG.addActionListener(e->{
imagetransformed = new ImageIcon(Greenify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
rgbR.addActionListener(e->{
imagetransformed = new ImageIcon(Redify(img));
imageLabel.setIcon(imagetransformed);
imgReset();
});
add(grayscale);
add(rgbB);
add(rgbG);
add(rgbR);
}
private void imgReset() {
try {
img = ImageIO.read(Kitten);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public static void main(String[] args)
{
createGUI();
}
private static void createGUI() {
JFrame frame = new JFrame("Kitten Changer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
KittenChanger newContentPane = new KittenChanger();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(400, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//Grayscale and rgb filter methods follow, not important to question
}
How would I create a method for this sort of filter and properly apply it to a button like the others?
You would have to edit each of the pixels individually. The main idea of this solution is that you have the center of an image, and each pixel is changed as a function of the distance to the center.
More simply put, pixels farther from the center will be made darker.
This is what your code would look like:
BufferedImage cat; //assuming it is assigned
for(int i = 0; i < cat.getWidth(); i++) { // i is the x coord
for(int j = 0; j < cat.getHeight(); j++) { // j is the y coord
int color = cat.getRGB(i, j);
int r = (color >> 16) & 0xff; //extract red value
int g = (color >> 8) & 0xff;
int b = color & 0xff;
double scale = 0.75; /**** Change this to change the resulting effect ****/
//pixel's distance from center
double dist = Math.sqrt( Math.pow(i - cat.getWidth()/2, 2) + Math.pow(j - cat.getHeight()/2, 2) );
r = (int) Math.max(0, r - dist*scale); //r - dist*scale makes px darker
g = (int) Math.max(0, g - dist*scale); //Math.max makes sure r is always >= 0
b = (int) Math.max(0, b - dist*scale);
int newRGB = (r << 16) + (g << 8) + b; //convert r,g,b to single int
cat.setRGB(i, j, newRGB); //finally, update rgb value
}
}
And, when I ran this code:
Remember, you can always change the effect by changing the scale variable in the code above.
I am trying display images in sequential manner, but having difficulties in displaying(only last image is getting displayed on screen). I tried using CardLayout but as my number of JLabels(which contains ImageIcon) are high so it's giving me outofmemory exception. here's my code:
I have JFrame inside that I have JPanel in which I am trying to display JLabel one by one.
public class ImageMain extends JFrame implements ActionListener{
private static final long serialVersionUID = 2916361361443483318L;
private JFileChooser fc = null;
private JMenuItem item1,item2;
private BufferedImage image = null;
private JPanel panel = null;
private int width = 0;
private int height = 0;
private BorderLayout card;
private Container contentPane;
public ImageMain() {
JFrame frame = new JFrame("Image Extraction Tool");
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = frame.getContentPane();
panel = new JPanel();
card = new BorderLayout();
panel.setLayout(card);
panel.setBackground(Color.white);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("Menu");
menuBar.add(menu);
item1 = new JMenuItem("Browse an image");
item2 = new JMenuItem("Exit");
item1.addActionListener(this);
item2.addActionListener(this);
menu.add(item1);
menu.add(item2);
frame.setJMenuBar(menuBar);
contentPane.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
ImageMain img = new ImageMain();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == item1){
if(fc == null)
fc = new JFileChooser();
int retVal = fc.showOpenDialog(null);
if(retVal == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try {
image = ImageIO.read(file);
height = image.getHeight();
width = image.getWidth();
int[][] pixelData = new int[height * width][3];
int[] rgb;
int counter = 0;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
rgb = getPixelData(image, i, j);
for(int k = 0; k < rgb.length; k++){
pixelData[counter][k] = rgb[k];
}
counter++;
}
}
for(int m=pixelData.length-10; m < pixelData.length; m++){
System.out.println(m);
int[] pix = new int[width*height*3];
for(int i=0;i< width*height*3; i+=3){
pix[i] = pixelData[m][0];
pix[i+1] = pixelData[m][1];
pix[i+2] = pixelData[m][2];
}
JLabel createImageLabel = createImageLabel(pix);
panel.add(createImageLabel);
// panel.revalidate();panel.repaint();
contentPane.revalidate();contentPane.repaint();
Thread.sleep(2000);
}
} catch (IOException e1) {
System.out.println("IO::"+e1.getMessage());
}catch(Exception e1){
System.out.println("Exception::"+e1.getMessage());
}
}
}
if(e.getSource() == item2){
System.exit(0);
}
}
private int[] getPixelData(BufferedImage image, int x, int y) {
int argb = image.getRGB(y, x);
int rgb[] = new int[] {
(argb & 0x00ff0000) >> 16 , //red
(argb & 0x0000ff00) >> 8, //green
argb & 0x000000ff //blue
};
return rgb;
}
private JLabel createImageLabel(int[] pixels){
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
WritableRaster raster = image.getRaster();
raster.setPixels(0, 0, width, height, pixels);
JLabel label = new JLabel(new ImageIcon(image));
return label;
}
}
please advice.
Thanks in advance.
Please check for similar questions and answers on this site about not calling Thread.sleep(...) inside of the Swing event thread. As they state, calling this will put your entire Swing GUI to sleep which is not your intention, I'm sure. Since your problem is no different from all the rest, the solution is the same as well: use a Swing Timer for intermittent actions, and use a background thread such as that generated by a SwingWorker for long-running background actions -- such as reading in images.
Also, if all you're doing is swapping images, then one JLabel should work fine. Just swap the ImageIcon that it displays in your Swing Timer.
Edit
You state:
I am not sure how to iterate my 2D array with swing Timer, every 2 sec I want to iterate my array and perform some action.
Suggestions:
Give your timer a delay of 2000 (for milliseconds)
Give the Timer's ActionListener one or two int counter fields that are initialized to 0.
In the actionPerformed method, use the counter to get the item of interest, and then advance the counter(s).
Read the Swing Timer Tutorial.
I'm trying to calculate DCT and IDCT of an input image, and display the IDCT output as resultant image. But my IDCT values are going above 300. My input image is a '.rgb' image.
I am also considering the height and width of the input image as constant i.e., 352*288
I am representing my input red, green and blue integers of each pixels as rgb[3][64][1583] where [3] -> index of red/green/blue and [64] -> index of pixel in each block and the [1583]->
index of the 8*8 block i.e., one of the block in 1583 blocks.
And lastly im keeping my quantization table as which has uniform value -> 2^N where N is passed as parameter.In this code the quantLevel is the N above.
Following is my Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Jpeg {
double rgb[][][]=new double[3][64][1584];
double rgbfinal[][][]=new double[3][64][1584];
double R[][]=new double[64][1584];
double G[][]=new double[64][1584];
double B[][]=new double[64][1584];
public void go(String fname, int quantLevel){
int numBlocks=(352*288)/64;
String fileName = fname;
int width=352,height=288;
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
try {
File file = new File(fname);
InputStream is = new FileInputStream(file);
long len = file.length();
byte[] bytes = new byte[(int)len];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
int ind = 0;
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
byte r = bytes[ind];
byte g = bytes[ind+height*width];
byte b = bytes[ind+height*width*2];
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
img.setRGB(x,y,pix);
ind++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int indexRow=0,indexCol=0,indexBlock=0,indexBits=0,indexPixelBlock=0,indexPixel=0;
long count=0L;
String binary="";
indexPixel=0;
indexBlock=0;
int i=0;
while(count!=(long)(numBlocks*64)){
int pix = img.getRGB(indexCol, indexRow);
int red = (pix >> 16) & 0x000000FF;
int green = (pix >>8 ) & 0x000000FF;
int blue = (pix) & 0x000000FF;
rgb[0][indexPixel][indexBlock]=red;
rgb[1][indexPixel][indexBlock]=green;
rgb[2][indexPixel][indexBlock]=blue;
count++;
indexPixel++;
if(indexCol==width-1 && indexRow==height-1)
break;
if(indexCol%7==0 && indexCol!=0 && indexPixel%8==0)
{
indexPixel=indexPixelBlock;
indexBlock++;
}
if(indexPixel%8==0 && indexCol%7!=0 && indexBlock!=1583)
{
indexPixel=indexPixelBlock;
indexBlock++;
}
if(indexCol==width-1)
{
indexCol=0;
indexRow++;
}
else
indexCol++;
if((indexPixel)%8==0 && indexBlock==numBlocks-1 && indexCol%7==0)
{
indexBlock=0;
indexPixelBlock=indexPixel;
}
}
calcQuantizedDCT(quantLevel);
calcInverseDCT(quantLevel);
JFrame frame = new JFrame();
frame.setLocation(0,0);
frame.setSize(1024, 720);
frame.getContentPane().setBackground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.setLocation(100,100);
JLabel label = new JLabel(new ImageIcon(img));
label.setLocation(0,0);
label.setSize(352,288);
p.add(label);
frame.add(p);
frame.setVisible(true);
return;
}
void calcQuantizedDCT(int quantLevel)
{
String binary="";
int indexBlock=0,indexPixel=0,indexBits=0,red=0,green=0,blue=0,x=0,y=0,indexPixelTemp=0,u=0,v=0;
double sumRed=0,sumGreen=0,sumBlue=0;
String substr="";
int i=0;
for(indexBlock=0;indexBlock<1584;indexBlock++)
{
indexPixel=0;
//
while(indexPixel!=64 && u<8)
{
while(indexPixelTemp<64 && x<8)
{
red=(int)rgb[0][indexPixelTemp][indexBlock];
green=(int)rgb[1][indexPixelTemp][indexBlock];
blue=(int)rgb[2][indexPixelTemp][indexBlock];
// System.out.println(red);
sumRed+=red*Math.cos((Math.PI*(2*y+1)*u)/(2*8))*Math.cos((Math.PI*(2*x+1)*v)/(2*8));
sumGreen+=green*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
sumBlue+=blue*Math.cos((Math.PI*(2*x+1)*u)/(16))*Math.cos((Math.PI*(2*y+1)*v)/(16));
indexPixelTemp++;
y++;
if(y==8)
{
x++;
y=0;
}
}
//System.out.println("SumRed :"+sumRed);
if(u==0 && v==0)
{
//System.out.println("U & V & Pixel & Block "+u+" "+v+" "+indexPixel+" "+indexBlock);
R[indexPixel][indexBlock]=(Math.sqrt(1.0/64.0)*sumRed)/Math.pow(2,quantLevel);
G[indexPixel][indexBlock]=(Math.sqrt(1.0/64.0)*sumGreen)/Math.pow(2,quantLevel);
B[indexPixel][indexBlock]=(Math.sqrt(1.0/64.0)*sumBlue)/Math.pow(2,quantLevel);
}
else
{
//System.out.println("U & V & Pixel & Block "+u+" "+v+" "+indexPixel+" "+indexBlock);
R[indexPixel][indexBlock]=(Math.sqrt(2.0/32.0)*sumRed)/Math.pow(2,quantLevel);
G[indexPixel][indexBlock]=(Math.sqrt(2.0/32.0)*sumGreen)/Math.pow(2,quantLevel);
B[indexPixel][indexBlock]=(Math.sqrt(2.0/32.0)*sumBlue)/Math.pow(2,quantLevel);
}
indexPixel++;
if(indexPixel==64)
break;
indexPixelTemp=0;
v++;
if(v==8)
{
v=0;
u++;
}
x=0;y=0;sumGreen=0;sumRed=0;sumBlue=0;
}
u=0;v=0;
}
/* for(int j=0;j<64;j++)
{
System.out.print(R[j][0]+" ");
if(j%7==0 && j!=0)
System.out.println();
}
*/
}
void calcInverseDCT(int quantLevel)
{
String binary="";
int indexBlock=0,indexPixel=0,indexBits=0,u=0,v=0,x=0,y=0,indexPixelTemp=0,sumRed=0,sumGreen=0,sumBlue=0,red=0,green=0,blue=0;
for(indexBlock=0;indexBlock<1584;indexBlock++)
{
for(indexPixel=0;indexPixel<64;indexPixel++)
{
R[indexPixel][indexBlock]=R[indexPixel][indexBlock]*Math.pow(2,quantLevel);
G[indexPixel][indexBlock]=G[indexPixel][indexBlock]*Math.pow(2,quantLevel);
B[indexPixel][indexBlock]=B[indexPixel][indexBlock]*Math.pow(2,quantLevel);
}
}
int i=0;
indexPixelTemp=0;
indexPixel=0;
for(indexBlock=0;indexBlock<1584;indexBlock++)
{
indexPixel=0;
while(indexPixel<64 && x<8)
{
indexPixelTemp=0;
while(indexPixelTemp<64 && u<8)
{
red=(int)R[indexPixelTemp][indexBlock];
if(u==0 && v==0)
sumRed+=Math.sqrt(1.0/2.0)*red*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
else
sumRed+=red*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
green=(int)G[indexPixelTemp][indexBlock];
if(u==0 && v==0)
sumGreen+=Math.sqrt(1.0/2.0)*green*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
else
sumGreen+=green*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
blue=(int)B[indexPixelTemp][indexBlock];
if(u==0 && v==0)
sumBlue+=Math.sqrt(1.0/2.0)*blue*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
else
sumBlue+=blue*Math.cos((Math.PI*(2*x+1)*u)/(2*8))*Math.cos((Math.PI*(2*y+1)*v)/(2*8));
indexPixelTemp++;
v++;
if(v==8)
{
u++;
v=0;
}
}
rgbfinal[0][indexPixel][indexBlock]=sumRed;
rgbfinal[1][indexPixel][indexBlock]=sumGreen;
rgbfinal[2][indexPixel][indexBlock]=sumBlue;
indexPixel++;
indexPixelTemp=0;
y++;
if(y==8)
{
y=0;
x++;
}
u=0;v=0;sumGreen=0;sumRed=0;sumBlue=0;
}
if(i==3)
break;
x=0;y=0;
}
System.out.println();
/*for(i=0;i<64;i++)
{
System.out.print(rgbfinal[0][i][0]+" ");
if(i%7==0 && i!=0)
System.out.println();
}*/
}
public static void main(String args[]){
Jpeg a = new Jpeg();
a.go("src/image2.rgb",0);
}
}
I am not trying to display the output image as the ouput of IDCT for Red which i checked goes above 255.
Please help.
First, are you hoping the create a JPEG-compatible codec? Or are you just toying with multimedia coding concepts? If the latter, then more power to you. If the former, you have a lot of work ahead.
To directly answer your question: It's normal to transform an 8x8 block using a 2D forward DCT and get numbers that go above 255. Low-level implementations of the forward DCT usually take an 8x8 vector of unsigned 8-bit samples as input and output an 8x8 vector of signed 16-bit samples.
If you actually are hoping to create a JPEG-compatible codec, you still have a few topics to study. For starters, JPEG does not compress RGB. RGB data is converted to YUV and those blocks get transformed (then quantized, zigzagged, and entropy coded).
I'm about to write a program that can take .rgb video file as input, and specify his fps and play it out. But how can I meet this purpose without using JMF package, because I think the meaning of this program is to know how the frame-by-frame works, so I decide it to use BufferedImage as my tool to implement this program, but I still not figure it out how it works on the video (frame-by-frame).
here's the starter code that TA gave us:
I try lots of things, but the delay between two frames is very obvious, I don't know why. is there any efficiency way to play the video frame-by-frame without using any JMF package.
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
public class imageReader {
public static void main(String[] args) {
String fileName = args[0];
int width = Integer.parseInt(args[1]);
int height = Integer.parseInt(args[2]);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
try {
File file = new File(fileName+".rgb");
InputStream is = new FileInputStream(file);
//BufferedInputStream bis = new BufferedInputStream(is);
long len = file.length(); //bytes in one frame (consist of R,G,B frame)
byte[] bytes = new byte[10000000];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
System.out.println("\noffset = "+ offset); //7603200
System.out.println("numRead = "+ numRead); //7603200
System.out.println("file length: " + len); //7603200
System.out.println("frame #: "+ (offset/(width*height*3))); //100
JFrame frame = new JFrame();
frame.setVisible(true);
int ind = 0;
int f = 1;
while(ind < offset){
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
byte a = 0;
byte r = bytes[ind];
byte g = bytes[ind+height*width];
byte b = bytes[ind+height*width*2];
int pix = 0xff000000 | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
//int pix = ((a << 24) + (r << 16) + (g << 8) + b);
img.setRGB(x,y,pix);
ind++;
}
}
JLabel label = new JLabel(new ImageIcon(img));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
System.out.println("frame #: "+ ind);
}
System.out.print("end");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Use a label to display the image
/*JFrame frame = new JFrame();
*JLabel label = new JLabel(new ImageIcon(img));
*frame.getContentPane().add(label, BorderLayout.CENTER);
*frame.pack();
*frame.setVisible(true);
**/
}
}
What is going wrong with this code??