Finding A URl on a Page Java Code --Java Programming: Solving Problems with Software
/**
* Write a description of URLFINDER here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.io.*;
public class URLFINDER
{
public void asd(){
URLResource file = new URLResource("http://www.dukelearntoprogram.com/course2/data/manylinks.html");
for ( String item : file.words() ) {
String itemLower = item.toLowerCase();
int pos = itemLower.indexOf("youtube.com");
if ( pos != -1 ) {
int beg = item.lastIndexOf("\"", pos);
int end = item.indexOf("\"", pos + 1);
System.out.println(item.substring(beg + 1, end));
}
}
}
}
Code For finding aProtien string in a DNA --Java Programming: Solving Problems with Software
import edu.duke.*;
import java.io.*;
public class TagFinder{
public String findProtein(String dna){
int start = dna.indexOf("atg");
int stop = dna.indexOf("tag", start +3);
if (start == -1){
return "no starton protein found";
}
if ((stop-start)%3==0){
return dna.substring(start,stop+3);
}
else{
return"not not a protein because of Divisible factor";
}
}
public void testing()
{
String a = "cccatggggtttaaataataataggagagagagagagagttt";
String ap = "atggggtttaaataataatag";
//String a = "sdafsdfsdatgccctagdsfsdfgsffd";
//String ap = "atgccctag";
//String a = "atgcctag";
//String ap = "";
//String a = "ATGCCCTAG";
//String ap = "ATGCCCTAG";
String result = findProtein(a);
if (ap.equals(result)){
System.out.println("sucess for "+ap + "length " + ap.length());
}
else{
System.out.println("mistake for input:"+a);
System.out.println("got:"+result);
System.out.println("not:"+ap);
}
}
public void realTesting(){
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()){
FileResource fr = new FileResource(f);
String s =fr.asString();
System.out.println("read "+s.length()+" character");
String result = findProtein(s);
System.out.println("found "+ result);
}
}
}
import java.io.*;
public class TagFinder{
public String findProtein(String dna){
int start = dna.indexOf("atg");
int stop = dna.indexOf("tag", start +3);
if (start == -1){
return "no starton protein found";
}
if ((stop-start)%3==0){
return dna.substring(start,stop+3);
}
else{
return"not not a protein because of Divisible factor";
}
}
public void testing()
{
String a = "cccatggggtttaaataataataggagagagagagagagttt";
String ap = "atggggtttaaataataatag";
//String a = "sdafsdfsdatgccctagdsfsdfgsffd";
//String ap = "atgccctag";
//String a = "atgcctag";
//String ap = "";
//String a = "ATGCCCTAG";
//String ap = "ATGCCCTAG";
String result = findProtein(a);
if (ap.equals(result)){
System.out.println("sucess for "+ap + "length " + ap.length());
}
else{
System.out.println("mistake for input:"+a);
System.out.println("got:"+result);
System.out.println("not:"+ap);
}
}
public void realTesting(){
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()){
FileResource fr = new FileResource(f);
String s =fr.asString();
System.out.println("read "+s.length()+" character");
String result = findProtein(s);
System.out.println("found "+ result);
}
}
}
Converting Images to Gray Scale And Saving A Copy Of Them(Multiple) -- Java Programming: Solving Problems with Software
import edu.duke.*;
import java.io.File;
public class GrayScaleConverter {
//started with the image i wanted(inImage)
public ImageResource makeGray(ImageResource inImage){
//I Made a blank image of the same size
ImageResource outImage = new ImageResource(inImage.getWidth(),inImage.getHeight());
//for each pixelin outImage
for(Pixel pixel : outImage.pixels()){
//look at the corresponding pixel in inImage
Pixel inPixel = inImage.getPixel(pixel.getX(),pixel.getY());
//compute inPixel's red + inPixel's blue + inpixel's green
//divide that sum by 3(call it average)
int average = (inPixel.getRed()+inPixel.getGreen()+inPixel.getBlue())/3;
//set pixel's red to average
pixel.setRed(average);
//set pixel's green to average
pixel.setGreen(average);
//set pixel's blur to average
pixel.setBlue(average);
}
return outImage;
}
public void SelectAndConvert(){
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()){
ImageResource inImage = new ImageResource(f);
ImageResource gray = makeGray(inImage);
String fname = inImage.getFileName();
String newfname = "copy-" + fname;
gray.setFileName(newfname);
gray.draw();
gray.save();
}
}
}
Batch Gratscale Image Code --Java Programming: Solving Problems with Software
import edu.duke.*;
import java.io.File;
public class GrayScaleConverter {
//started with the image i wanted(inImage)
public ImageResource makeGray(ImageResource inImage){
//I Made a blank image of the same size
ImageResource outImage = new ImageResource(inImage.getWidth(),inImage.getHeight());
//for each pixelin outImage
for(Pixel pixel : outImage.pixels()){
//look at the corresponding pixel in inImage
Pixel inPixel = inImage.getPixel(pixel.getX(),pixel.getY());
//compute inPixel's red + inPixel's blue + inpixel's green
//divide that sum by 3(call it average)
int average = (inPixel.getRed()+inPixel.getGreen()+inPixel.getBlue())/3;
//set pixel's red to average
pixel.setRed(average);
//set pixel's green to average
pixel.setGreen(average);
//set pixel's blur to average
pixel.setBlue(average);
}
return outImage;
}
public void SelectAndConvert(){
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles()){
ImageResource inImage = new ImageResource(f);
ImageResource gray = makeGray(inImage);
gray.draw();
}
}
}
Image Saver --Java Programming: Solving Problems with Software
import edu.duke.*;
import java.io.*;
public class imageSaver {
public void doSave(){
DirectoryResource dr = new DirectoryResource();
for(File f : dr.selectedFiles()){
ImageResource image = new ImageResource(f);
String fname = image.getFileName();
String newfname = "copy-" + fname;
image.setFileName(newfname);
image.draw();
image.save();
}
}
}
Grayscaling An Image One At A Time -- Java Programming: Solving Problems with Software
/**
* Write a description of GrayScaleConverter here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
public class GrayScaleConverter {
//started with the image i wanted(inImage)
public ImageResource makeGray(ImageResource inImage){
//I Made a blank image of the same size
ImageResource outImage = new ImageResource(inImage.getWidth(),inImage.getHeight());
//for each pixelin outImage
for(Pixel pixel : outImage.pixels()){
//look at the corresponding pixel in inImage
Pixel inPixel = inImage.getPixel(pixel.getX(),pixel.getY());
//compute inPixel's red + inPixel's blue + inpixel's green
//divide that sum by 3(call it average)
int average = (inPixel.getRed()+inPixel.getGreen()+inPixel.getBlue())/3;
//set pixel's red to average
pixel.setRed(average);
//set pixel's green to average
pixel.setGreen(average);
//set pixel's blur to average
pixel.setBlue(average);
}
return outImage;
}
public void testgray(){
ImageResource ir = new ImageResource();
ImageResource gray = makeGray(ir);
gray.draw();
}
}
Display the Name Of The Selected Files For GreyScale --Java Programming: Solving Problems with Software
/**
* Write a description of greyscale here.
*
* @author (your name)
* @version (a version number or a date)
*/
import edu.duke.*;
import java.io.*;
public class greyscale {
public void checkdir(){
DirectoryResource dr = new DirectoryResource();
for (File f : dr.selectedFiles())
{
System.out.println(f);
}
}
}
public class DaysAlivePrinter
{
public static void main(String[] args)
{
// Sally Ride was born on May 26 1951
Day birthday = new Day (1951 , 5 , 26);
// Her last day was July 23 2012
Day Lastday = new Day (2012 , 7 , 23);
// Use Day objects to calculate and print
int DaysAlive = Lastday.daysFrom(birthday);
// how many days she lived.
System.out.println(DaysAlive);
// You'll want to use the daysFrom method.
// Do you expect to get a negative or a positive number of days?
}
}
{
public static void main(String[] args)
{
// Sally Ride was born on May 26 1951
Day birthday = new Day (1951 , 5 , 26);
// Her last day was July 23 2012
Day Lastday = new Day (2012 , 7 , 23);
// Use Day objects to calculate and print
int DaysAlive = Lastday.daysFrom(birthday);
// how many days she lived.
System.out.println(DaysAlive);
// You'll want to use the daysFrom method.
// Do you expect to get a negative or a positive number of days?
}
}
Java Programming: Solving Problems with Software,
// You can use this program to run experiments
// 1. Add statements inside main
// 2. Compile
// 3. Right-click on the Test rectangle and select main
// 4. Click Ok on the next dialog
// 5. The output is displayed in a terminal window
public class Test
{
public static void main(String[] args)
{
// Add something inside the ( )
//compile time error
//System.out.println(a b);
//prints total and in only one line
//System.out.print(3+4+5);
//runtme Error
//System.out.println(1/0);
//prints total and goes to the next line
//System.out.println(3+4+5);
//prints the string as it is "3+4+5"
//System.out.println("3+4+5");
// Add more statements below, as needed
System.out.print("My Lucky number is");
System.out.println(3 + 4 + 5);
}
}
// 1. Add statements inside main
// 2. Compile
// 3. Right-click on the Test rectangle and select main
// 4. Click Ok on the next dialog
// 5. The output is displayed in a terminal window
public class Test
{
public static void main(String[] args)
{
// Add something inside the ( )
//compile time error
//System.out.println(a b);
//prints total and in only one line
//System.out.print(3+4+5);
//runtme Error
//System.out.println(1/0);
//prints total and goes to the next line
//System.out.println(3+4+5);
//prints the string as it is "3+4+5"
//System.out.println("3+4+5");
// Add more statements below, as needed
System.out.print("My Lucky number is");
System.out.println(3 + 4 + 5);
}
}
week 1, Lesson 3 of 6: "Hello!" around the World, Iterables in Java Module --Java Programming: Solving Problems with Software
https://www.coursera.org/learn/java-programming/supplement/6CLKD/module-learning-outcomes-resources
Iterables in Java Module
In this module, you will learn to design and run your first Java programs, including one program that prints “Hello!” in various countries’ languages and another where you will copy and edit many files by converting color images to grayscale. To accomplish these tasks, you will learn about classes constructed for this course: Libraries of iterables that allow you to perform the same task over multiple lines in a document or web page or multiple files within a directory.By the end of this module, you will be able to:
- Download and run BlueJ, the Java programming environment for this course;
- Access the documentation for the Java libraries specially designed for this course;
- Edit, compile, and run a Java program;
- Construct for loops in Java; and
- Use Iterables to run a program that iterates over multiple lines in a document or webpage or multiple files in a directory.
Lecture Slides
More Course Resources
http://www.dukelearntoprogram.com/course2/index.php - This website of programming resources contains pages for each course in the Duke Java Programming specialization. The link above for this course is where you will go to:- Download the custom version of the BlueJ environment;
- Find project resources, such as example code from the lecture videos;
- Download images and data files for the programming exercises; and
- See documentation for the custom classes developed for this course.
Top 5 Free Screen Recording Softwares For Windows
Screen recording can be useful when you need to record a how-to video to help someone learn how to use a program, record a game walkthrough, or prepare for a presentation. Record here means you can create a video of whatever you are doing on your desktop, save the recording as a video file and send it to a friend or upload it on YouTube.
We’ve featured screen capturing tools before, and you know a few ways to take screenshots on your computer. For something more than just a still image, here are 8 free screen recording programs that will help you record every action you make on your Windows desktop.
Free programs at times come with drawbacks or limitations, and watermarks are common in many free screen recording softwares. However, the following list of programs has been tested to not have any watermarks and can export to a file format recognizable to most video editing softwares.
1. Ezvid
Ezvid is a screen recorder program that comes with an in-built video editor where you can split your recordings and add text in between two clips, creating aslideshow effect. There is no way to export the video you recorded. However, you can upload the video to YouTube through the program itself.
For gamers, there’s an option to enable ‘Gaming mode’ where it records the windowed mode of your game. The program comes with a few music clips. However if you decide to have no music, it will be replaced with a ‘Silent machine’ which sounds like a small fan from a computer. You can add in other pictures and video clips, as well as add in your voice after you’re done editing your clip.
2. BlueBerry FlashBack Express Recorder
BB (short for BlueBerry) FlashBack Express Recorder lets you use your webcam to record yourself while recording the activities that are happening on your desktop. After you have stopped recording, it creates an FBR file which can be edited with its packaged video editor.
If you did not enable your webcam, you can skip the video editor program and export it to the AVI file format right away. Otherwise, you can use the software to position and resize your webcam box before exporting it. Although it requires you to register (for a free account) after 30 days of usage, it still provides you with all of its functions before you register.
3. Screenr
Screenr is an interesting way to share a screencast (recording of your screen) online without installing a program on your computer; it requires Java to work. You select an area on your screen which you want to record (max: 5 minutes). All recordings are saved into your account.
After recording you’ll be given a link which you can share. You can also export your video to MP4 or upload it on YouTube.
Screenr also provides a bookmarklet so you can record without going to the website. Register with your Facebook, Twitter, Google, LinkedIn, Yahoo or Windows Live account in order to use Screenr for free.
4. Rylstim Screen Recorder
Rylstim just records your screen after you hit the ‘Start Record’ button. This will be useful for people who do not want to configure anything and just want a basic recorder. This program does not record sound from input devices like a microphone.
The only options available determine if you want to show your left or right mouse button clicks in the video. If you enable the mouse click options, a red ripple will appear at your cursor when you left click and a green ripple appears for right clicks. This mouse click ripple effect will only be visible when you view your recording.
5. CamStudio
CamStudio comes equipped with many options to tweak the way it records. There are options to enable or disable your mouse cursor, record sounds from programs or a microphone (or have no sound at all) and the option to enable custom screen annotations.
Here’s more!
Webinaria
Webinaria is another easy-to-use screen recording software with basic options for your recording needs. It can record your entire screen, a program window or a custom selection. You can choose from 3 frame rate options; 5, 10 and 15 FPS (frames per second).
Videos are produced in AVI file format. If you were using Google Chrome before running Webinaria, Google Chrome will be detected as the program window for recording.
DVDVideoSoft Free Screen Video Recorder
This program has a simple user interface carrying 9 icons. The first 4 is for screen capturing, the next 4 handles screen recording and the last icon opens the options of the program. It also exports its video to an AVI file format. What’s good about this program is its automatic file naming options.
It gives you options to include the specific date and time in the filename of your recorded video. Users who need to keep track of many screen recordings back to back will find this automatic file naming option useful.
Krut Computer Recorder
Krut does not require installation but uses Java to run. After downloading, you’ll have a folder where you have to run the ‘KRUT.jar’ file to get started. The capture area and recording frames per second can be set by the user.
There is an option called ‘Follow Mouse’ where it captures the area around your mouse, wherever it moves to. When using the ‘Follow Mouse’ feature, you can enable preview mode to see the captured area as you record. This program outputs to three types of files: the WAV file only has the audio recorded, while of the two MOV files; one has no audio and the other has both audio and video of the recording.
You can also choose to record a particular area on your screen or a program window so that the rest of your desktop isn’t showing on the recording. It can record at different frame rate speeds; for example 1 FPS (frames per second)to create a time lapse video effect, or 30 FPS for a smooth video.
“Java Programming: Solving Problems with Software”
Welcome to “Welcome to “Java Programming: Solving Problems with Software”! We are excited that you are starting our course to learn how to write programs in Java, one of the most popular programming languages in the world. In this introductory module, you will get to meet the instructor team from Duke University, have an overview of the course, and hear from Google engineers about why they find programming exciting and why Java is important. Have fun!”! We are excited that you are starting our course to learn how to write programs in Java, one of the most popular programming languages in the world. In this introductory module, you will get to meet the instructor team from Duke University, have an overview of the course, and hear from Google engineers about why they find programming exciting and why Java is important. Have fun!
http://www.dukelearntoprogram.com/course1/example/index.php
// write your code here
//Function returns chopped pixel color values
function pixchange(pixval){
var x = Math.floor(pixval/4) * 4;
return x;
}
//Code section chops bits of pixel colors of image that hides another image
function chop2hide(image){
for(var px of image.values()){
px.setRed(pixchange(px.getRed()));
px.setGreen(pixchange(px.getGreen()));
px.setBlue(pixchange(px.getBlue()));
}
return image;
}
//Code Section used to shift bits of pixel colors of image to be hidden
function shift(im){
var nim = new SimpleImage(im.getWidth(),im.getHeight());
for(var px of im.values()){
var x = px.getX();
var y = px.getY();
var npx = nim.getPixel(x,y);
npx.setRed(Math.floor(px.getRed()/64));
npx.setGreen(Math.floor(px.getGreen()/64));
npx.setBlue(Math.floor(px.getBlue()/64));
}
return nim;
}
//Function returns cropped images
function crop(image,w,h){
var img = new SimpleImage(w,h);
for (var pix of img.values()){
var x = pix.getX();
var y = pix.getY();
var p = image.getPixel(x,y);
pix.setRed(p.getRed());
pix.setGreen(p.getGreen());
pix.setBlue(p.getBlue());
}
return img;
}
//Code Section combines the 2 images into a single one
function combine(image1,image2){
var nimg = new SimpleImage(image1.getWidth(),image1.getHeight());
for (var pix of image1.values()){
var x = pix.getX();
var y = pix.getY();
var p = image2.getPixel(x,y);
var pixel = nimg.getPixel(x,y);
pixel.setRed(pix.getRed()+p.getRed());
pixel.setGreen(pix.getGreen()+p.getGreen());
pixel.setBlue(pix.getBlue()+p.getBlue());
}
return nimg;
}
//Function returns extracted Image's pixel values
function pixExtracted(px, pixel){
var num = pixel.getRed();
px.setRed((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getGreen();
px.setGreen((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getBlue();
px.setBlue((num - ( Math.floor(num /4) * 4)) * 64);
return px;
}
var start = new SimpleImage("eastereggs.jpg");
print(start);
var hide = new SimpleImage("rsz_canvas6.png");
print (hide);
//Cropping Images
var cropw = hide.getWidth();
if(start.getWidth() < cropw){
cropw = start.getWidth();
}
var croph = hide.getHeight();
if(start.getHeight() < croph){
croph = start.getHeight();
}
start = crop(start, cropw, croph);
hide = crop(hide, cropw, croph);
start = chop2hide(start);
hide = shift(hide);
var newImg = combine(start,hide);
print(newImg);
var outimage = new SimpleImage(newImg.getWidth(), newImg.getHeight());
for(var pixel of newImg.values()){
var x = pixel.getX();
var y = pixel.getY();
var px = outimage.getPixel(x, y);
px = pixExtracted(px, pixel);
//Code Section shows how image will look after hidden image is extracted from it.
pixel.setRed(pixel.getRed()-(px.getRed()/64));
pixel.setGreen(pixel.getGreen()-(px.getGreen()/64));
pixel.setBlue(pixel.getBlue()-(px.getBlue()/64));
}
//print extracted image
print(outimage);
//print remaining image after extraction
print(newImg);
//Function returns chopped pixel color values
function pixchange(pixval){
var x = Math.floor(pixval/4) * 4;
return x;
}
//Code section chops bits of pixel colors of image that hides another image
function chop2hide(image){
for(var px of image.values()){
px.setRed(pixchange(px.getRed()));
px.setGreen(pixchange(px.getGreen()));
px.setBlue(pixchange(px.getBlue()));
}
return image;
}
//Code Section used to shift bits of pixel colors of image to be hidden
function shift(im){
var nim = new SimpleImage(im.getWidth(),im.getHeight());
for(var px of im.values()){
var x = px.getX();
var y = px.getY();
var npx = nim.getPixel(x,y);
npx.setRed(Math.floor(px.getRed()/64));
npx.setGreen(Math.floor(px.getGreen()/64));
npx.setBlue(Math.floor(px.getBlue()/64));
}
return nim;
}
//Function returns cropped images
function crop(image,w,h){
var img = new SimpleImage(w,h);
for (var pix of img.values()){
var x = pix.getX();
var y = pix.getY();
var p = image.getPixel(x,y);
pix.setRed(p.getRed());
pix.setGreen(p.getGreen());
pix.setBlue(p.getBlue());
}
return img;
}
//Code Section combines the 2 images into a single one
function combine(image1,image2){
var nimg = new SimpleImage(image1.getWidth(),image1.getHeight());
for (var pix of image1.values()){
var x = pix.getX();
var y = pix.getY();
var p = image2.getPixel(x,y);
var pixel = nimg.getPixel(x,y);
pixel.setRed(pix.getRed()+p.getRed());
pixel.setGreen(pix.getGreen()+p.getGreen());
pixel.setBlue(pix.getBlue()+p.getBlue());
}
return nimg;
}
//Function returns extracted Image's pixel values
function pixExtracted(px, pixel){
var num = pixel.getRed();
px.setRed((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getGreen();
px.setGreen((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getBlue();
px.setBlue((num - ( Math.floor(num /4) * 4)) * 64);
return px;
}
var start = new SimpleImage("eastereggs.jpg");
print(start);
var hide = new SimpleImage("rsz_canvas6.png");
print (hide);
//Cropping Images
var cropw = hide.getWidth();
if(start.getWidth() < cropw){
cropw = start.getWidth();
}
var croph = hide.getHeight();
if(start.getHeight() < croph){
croph = start.getHeight();
}
start = crop(start, cropw, croph);
hide = crop(hide, cropw, croph);
start = chop2hide(start);
hide = shift(hide);
var newImg = combine(start,hide);
print(newImg);
var outimage = new SimpleImage(newImg.getWidth(), newImg.getHeight());
for(var pixel of newImg.values()){
var x = pixel.getX();
var y = pixel.getY();
var px = outimage.getPixel(x, y);
px = pixExtracted(px, pixel);
//Code Section shows how image will look after hidden image is extracted from it.
pixel.setRed(pixel.getRed()-(px.getRed()/64));
pixel.setGreen(pixel.getGreen()-(px.getGreen()/64));
pixel.setBlue(pixel.getBlue()-(px.getBlue()/64));
}
//print extracted image
print(outimage);
//print remaining image after extraction
print(newImg);
<p>The following picture was given with a hidden message to extract:</p>
<center><img src = "http://i36.photobucket.com/albums/e42/Samksha/hiltonHiding2bitMessage_zpsze7d7x8e.png" /></center>
<p>After extraction, this is what the hidden message was!</p>
<center><img src = "http://i36.photobucket.com/albums/e42/Samksha/Part%203_zpsmdhehkly.png"/></center>
<h3> Algorithm</h3>
<p> The algorithm used for the extraction is similar to the one used for extraction in the first part.</p>
<h3> Code</h3>
<pre>
function pchange(pixval)
{
var x = (pixval-Math.floor(pixval/4)*4)*64;
return x;
}
function extract(image)
{
for(var pixel of image.values())
{
pixel.setRed(pchange(pixel.getRed()));
pixel.setGreen(pchange(pixel.getGreen()));
pixel.setBlue(pchange(pixel.getBlue()));
}
return image;
}
var combinedimage = new SimpleImage("hiltonHiding2bitMessage.png");
hiddenimage = extract(combinedimage);
print(hiddenimage);
</pre>
</html>
nice one
Steganography
Original Images
This is the image that will be used to hide the second image!https://www.hdwallpapers.net/abstract/colorful-diamond-wallpaper-674.htm
https://www.hdwallpapers.net/abstract/dark-wood-texture-wallpaper-383.htm
After the Hiding
This is what the image looks like with the hidden value:Algorithm
The algorithm used to hide an image inside another using 2 bits is quite similar to the 4 bits, but has some distinctive differences. Instead of using 2^4, or 16, as the number used for dividing, we will be using 2^2, or 4.The first two bits of the image to be hidden are selected using the shift function. Since it's a 2 bit hidden message, we take the two most significant bits of the image we are hiding, i.e., "Diamond.jpg", by dividing the pixel values by 64.
The first six bits of the image, "Dark Wood.jpg", are obtained using the chop2hide function.
Finally, the combined image is formed by adding the bits of the modified images together.
To get the hidden value back, we simply reverse what we have done to modify the image. This is implemented using the extract function.
Code
var start = new SimpleImage("Dark Wood.jpg");
var hide = new SimpleImage("Diamond.jpg");
function pixchange(pixval)
{
//Since it's only two bits, instead of 2^4, we use 2^2
var x = Math.floor(pixval/4) * 4;
return x;
}
//Function to alter the image to be hidden
function chop2hide(image)
{
for(var p of image.values())
{
p.setRed(pixchange(p.getRed()));
p.setGreen(pixchange(p.getGreen()));
p.setBlue(pixchange(p.getBlue()));
}
return image;
}
//Function to shift the bits of the main image
function shift(im)
{
var nim = new SimpleImage(im.getWidth(), im.getHeight());
for(var p of im.values())
{
var x = p.getX();
var y = p.getY();
var np = nim.getPixel(x,y);
np.setRed(Math.floor(p.getRed()/64));
np.setGreen(Math.floor(p.getGreen()/64));
np.setBlue(Math.floor(p.getBlue()/64));
}
return nim;
}
start_mod = chop2hide(start);
hide_mod = shift(hide);
print(starts); //Prints the first image modified
print(hides); // Prints the second image modified
//Function to add the two numbers
function add(a, b)
{
var ans = a + b;
return ans;
}
//Function to combine both images
function combine(st,hi)
{
var fin = new SimpleImage (st.getWidth(),st.getHeight());
for (var pixel_st of st.values())
{
var x = pixel_st.getX();
var y = pixel_st.getY();
var pixel_hi = hi.getPixel(x,y);
var pixel_fin = fin.getPixel(x,y);
pixel_fin.setRed(add(pixel_st.getRed(),pixel_hi.getRed()));
pixel_fin.setGreen(add(pixel_st.getGreen(),pixel_hi.getGreen()));
pixel_fin.setBlue(add(pixel_st.getBlue(),pixel_hi.getBlue()));
}
return fin;
}
combinedimage=combine(start_mod,hide_mod);
print(combinedimage); //Prints combined image
//Function to get the hidden value back
function pchange(pixval)
{
var x = (pixval-Math.floor(pixval/4)*4)*64;
return x;
}
//Function to extract the hidden image
function extract(image)
{
for(var pixel of image.values())
{
pixel.setRed(pchange(pixel.getRed()));
pixel.setGreen(pchange(pixel.getGreen()));
pixel.setBlue(pchange(pixel.getBlue()));
}
return image;
}
hiddenimage = extract(combinedimage);
print(hiddenimage); //Prints hidden image
Hidden Message in Given Picture
The following picture was given with a hidden message to extract:Algorithm
The algorithm used for the extraction is similar to the one used for extraction in the first part.Code
function pchange(pixval)
{
var x = (pixval-Math.floor(pixval/4)*4)*64;
return x;
}
function extract(image)
{
for(var pixel of image.values())
{
pixel.setRed(pchange(pixel.getRed()));
pixel.setGreen(pchange(pixel.getGreen()));
pixel.setBlue(pchange(pixel.getBlue()));
}
return image;
}
var combinedimage = new SimpleImage("hiltonHiding2bitMessage.png");
hiddenimage = extract(combinedimage);
print(hiddenimage);
Hide And Extract Image 2 bits
//Function returns chopped pixel color values
function pixchange(pixval){
var x = Math.floor(pixval/4) * 4;
return x;
}
//Code section chops bits of pixel colors of image that hides another image
function chop2hide(image){
for(var px of image.values()){
px.setRed(pixchange(px.getRed()));
px.setGreen(pixchange(px.getGreen()));
px.setBlue(pixchange(px.getBlue()));
}
return image;
}
//Code Section used to shift bits of pixel colors of image to be hidden
function shift(im){
var nim = new SimpleImage(im.getWidth(),im.getHeight());
for(var px of im.values()){
var x = px.getX();
var y = px.getY();
var npx = nim.getPixel(x,y);
npx.setRed(Math.floor(px.getRed()/64));
npx.setGreen(Math.floor(px.getGreen()/64));
npx.setBlue(Math.floor(px.getBlue()/64));
}
return nim;
}
//Function returns cropped images
function crop(image,w,h){
var img = new SimpleImage(w,h);
for (var pix of img.values()){
var x = pix.getX();
var y = pix.getY();
var p = image.getPixel(x,y);
pix.setRed(p.getRed());
pix.setGreen(p.getGreen());
pix.setBlue(p.getBlue());
}
return img;
}
//Code Section combines the 2 images into a single one
function combine(image1,image2){
var nimg = new SimpleImage(image1.getWidth(),image1.getHeight());
for (var pix of image1.values()){
var x = pix.getX();
var y = pix.getY();
var p = image2.getPixel(x,y);
var pixel = nimg.getPixel(x,y);
pixel.setRed(pix.getRed()+p.getRed());
pixel.setGreen(pix.getGreen()+p.getGreen());
pixel.setBlue(pix.getBlue()+p.getBlue());
}
return nimg;
}
//Function returns extracted Image's pixel values
function pixExtracted(px, pixel){
var num = pixel.getRed();
px.setRed((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getGreen();
px.setGreen((num - ( Math.floor(num /4) * 4)) * 64);
num = pixel.getBlue();
px.setBlue((num - ( Math.floor(num /4) * 4)) * 64);
return px;
}
var start = new SimpleImage("eastereggs.jpg");
print(start);
var hide = new SimpleImage("csife.png");
print (hide);
//Cropping Images
var cropw = hide.getWidth();
if(start.getWidth() < cropw){
cropw = start.getWidth();
}
var croph = hide.getHeight();
if(start.getHeight() < croph){
croph = start.getHeight();
}
start = crop(start, cropw, croph);
hide = crop(hide, cropw, croph);
start = chop2hide(start);
hide = shift(hide);
var newImg = combine(start,hide);
print(newImg);
var outimage = new SimpleImage(newImg.getWidth(), newImg.getHeight());
for(var pixel of newImg.values()){
var x = pixel.getX();
var y = pixel.getY();
var px = outimage.getPixel(x, y);
px = pixExtracted(px, pixel);
//Code Section shows how image will look after hidden image is extracted from it.
pixel.setRed(pixel.getRed()-(px.getRed()/64));
pixel.setGreen(pixel.getGreen()-(px.getGreen()/64));
pixel.setBlue(pixel.getBlue()-(px.getBlue()/64));
}
//print extracted image
print(outimage);
//print remaining image after extraction
print(newImg);
Modify Image Algorithmically
function ensureInImage(coordinate, size){
//coordinate cannot be negative
if (coordinate < 0){
return 0;
}
//coordinate size must be between [0....size-1]
if (coordinate >= size){
return size-1;
}
else{
return coordinate;
}
}
function getPixelNearby(image , x, y, diameter){
var dx = Math.random() * diameter - diameter/2;
var dy = Math.random() * diameter - diameter/2;
var nx = ensureInImage(x + dx, image.getWidth());
var ny = ensureInImage(y + dy, image.getHeight());
return image.getPixel(nx, ny);
}
//Select An Image from the list
var image = new SimpleImage ("chapel.png");
//I'm gonna create a new image that is the same size as my starting image.
var output = new SimpleImage(image.getWidth() , image.getHeight() );
//for all the values of pixel of the image the values of x and y are taken and math.random is done since we dont want the new pixel more than the radius of some walue we enter 10 or any random number depending uopn the number we enter the blurrier the image
for (var pixel of image.values()){
var x = pixel.getX();
var y = pixel.getY();
if (Math.random() > 0.5) {
//we call the getPixelNearby(image , x, y, diameter) which inturn calls the ensureInImage(coordinate, size)
var other = getPixelNearby(image , x, y, 10);
output.setPixel(x, y, other);
}
else{
output.setPixel(x, y, pixel);
}
}
print (output);
print (image);
Creating a Image Algorithmic-ally
function dist(pixel,x,y){
var dx = pixel.getX() - x ;
var dy = pixel.getY() - y ;
return Math.sqrt(dx * dx + dy * dy);
}
// start with a blank image
var output = new SimpleImage(320,320);
// write something here
for (var pixel of output.values()){
if (dist(pixel,100,100) < 50 ){
pixel.setRed(255 - 4 * dist (pixel,100,100));
}
else if (dist(pixel,200,200) < 80 ){
pixel.setGreen(255 - 4 * dist (pixel,200,200));
}
else if (Math.random() > 0.995 ){
pixel.setRed(255);
pixel.setGreen(255);
}
pixel.setBlue(Math.max(1.05 * pixel.getY() - pixel.getX() , pixel.getX() - 1.5 * pixel.getY() ))
}
print (output);
var dx = pixel.getX() - x ;
var dy = pixel.getY() - y ;
return Math.sqrt(dx * dx + dy * dy);
}
// start with a blank image
var output = new SimpleImage(320,320);
// write something here
for (var pixel of output.values()){
if (dist(pixel,100,100) < 50 ){
pixel.setRed(255 - 4 * dist (pixel,100,100));
}
else if (dist(pixel,200,200) < 80 ){
pixel.setGreen(255 - 4 * dist (pixel,200,200));
}
else if (Math.random() > 0.995 ){
pixel.setRed(255);
pixel.setGreen(255);
}
pixel.setBlue(Math.max(1.05 * pixel.getY() - pixel.getX() , pixel.getX() - 1.5 * pixel.getY() ))
}
print (output);
OR
//all the points here are numbered in the same way the numbers are displayed in code as comments
// Since The Function Is Called It runs And creates a circle
function dist(pixel,x,y){
var dx = pixel.getX() - x ;
var dy = pixel.getY() - y ;
return Math.sqrt(dx * dx + dy * dy);
}
// I started with a blank image of size 256*256 as shown in the code below with Comment as 1
var output = new SimpleImage(256,256);
// now will will take the cordinates of the image(which i consider as mercury) as 80,80 & green(earth) as 255,255 blue(venus) as 150,150 and so on and create a circle of size 25 by calling the function dist
for (var pixel of output.values()){
if (dist(pixel,80,80) < 25 ){
pixel.setRed(255 - 4 * dist (pixel,80,80));
}
else if (dist(pixel,255,255) < 80 ){
pixel.setGreen(255 - 2.5 * dist (pixel,255,255));
}
else if (dist(pixel,150,150) < 60 ){
pixel.setBlue(255 - 3.5 * dist (pixel,150,150));
}
else if (dist(pixel,1,1) < 80 ){
(pixel.setGreen(205)+pixel.setRed(205)) - 3.5 * dist (pixel,1,1);
}
else if (Math.random() > 0.995 ){
pixel.setRed(255);
pixel.setGreen(255);
}
//pixel.setBlue(Math.max( 1.05 * pixel.getX() - pixel.getY() , pixel.getY() - 1.05 * pixel.getX() ))
}
print (output);
EXTRACTION OF IMAGE code
// write your code here
function clearbits(colorval){
//compute the same color value with the lowest bits zeroed
var x = Math.floor(colorval/16)*16;
return x;
}
function chop2hide(image){
//for each pixel in the image
for (var px of image.values()){
//clear the lower values of red
px.setRed(clearbits(px.getRed()));
//clear the lower values of green
px.setGreen(clearbits(px.getGreen()));
//clear the lower values of blue
px.setBlue(clearbits(px.getBlue()));
}
return image;
}
function shift(image){
//for each pixel in an image
for (var px of image.values()){
//shift the red bits over
px.setRed(px.getRed()/16);
//shift the Green bits over
px.setGreen(px.getGreen()/16);
//shift the Blue bits over
px.setBlue(px.getBlue()/16);
}
return image;
}
function open(imagetoopen){
for (var px of imagetoopen.values()){
var or = px.getRed();
var og = px.getGreen();
var ob = px.getBlue();
px.setRed((or - (clearbits(or)))*16);
px.setGreen((og - (clearbits(og)) )*16);
px.setBlue((ob - (clearbits(ob)))*16);
}
return imagetoopen;
}
function combine(start,Hide){
//make a new image of the same size as SHOW (Call it Answer)
var answer = new SimpleImage(start.getWidth() , start.getHeight());
//for every pixel of new image
for (var px of answer.values()){
var x = px.getX();
var y = px.getY();
//get the pixel in the same place from show
var startPixel = start.getPixel(x,y);
//get the pixel in the same place from hide
var hidePixel = hide.getPixel(x,y);
//set the red pixel to the sum of the show pixel red + hide pixel red
px.setRed(startPixel.getRed() + hidePixel.getRed());
//set the Green pixel to the sum of the show pixel Green + hide pixel Green
px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
//set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
}
return answer;
}
var start = new SimpleImage("dinos.png");
var Hide = new SimpleImage("drewRobert.png");
start = chop2hide(start);
hide = shift(Hide);
var answer = combine (start,hide);
print (answer);
var undo = open(answer);
print (undo);
function clearbits(colorval){
//compute the same color value with the lowest bits zeroed
var x = Math.floor(colorval/16)*16;
return x;
}
function chop2hide(image){
//for each pixel in the image
for (var px of image.values()){
//clear the lower values of red
px.setRed(clearbits(px.getRed()));
//clear the lower values of green
px.setGreen(clearbits(px.getGreen()));
//clear the lower values of blue
px.setBlue(clearbits(px.getBlue()));
}
return image;
}
function shift(image){
//for each pixel in an image
for (var px of image.values()){
//shift the red bits over
px.setRed(px.getRed()/16);
//shift the Green bits over
px.setGreen(px.getGreen()/16);
//shift the Blue bits over
px.setBlue(px.getBlue()/16);
}
return image;
}
function open(imagetoopen){
for (var px of imagetoopen.values()){
var or = px.getRed();
var og = px.getGreen();
var ob = px.getBlue();
px.setRed((or - (clearbits(or)))*16);
px.setGreen((og - (clearbits(og)) )*16);
px.setBlue((ob - (clearbits(ob)))*16);
}
return imagetoopen;
}
function combine(start,Hide){
//make a new image of the same size as SHOW (Call it Answer)
var answer = new SimpleImage(start.getWidth() , start.getHeight());
//for every pixel of new image
for (var px of answer.values()){
var x = px.getX();
var y = px.getY();
//get the pixel in the same place from show
var startPixel = start.getPixel(x,y);
//get the pixel in the same place from hide
var hidePixel = hide.getPixel(x,y);
//set the red pixel to the sum of the show pixel red + hide pixel red
px.setRed(startPixel.getRed() + hidePixel.getRed());
//set the Green pixel to the sum of the show pixel Green + hide pixel Green
px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
//set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
}
return answer;
}
var start = new SimpleImage("dinos.png");
var Hide = new SimpleImage("drewRobert.png");
start = chop2hide(start);
hide = shift(Hide);
var answer = combine (start,hide);
print (answer);
var undo = open(answer);
print (undo);
Suppose we decide to write a function to set the color of a pixel by giving it three RGB values. Think about what the parameters and the code would need to be for this function. Let’s call the function setColor. Which one of the following is a correct setColor function?
// write your code here
function clearbits(colorval){
//compute the same color value with the lowest bits zeroed
var x = Math.floor(colorval/16)*16;
return x;
}
function setColor( r, g, b) {
for (var pixel of start.values()){
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}
return start;
}
function chop2hide(image){
//for each pixel in the image
for (var px of image.values()){
//clear the lower values of red
px.setRed(clearbits(px.getRed()));
//clear the lower values of green
px.setGreen(clearbits(px.getGreen()));
//clear the lower values of blue
px.setBlue(clearbits(px.getBlue()));
}
return image;
}
function shift(image){
//for each pixel in an image
for (var px of image.values()){
//shift the red bits over
px.setRed(px.getRed()/16);
//shift the Green bits over
px.setGreen(px.getGreen()/16);
//shift the Blue bits over
px.setBlue(px.getBlue()/16);
}
return image;
}
function open(imagetoopen){
for (var px of imagetoopen.values()){
var or = px.getRed();
var og = px.getGreen();
var ob = px.getBlue();
px.setRed((or - (clearbits(or)))+16);
px.setGreen((og - (clearbits(og)) )+16);
px.setBlue((ob - (clearbits(ob)))+16);
}
return imagetoopen;
}
function combine(start,Hide){
//make a new image of the same size as SHOW (Call it Answer)
var answer = new SimpleImage(start.getWidth() , start.getHeight());
//for every pixel of new image
for (var px of answer.values()){
var x = px.getX();
var y = px.getY();
//get the pixel in the same place from show
var startPixel = start.getPixel(x,y);
//get the pixel in the same place from hide
var hidePixel = hide.getPixel(x,y);
//set the red pixel to the sum of the show pixel red + hide pixel red
px.setRed(startPixel.getRed() + hidePixel.getRed());
//set the Green pixel to the sum of the show pixel Green + hide pixel Green
px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
//set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
}
return answer;
}
var start = new SimpleImage("test2.png");
var newcolor = setColor(232,12,20)
//var Hide = new SimpleImage("drewRobert.png");
//start = chop2hide(start);
//hide = shift(Hide);
//var answer = combine (start,hide);
//print (answer);
//var undo = open(start);
//print (undo);
print (newcolor);
function clearbits(colorval){
//compute the same color value with the lowest bits zeroed
var x = Math.floor(colorval/16)*16;
return x;
}
function setColor( r, g, b) {
for (var pixel of start.values()){
pixel.setRed(r);
pixel.setGreen(g);
pixel.setBlue(b);
}
return start;
}
function chop2hide(image){
//for each pixel in the image
for (var px of image.values()){
//clear the lower values of red
px.setRed(clearbits(px.getRed()));
//clear the lower values of green
px.setGreen(clearbits(px.getGreen()));
//clear the lower values of blue
px.setBlue(clearbits(px.getBlue()));
}
return image;
}
function shift(image){
//for each pixel in an image
for (var px of image.values()){
//shift the red bits over
px.setRed(px.getRed()/16);
//shift the Green bits over
px.setGreen(px.getGreen()/16);
//shift the Blue bits over
px.setBlue(px.getBlue()/16);
}
return image;
}
function open(imagetoopen){
for (var px of imagetoopen.values()){
var or = px.getRed();
var og = px.getGreen();
var ob = px.getBlue();
px.setRed((or - (clearbits(or)))+16);
px.setGreen((og - (clearbits(og)) )+16);
px.setBlue((ob - (clearbits(ob)))+16);
}
return imagetoopen;
}
function combine(start,Hide){
//make a new image of the same size as SHOW (Call it Answer)
var answer = new SimpleImage(start.getWidth() , start.getHeight());
//for every pixel of new image
for (var px of answer.values()){
var x = px.getX();
var y = px.getY();
//get the pixel in the same place from show
var startPixel = start.getPixel(x,y);
//get the pixel in the same place from hide
var hidePixel = hide.getPixel(x,y);
//set the red pixel to the sum of the show pixel red + hide pixel red
px.setRed(startPixel.getRed() + hidePixel.getRed());
//set the Green pixel to the sum of the show pixel Green + hide pixel Green
px.setGreen(startPixel.getGreen() + hidePixel.getGreen());
//set the Blue pixel to the sum of the show pixel Blue + hide pixel Blue
px.setBlue(startPixel.getBlue() + hidePixel.getBlue());
}
return answer;
}
var start = new SimpleImage("test2.png");
var newcolor = setColor(232,12,20)
//var Hide = new SimpleImage("drewRobert.png");
//start = chop2hide(start);
//hide = shift(Hide);
//var answer = combine (start,hide);
//print (answer);
//var undo = open(start);
//print (undo);
print (newcolor);
Subscribe to:
Posts (Atom)
కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము?
కరోనా కోవిడ్ -19 గురించి ఏ వికీపీడియా మీకు చెప్పలేము? మిమ్మల్ని మీరు రక్షించుకోండి Your మీ చేతులను తరచుగా కడగాలి Eyes మీ కళ్ళు, న...
-
An intriguing report this week suggests that Samsung may be imitating the iPhone range when the Galaxy Note 8 hits the stores. With Samsun...