Incorrect window size with OCaml Graphics - graphics

Description of the issue
When using OCaml Graphics module, I often get a window with a wrong size ; and this isn't a predictable behaviour. The displayed window always has the required size, but for an unknown reason, OCaml sometimes considers that it has actually a different size, and this affects the drawing consequently.
Perhaps it is related to the window system ; I am using X11 (XQuartz) on OS X.
Evidence of the issue
Consider the following sequence :
open Graphics
let f () =
Printf.printf "size_x = %d, size_y = %d\n" (size_x ()) (size_y ())
let () =
open_graph "";
resize_window 500 500;
f ();
Unix.sleepf 0.01
f ();
Unix.sleepf 0.1
f ()
The expected result is a window of constant size 500x500, that is :
size_x = 500, size_y = 500
size_x = 500, size_y = 500
size_x = 500, size_y = 500
But I also get sometimes (with a strong probability)
size_x = 500, size_y = 500
size_x = 600, size_y = 450
size_x = 500, size_y = 500
or
size_x = 600, size_y = 450
size_x = 500, size_y = 500
size_x = 500, size_y = 500
or
size_x = 600, size_y = 450
size_x = 600, size_y = 450
size_x = 500, size_y = 500
In other words, this is a variable behaviour that seems to occur only a few tenths of a second after the opening of the window.
The result is the same whether I turn options auto_synchronize and display_mode true or false.
Workaround
I make sure all resize_window instructions are followed by Unix.sleepf 0.1 at least (or by synchronize (); Unix.sleepf 0.1 when auto_synchronize is turned off) ; of course, this isn't a very satisfactory solution…

Related

VTK: why the vtkImageBlend result is different from RadiAnt

Let's say I have two image, and the artery (the red arrow) is about the same position:
Now, I need to show the two image in one figure, and I use the vtkImageBlend for this purpose. My code is:
import vtk
img1 = vtk.vtkDICOMImageReader()
img1.SetFileName('C:\\Users\\MLoong\\Desktop\\dicom_data\\Chang Cheng\\TOF\\IM_0198')
img1.Update()
print('img1: ', img1.GetOutput().GetSpacing())
print('img1: ', img1.GetOutput().GetExtent())
img2 = vtk.vtkDICOMImageReader()
img2.SetFileName('C:\\Users\\MLoong\\Desktop\\dicom_data\\Chang Cheng\\SNAP\\IM_0502')
img2.Update()
print('img2: ', img2.GetOutput().GetSpacing())
print('img2: ', img2.GetOutput().GetExtent())
image_blender = vtk.vtkImageBlend()
image_blender.AddInputConnection(img1.GetOutputPort())
image_blender.AddInputConnection(img2.GetOutputPort())
image_blender.SetOpacity(0, 0.1)
image_blender.SetOpacity(1, 0.9)
image_blender.Update()
imageActor = vtk.vtkImageActor()
windowLevel = vtk.vtkImageMapToWindowLevelColors()
imageActor.GetMapper().SetInputConnection(windowLevel.GetOutputPort())
ren = vtk.vtkRenderer()
ren.AddActor(imageActor)
ren.SetBackground(0.1, 0.2, 0.4)
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(400, 400)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
windowLevel.SetInputData(image_blender.GetOutput())
windowLevel.Update()
renWin.Render()
iren.Start()
And the result is:
In the above figure, the img2 is about half ot the img1.
Hoever, the printed information is:
img1: (0.3571428656578064, 0.3571428656578064, 1.399999976158142)
img1: (0, 559, 0, 559, 0, 0)
img2: (0.5, 0.5, 1.0)
img2: (0, 319, 0, 319, 0, 0)
For img1, the extent is (560, 560), and the spacing is (0.357, 0.357). Thus, the FOV is: 0.357*560=200, and the FOV of img2 is 160. Thus, I think the blend figure may be wrong.
Moreover, the RadiAnt also provide the fusion figure:
In the RadiAnt fusion figure, the artery of two images is overlay, which is what I want.
Is there anything wrong with my vtkImageBlend code?
From the Detailed Description of vtkImageBlend:
"blend images together using alpha or opacity
vtkImageBlend takes L, LA, RGB, or RGBA images as input and blends them according to the alpha values and/or the opacity setting for each input.
The spacing, origin, extent, and number of components of the output are the same as those for the first input."
So, you need to make sure to have the same spacing, extent and origin.
The way to solve it is to create new 2 vtkImageData with the same properties and copy your existing data to the new images.
Here I handle extent:
private static void AdjustImage(ref vtkImageData oldImage, int[] newDimensions)
{
vtkImageData newImage = new vtkImageData();
vtkInformation info = new vtkInformation();
int[] oldDimensions = oldImage.GetDimensions();
double[] spacing = oldImage.GetSpacing();
newImage.SetDimensions(newDimensions[0], newDimensions[1], newDimensions[2]);
vtkImageData.SetScalarType(4/*VTK_SHORT*/, info);
vtkImageData.SetNumberOfScalarComponents(1, info);//The components that each pixel needs to represent =1 is the index quantity map
newImage.AllocateScalars(info);//It is very important to allocate memory and generate image data. After the image is generated, the default value of all pixels is 0
newImage.SetSpacing(spacing[0], spacing[1], spacing[2]);
vtkImageData data = oldImage;
Parallel.For(0, newDimensions[0], i =>
{
if (i < oldDimensions[0])
{
Parallel.For(0, newDimensions[1], j =>
{
if (j < oldDimensions[1])
{
Parallel.For(0, newDimensions[2], k =>
{
if (k < oldDimensions[2])
{
newImage.SetScalarComponentFromDouble(i, j,
newDimensions[2] - 1 - k, 0,
data.GetScalarComponentAsDouble(i, j,
oldDimensions[2] - 1 - k, 0));
}
else
{
SetImageToDefault(newImage, newDimensions, i, j, k);
}
});
}
else
{
SetImageToDefault(newImage, newDimensions, i, j);
}
});
}
else
{
SetImageToDefault(newImage, newDimensions, i);
}
});
oldImage.Dispose();
oldImage = newImage;
}
private static void SetImageToDefault(vtkImageData img, int[] imageDimensions, int i, int j, int k)
{
const double transparentHu = -1000;
img.SetScalarComponentFromDouble(i, j, imageDimensions[2] - 1 - k, 0, transparentHu);
}
Afterward you will need to translate the second image by the delta of the differences between the 2 images origins.

OpenCV Compare values through matrices for error level analysis

My aim is to generate an error level analysis workflow using opencv and python, and I found this example doing what I need, but in C++ : Error level analysis in Image
While I managed to convert a part of it I'm really stuck on the loop rows part due to my lack of C++ knowledge, so I could use some support here.
This is what I managed to convert :
path=r'C:\****\training\pop_test'
image='source.jpg'
scale = 15
jpg_quality = 75
input_image= cv2.imread(os.path.join(path, image))
w,h,d = img.shape
# save tmp compressed file
cv2.imwrite(os.path.join(path, 'tmp.jpg'), input_image, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality])
compressed_image= cv2.imread(os.path.join(path, 'tmp.jpg'))
# create matrice
output_image = np.zeros((w, h, d), dtype = "uint8")
And this is the C++ I'm having a hard time with :
for (int row = 0; row < input_image.rows; ++row)
{
const uchar* ptr_input = input_image.ptr<uchar>(row);
const uchar* ptr_compressed = compressed_image.ptr<uchar>(row);
uchar* ptr_out = output_image.ptr<uchar>(row);
for (int column = 0; column < input_image.cols; column++)
{
// Calc abs diff for each color channel multiplying by a scale factor
ptr_out[0] = abs(ptr_input[0] - ptr_compressed[0]) * scale;
ptr_out[1] = abs(ptr_input[1] - ptr_compressed[1]) * scale;
ptr_out[2] = abs(ptr_input[2] - ptr_compressed[2]) * scale;
ptr_input += 3;
ptr_compressed += 3;
ptr_out += 3;
}
}
Anyone that can point me in the right direction?
I think this is what you want for ELA in Python/OpenCV (from reading information at http://www.hackerfactor.com/papers/bh-usa-07-krawetz-wp.pdf
Input:
import cv2
import numpy as np
# read image
img1 = cv2.imread("lenna.png")
# set compression and scale
jpg_quality1 = 95
jpg_quality2 = 90
scale = 15
# write img1 at 95% jpg compression
cv2.imwrite("lenna_c95.jpg", img1, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality1])
# read compressed image
img2 = cv2.imread("lenna_c95.jpg")
# get absolute difference between img1 and img2 and multiply by scale
diff1 = scale * cv2.absdiff(img1, img2)
# write img2 at 90% jpg compression
cv2.imwrite("lenna_c90.jpg", img2, [cv2.IMWRITE_JPEG_QUALITY, jpg_quality2])
# read compressed image
img3 = cv2.imread("lenna_c90.jpg")
# get absolute difference between img1 and img2 and multiply by scale
diff2 = scale * cv2.absdiff(img2, img3)
# write result to disk
cv2.imwrite("lenna_ela_95.jpg", diff1)
cv2.imwrite("lenna_ela_90.jpg", diff2)
# display it
cv2.imshow("ela95", diff1)
cv2.imshow("ela90", diff2)
cv2.waitKey(0)
ELA result at 95%:
ELA result at 90%:
I found this code in this kaggle code. It does the same thing that the #fmw42 just added as a response, but it uses PIL library tools. I hope it come in handy:
def convert_to_ela_image(path, quality):
temp_filename = 'temp_file_name.jpg'
ela_filename = 'temp_ela.png'
image = Image.open(path).convert('RGB')
image.save(temp_filename, 'JPEG', quality = quality)
temp_image = Image.open(temp_filename)
ela_image = ImageChops.difference(image, temp_image)
extrema = ela_image.getextrema()
max_diff = max([ex[1] for ex in extrema])
if max_diff == 0:
max_diff = 1
scale = 255.0 / max_diff
ela_image = ImageEnhance.Brightness(ela_image).enhance(scale)
return ela_image
real_image_path = '/kaggle/input/casia-dataset/casia/CASIA2/Au/Au_ani_00001.jpg'
Image.open(real_image_path)
convert_to_ela_image(real_image_path, 90)

How to get the average color of a specific area in a webcam feed (Processing/JavaScript)?

I'm using Processing to get a webcam feed from my laptop. In the top left corner, I have drawn a rectangle over the displayed feed. I'm trying to get the average color of the webcam, but only in the region contained by that rectangle.
I keep getting color (0, 0, 0), black, as the result.
Thank you all!
PS sorry if my code seems messy..I'm new at Processing and so I don't know if this might be hard to read or contain bad practices. Thank you.
import processing.video.*;
Capture webcam;
Capture cap;
PImage bg_img;
color bgColor = color(0, 0, 0);
int rMargin = 50;
int rWidth = 100;
color input = color(0, 0, 0);
color background = color(255, 255, 255);
color current;
int bgTolerance = 5;
void setup() {
size(1280,720);
// start the webcam
String[] inputs = Capture.list();
if (inputs.length == 0) {
println("Couldn't detect any webcams connected!");
exit();
}
webcam = new Capture(this, inputs[0]);
webcam.start();
}
void draw() {
if (webcam.available()) {
// read from the webcam
webcam.read();
image(webcam, 0,0);
webcam.loadPixels();
noFill();
strokeWeight(2);
stroke(255,255, 255);
rect(rMargin, rMargin, rWidth, rWidth);
int yCenter = (rWidth/2) + rMargin;
int xCenter = (rWidth/2) + rMargin;
// rectMode(CENTER);
int rectCenterIndex = (width* yCenter) + xCenter;
int r = 0, g = 0, b = 0;
//for whole image:
//for (int i=0; i<bg_img.pixels.length; i++) {
// color c = bg_img.pixels[i];
// r += c>>16&0xFF;
// g += c>>8&0xFF;
// b += c&0xFF;
//}
//r /= bg_img.pixels.length;
//g /= bg_img.pixels.length;
//b /= bg_img.pixels.length;
//CALCULATE AVG COLOR:
int i;
for(int x = 50; x <= 150; x++){
for(int y = 50; y <= 150; y++){
i = (width*y) + x;
color c = webcam.pixels[i];
r += c>>16&0xFF;
g += c>>8&0xFF;
b += c&0xFF;
}
}
r /= webcam.pixels.length;
g /= webcam.pixels.length;
b /= webcam.pixels.length;
println(r + " " + g + " " + b);
}
}
You're so close, but missing out one important aspect: the number of pixels you're sampling.
Notice in the example code that is commented out for a full image you're dividing by the full number of pixels (pixels.length).
However, in your adapted version you want to compute the average colour of only a subsection of the full image which means a smaller number of pixels.
You're only sampling an area that is 100x100 pixels meaning you need to divide by 10000 instead of webcam.pixels.length (1920x1000). That is why you get 0 as it's integer division.
This is what I mean in code:
int totalSampledPixels = rWidth * rWidth;
r /= totalSampledPixels;
g /= totalSampledPixels;
b /= totalSampledPixels;
Full tweaked sketch:
import processing.video.*;
Capture webcam;
Capture cap;
PImage bg_img;
color bgColor = color(0, 0, 0);
int rMargin = 50;
int rWidth = 100;
int rHeight = 100;
color input = color(0, 0, 0);
color background = color(255, 255, 255);
color current;
int bgTolerance = 5;
void setup() {
size(1280,720);
// start the webcam
String[] inputs = Capture.list();
if (inputs.length == 0) {
println("Couldn't detect any webcams connected!");
exit();
}
webcam = new Capture(this, inputs[0]);
webcam.start();
}
void draw() {
if (webcam.available()) {
// read from the webcam
webcam.read();
image(webcam, 0,0);
webcam.loadPixels();
noFill();
strokeWeight(2);
stroke(255,255, 255);
rect(rMargin, rMargin, rWidth, rHeight);
int yCenter = (rWidth/2) + rMargin;
int xCenter = (rWidth/2) + rMargin;
// rectMode(CENTER);
int rectCenterIndex = (width* yCenter) + xCenter;
int r = 0, g = 0, b = 0;
//for whole image:
//for (int i=0; i<bg_img.pixels.length; i++) {
// color c = bg_img.pixels[i];
// r += c>>16&0xFF;
// g += c>>8&0xFF;
// b += c&0xFF;
//}
//r /= bg_img.pixels.length;
//g /= bg_img.pixels.length;
//b /= bg_img.pixels.length;
//CALCULATE AVG COLOR:
int i;
for(int x = 0; x <= width; x++){
for(int y = 0; y <= height; y++){
if (x >= rMargin && x <= rMargin + rWidth && y >= rMargin && y <= rMargin + rHeight){
i = (width*y) + x;
color c = webcam.pixels[i];
r += c>>16&0xFF;
g += c>>8&0xFF;
b += c&0xFF;
}
}
}
//divide by just the area sampled (x >= 50 && x <= 150 && y >= 50 && y <= 150 is a 100x100 px area)
int totalSampledPixels = rWidth * rHeight;
r /= totalSampledPixels;
g /= totalSampledPixels;
b /= totalSampledPixels;
fill(r,g,b);
rect(rMargin + rWidth, rMargin, rWidth, rHeight);
println(r + " " + g + " " + b);
}
}
Bare in mind this is averaging in the RGB colour space which is not the same as perceptual colour space. For example, if you average red and yellow you'd expect orange, but in RGB, a bit of red and green makes yellow.
Hopefully the RGB average is good enough for what you need, otherwise you may need to convert from RGB to CIE XYZ colour space then to Lab colour space to compute the perceptual average (then convert back to XYZ and RGB to display on screen). If that is something you're interested in trying, you can find an older answer demonstrating this in openFrameworks (which you'll notice can be similar to Processing in simple scenarios).

Using a table in Lua to display text isn't working?

I'm using Corona SDK, Sublime Text 3, and Lua. My goal in this particular program is to make a visual novel app that changes when the player clicks on the text displayed at the bottom of the screen to progress to the next text entry, therefore continuing the story.
I set up a bunch of code for logo, title screen, and other things, that works perfectly. What I'm currently trying to do within the visual novel scene is to use a table to draw the text from by change the .TEXT property to select a certain value from the table, therefore selecting the text and making that the new text. Basically, something like... (some dummy code below)
novelText = display.newText (insert the parameters for old text and the old text)
--the variable used to call the value in the table
page = 1
dummy table Novel_pages = {
[1] = nil,
[2] = "new text"
}
(insert runtime event here that calls this function)
page = page + 1
novelText.text = Novel_pages[page]
display.newText(novelText)
That was just dummy code, so please don't mind the format. :) I just want to show how I attempted to call these values from the table, and to show what I was doing without having to make people look through all my code.
So everything works fine in Corona SDK simulator, with the text even changing just momentarily -- until one second later, I get a message that reads
"mainl.lua:160: bad argument #1 to 'newText' (string expected, got table)
stack traceback:
[C]: in function 'NewText'
main.lua:160: in function <main.lua: 156>
?: in function <?169>"
Now's the part where I give you all my code! I hope it's not too much, and that I specified the problem enough. I can't see where I made the error in the table, since it should be JUST replacing the .text and not all the other properties? And then displaying with the new text properties and not have to reference the table at all afterwards? Perhaps there's a problem with needing the program to process the .text change before displaying the visual novel text...
Anyway, please help me! I would appreciate knowing what went wrong here, or being proposed an alternative! And thank you so much :)
Here's the code -- everything starts in function sceneVN()! And please excuse my cringy dialogue ingame c: It's a practice project!
local store = require( "plugin.google.iap.v3" )
local composer = require("composer")
local scene = composer.newScene()
display.setStatusBar( display.HiddenStatusBar ) -- Removes status bar
coins = 5 -- variable that defines the number of coins player has in the game.It will be different
--in a stand alone game, but as a small app meant to demonstrate function, it's necessary to start off
--with a small, defined number in the beginning.
local logo = display.newImage("/Images/logo.png", 155, 275) --code for my personal logo, drawn by me.
--Not actually showcased in the video because of time limit.
logo.alpha = 0
local function makeTitleTrue()
logo:removeSelf()
print("menu should be TRUE")
titleScreen()
end
local function fadeOut()
transition.to(logo, {time = 1000, alpha = 0, onComplete = makeTitleTrue})
end
transition.to(logo, {time = 1000, alpha = 1, onComplete = fadeOut}) -- end of logo code
function titleScreen() -- beginning of title code, which is not managed as a separate scene
title = true
titleImg = display.newImage("/Images/vn_bg.png", 155, 275)
--titleWords = display.newImage("/Images/TitleWords.png", 155, 275)
--fix to flow towards the upper right corner.
local flare = display.newImage("/Images/flare2.png", 40, 30)
flare.xScale = .5
flare.yScale = .5
local flare2 = display.newImage("/Images/flare2.png", 400, 70)
flare2.xScale = .6
flare2.yScale = .6
local flare3 = display.newImage("/Images/flare2.png", -30, 100)
flare3.xScale = .4
flare3.yScale = .4
local flare4 = display.newImage("/Images/flare2.png", 100, 400)
flare4.xScale = .4
flare4.yScale = .4
local flare5 = display.newImage("/Images/flare2.png", 400, 400)
flare5.xScale = .3
flare5.yScale = .3
local flare6 = display.newImage("/Images/flare2.png", 250, 200)
flare6.xScale = .3
flare6.yScale = .3
local function moveFlare1()
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
end
local function moveFlare2()
transition.to(flare2, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
end
local function moveFlare3()
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
end
local function moveFlare4()
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
end
local function moveFlare5()
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
end
local function moveFlare6()
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
end
transition.to(flare, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare1})
transition.to(flare2, {time=2500, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare2})
transition.to(flare3, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare3})
transition.to(flare4, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare4})
transition.to(flare5, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare5})
transition.to(flare6, {time=2000, x = math.random(-100, 450), y = math.random(-100, 700), onComplete = moveFlare6})
--add options that can when the screen is tapped, tap on an option twice to select
-- start story
-- continue story
-- coin gambling
-- end game
if (title == true) then
Runtime:addEventListener("tap", sceneVN)
end
end
function forceQuit()
function quit()
os.exit()
end
timer.performWithDelay(1000,quit)
end
function sceneVNChapter2()
return
end
function sceneVN() -- the actual visual novel code itself
display.remove(titleImg)
--display.remove(titleWords)
display.remove(flare)
display.remove(flare2)
display.remove(flare3)
display.remove(flare4)
display.remove(flare5)
display.remove(flare6)
title = false
local coinSheetData =
{
width = 32,
height = 32,
numFrames = 8,
}
local coinimageSheet = graphics.newImageSheet( "/Images/spinning_coin.png", coinSheetData )
local sequenceData =
{
name= "spinning_coin",
start = 1,
count = 8,
time = 1000,
loopCount = 0
}
--the properties of the name plate that can be changed ingame by using ".text" property
local nameOptions =
{
text = "Frankenstein",
x = 165,
y = 450,
width = 310,
font = "Charlesworth.ttf",
fontSize = 22,
align = "left"
}
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
page = 1
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
local VN_pages = {
[1] = nil,
[2] = "\"Then, seeing as this is a simulation of\n a visual novel dating sim, I have no\n choice but to ask you...\"",
[3] = "\"My lady, would you go on a date with me?\nFrankenstein... butler of the fineest noble,\nCadis Etrama di Raizel?\"",
[4] = "duck",
[5] = "duck",
[6] = "duck",
}
local displayNovelText = display.newText("\"I see. So I\'m supposed to pretend I am\na character in a multi-chapter phone\napp that you\'ve been reading...\"", 165, 500, "Goudy Old Style Regular.ttf", 17)
function changePage()
print("dang it")
page = page + 1
displayNovelText.text = VN_pages[page]
display.newText(displayNovelText)
end
textRect:addEventListener("tap", changePage)
if (coins < 10) then
coinsDigits = 2
else
if (coins > 9) and (coins < 100) then
coinsDigits = 3
else
if (coins > 99) and (coins < 1000) then
coinsDigits = 4
else
if (coins > 999) and (coins < 10000) then
coinsDigits = 5
else
if (coins > 9999) and (coins < 100000) then
coinsDigits = 6
end
end
end
end
end
cooin = display.newSprite(coinimageSheet, sequenceData)
cooin.x = 25
cooin.y = 30
cooin:play()
coinText = display.newText("1", 57 + 4 * coinsDigits, 32, "VCR_OSD_MONO_1.001.ttf", 25)
coinText.text = coins
coinTimer = timer.performWithDelay(2000, cooin, 1)
end
function choiceMade( event ) --the scenes where all the choices are made
if (event.action == "clicked") then
local i = event.index
if (i == 1) then
Runtime:removeEventListener()
titleScreen()
else
if (i == 2) then
system.openURL( "https://www.paypal.com/us/home" )
else
if (i == 3) then
return
end
end
end
end
end -- end of choice scenes
function Outofcoins()
--native alert lack of zero coins
local alertMessage = "Uh oh, looks like you've run out of coins! To continue reading the story, would you like to buy or gameble for coins?"
native.showAlert( "Out of coins!", alertMessage, {"Gamble for coins", "Purchase coins", "Exit to Menu"}, choiceMade)
end
if (coins == 0) then -- conditional branch that alerts Outofcoins if no coins left
Outofcoins()
end
function sceneGambleStart()
function earntCoins()
numberEarnt = 0
local coinsGot = display.newImage("/Images/coins_gold.png", 155, 275)
coinsGot.alpha = 0
local function fadeOutCoinsEart()
transition.to(logo, {time = 2000, alpha = 0})
display.remove(coinsGot)
end
local transitionFade = transition.to(logo, {time = 2000, alpha = 1, onComplete = fadeOutCoinsEarnt})
timer.performWithDelay(2000, transitionFade, 1)
coinText.text = coins + numberEarnt
end
local function gamblerIntro()
nameOptions.text = "Gambler"
local bg = display.newImage("/Images/bg4.jpg", 155, 275)
textRect = display.newRect(155, 525, 325, 200)
textRect:setFillColor(.02, .02, .02)
textRect.alpha = .6
local frames = display.newImage("/Images/windowframes_gold.png", 155, 275)
display.newText(nameOptions)
if (gambleVisit == false) then
display.newText("\"Welcome to the coin gambling shop!\nHere's your chance to earn free coins\nwithout having to use the app store!", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"You can play here once a day if you\'ve\nNO coins in your inventory! You are\ngiven three tries at any game each visit.", 165, 500, "Goudy Old Style Regular.ttf", 17)
--display.newText("\"So, then! What games will you play\nin our shop today? \n \n", 165, 500, "Goudy Old Style Regular.ttf", 17)
else
display.newText("\"Welcome back, player! You have\nthree tries left. So, what games\nwill you try your hand at?", 165, 500, "Goudy Old Style Regular.ttf", 17)
end
end
local function sceneDiceRoll()
--local show background graphics
--draw dice on screen, with function ready to shake/transition on screen when accelerometer
--transition hands up and drop dice animation
-- if dice = # chosen then give coins
end
local function sceneCardChoose()
-- a function that defines the mechanics of the card game.
--draw several options on the screen:
--3 cards, earn 5 coins.
--6 cards, earn 15 coins.
--9 cards, earn 30 coins.
--The player needs 5 coins to read another chapter, but by increasing card numbers,
--depending on the player's choice, show the images of the cards (with whatever numbers, always set)
-- on the screen and allow the player to choose a card. Make a shuffling animation.
-- lay all the cards on the screen, now with randomised positions defined by a number.
--the player may choose one. Event listener, if the number defined = card number, the card flips,
--shows its number, and the player wins the coins. defer to earntCoins.
--if the player chooses the wrong card, show him a "WRONG CARD" result, and ask if he would like another
--round or to exit to the main shop.
return
end
local function sceneGuessNumber()
--this game is not created, but is a dummy function that's shown here. It's included in the
--options to show that if this were a real game, that's what it would look like
return
end
end
The error message says all you need:
"mainl.lua:160: bad argument #1 to 'newText' (string expected, got
table)
if you go to line 160 of your code you'll find the following:
display.newText(displayNovelText)
a few lines above you do this:
local displayNovelText = display.newText("sometext")
Refer to the display.newText documentation to find out how to use this function correctly.
https://docs.coronalabs.com/api/library/display/newText.html
You'll see that display.newText() does not return a string, but a text-object.
It also does not take a text-object as input. That's what the error message is telling you.
To access the text of displayNovelText you have to write displayNovelText.text which you can stuff into display.newText() among other options...
Please make sure you always read the documentation of functions you use. You cannot be successful if you don't know what you are dealing with.

R the size of my graph is twice as big on linux than it is on windows

I have a strange problem with my stacked plots, which must be outputted as .png. I have R 3.1.3 on windows xp and on linux mint ( linux is my guest OS via virtual box , with my host windows xp). I have no other problem with my linux, besides the natural sluggishness of such a set-up. I must use linux because of the better management of the locales; windows renders all my characters from various alphabets all gibberish, despite the language pack installed.
The sizes of my graphs on linux (mint 17.1) are twice the ones on windows. Does this come from the fact that my linux is used via virtual box ?
I have no particular reason to install linux, that's why I have put it in a box. I have this little message when I log in on my linux through virtual box:
Can you tell me what sizes are the graphs on your system ?
the result in terms of sizes, see the GSC graphs
windowsGridnoGROBnoLOESS.png is 98 kB
windowsGridyesGROBnoLOESS.png is 679kB
windowsgscGROBnoLOESS.png is 332 kB
windowsGscnoGROBnoLOESS.png is 49 kB
windowsGscnoGROByesLOESS is 49 kB
windowsgscyesGROByesLOESS.png is 340 kB
linuxGridnoGROBnoLOESS is 104kB
linuxGridyesGROBnoLOESS is 729 kB
linuxgscGROBnoLOESS is 334 kB
linuxGscnoGROBnoLOESS is 48 kB
linuxGscnoGROByesLOESS is 115kB
linuxgscyesGROByesLOESS is 805 kB
especially once I incorporate the loess data, the size increases on linux but less on windows.
My personal graphs weight around 700KB on windows and nearly 3 MB on linux ! My graphs are closer to the graphs gsc since they have a long legend and are colored, with alpha layers and many many points with also the loess smoothings.
The basic graph can reproduce the phenomenon:
if you do not comment the first graph, you will need to download the data
here
I have put the arial font to avoid size discrepancies, but I think it is unnecessary. Feel free to comment the lines about the extrafonts and the line for the gsc plot theme.
require("ggplot2")
require("grid")
require("gridExtra")
require("extrafont")
# font_import() # import all your fonts
# fonts() #get a list of fonts
loadfonts(device = "win")
nmmaps<-read.csv("chicago-nmmaps.csv", as.is=T)
g<-ggplot(nmmaps, aes(date, temp, color=o3))+geom_point()
png(filename = "linuxGridnoGROBnoLOESS.png", width = 1920, height = 600, units = "px", pointsize = 11, bg = "white", res = 67.5)
g
dev.off()
png(filename = "linuxGridyesGROBnoLOESS.png", width = 1920, height = 4200, units = "px", pointsize = 11, bg = "white", res = 67.5)
grid.arrange(arrangeGrob(g,g,g,g,g,g,g,ncol=1, nrow=7))
dev.off()
gsc<- ggplot(aes(x = carat, y = price, color=cut), data = diamonds) + geom_point()+
theme_grey(base_family = "Arial") + theme(legend.background = element_rect(fill="grey95", colour=NA))
png(filename = "linuxGscnoGROBnoLOESS.png", width = 1920, height = 600, units = "px", pointsize = 11, bg = "white", res = 67.5)
gsc
dev.off()
png(filename = "linuxgscGROBnoLOESS.png", width = 1920, height = 4200, units = "px", pointsize = 11, bg = "white", res = 67.5)
grid.arrange(arrangeGrob(gsc,gsc,gsc,gsc,gsc,gsc,gsc,ncol=1, nrow=7))
dev.off()
gscloess<- ggplot(aes(x = carat, y = price, color=cut), data = diamonds) + geom_point()+
stat_smooth(method="loess", aes(group = 1), se=FALSE, span = 0.2, size=1.25, n=200, fullrange = T)+
theme_grey(base_family = "Arial") + theme(legend.background = element_rect(fill="grey95", colour=NA))
png(filename = "linuxGscnoGROByesLOESS.png", width = 1920, height = 600, units = "px", pointsize = 11, bg = "white", res = 67.5)
gscloess
dev.off()
png(filename = "linuxgscyesGROByesLOESS.png", width = 1920, height = 4200, units = "px", pointsize = 11, bg = "white", res = 67.5)
grid.arrange(arrangeGrob(gscloess,gscloess,gscloess,gscloess,gscloess,gscloess,gscloess,ncol=1, nrow=7))
dev.off()
I used optipng to reduce the size of the outputs from linux, but they remain higher than the ones on windows.
I also had to install xfonts-100-dpi and everything works, but the output is very ugly. but also very light.

Resources