commit 43cb082194f947f70985554d705814b749dfa54d
parent 67ba9f32d1f09812336dc823f760500e6c34366f
Author: Adam M <aemalone@gmail.com>
Date: Fri, 25 Dec 2020 14:45:30 -0600
better handling of unreadable gifs and gifs without clear frame delays
Diffstat:
2 files changed, 45 insertions(+), 32 deletions(-)
diff --git a/src/ComputerscareBlank.cpp b/src/ComputerscareBlank.cpp
@@ -28,8 +28,8 @@ struct ComputerscareBlank : Module {
int currentFrame = 0;
int numFrames = 0;
int stepCounter = 0;
- float frameDelay=.5;
- int samplesDelay=10000;
+ float frameDelay = .5;
+ int samplesDelay = 10000;
int speed = 100000;
ComputerscareSVGPanel* panelRef;
@@ -54,17 +54,17 @@ struct ComputerscareBlank : Module {
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
configParam(ANIMATION_SPEED, 0.f, 2.f, 1.f, "Animation Speed");
- paths.push_back("");
+ paths.push_back("empty");
}
void process(const ProcessArgs &args) override {
stepCounter++;
samplesDelay = frameDelay * args.sampleRate;
-
+
if (stepCounter > samplesDelay) {
//DEBUG("samplesDelay: %i",samplesDelay);
//DEBUG("%f",args.sampleRate);
stepCounter = 0;
- if(numFrames > 1) {
+ if (numFrames > 1) {
currentFrame ++;
currentFrame %= numFrames;
}
@@ -93,19 +93,19 @@ struct ComputerscareBlank : Module {
//paths.push_back(path);
//}
//else {
- paths[index] = path;
+ paths[index] = path;
//}
printf("setted %s\n", path.c_str());
//numFrames = paths.size();
currentFrame = 0;
}
void setFrameCount(int frameCount) {
- DEBUG("setting frame count %i",frameCount);
- numFrames=frameCount;
+ DEBUG("setting frame count %i", frameCount);
+ numFrames = frameCount;
}
void setFrameDelay(float frameDelaySeconds) {
- DEBUG("setting frame delay %f",frameDelaySeconds);
- frameDelay=frameDelaySeconds;
+ DEBUG("setting frame delay %f", frameDelaySeconds);
+ frameDelay = frameDelaySeconds;
}
std::string getPath() {
//return numFrames > 0 ? paths[currentFrame] : "";
@@ -132,14 +132,14 @@ struct ComputerscareBlank : Module {
void dataFromJson(json_t *rootJ) override {
-
+
json_t *pathJ = json_object_get(rootJ, "path");
if (pathJ) {
//paths.push_back(path)
path = json_string_value(pathJ);
setPath(path);
}
-
+
json_t *widthJ = json_object_get(rootJ, "width");
if (widthJ)
width = json_number_value(widthJ);
@@ -209,7 +209,7 @@ struct PNGDisplay : TransparentWidget {
int lastEnum = -1;
std::string path = "empty";
int img = 0;
- int currentFrame=-1;
+ int currentFrame = -1;
AnimatedGifBuddy gifBuddy;
PNGDisplay() {
@@ -244,21 +244,23 @@ struct PNGDisplay : TransparentWidget {
void draw(const DrawArgs &args) override {
if (blankModule && blankModule->loadedJSON) {
std::string modulePath = blankModule->getPath();
- //printf("%s\n", modulePath.c_str());
if (path != modulePath) {
- //img = nvgCreateImage(args.vg, modulePath.c_str(), 0);
+ DEBUG("path not module path");
+ DEBUG("path: %s, modulePath:%s",path.c_str(),modulePath.c_str());
gifBuddy = AnimatedGifBuddy(args.vg, modulePath.c_str());
img = gifBuddy.getHandle();
+
blankModule->setFrameCount(gifBuddy.getFrameCount());
blankModule->setFrameDelay(gifBuddy.getSecondsDelay());
-
+
nvgImageSize(args.vg, img, &imgWidth, &imgHeight);
imgRatio = ((float)imgWidth / (float)imgHeight);
-
+ path = modulePath;
if (path != "empty") {
setZooms();
}
- path = modulePath;
+
+
}
if (blankModule->imageFitEnum != lastEnum && lastEnum != -1) {
@@ -276,9 +278,9 @@ struct PNGDisplay : TransparentWidget {
nvgFill(args.vg);
nvgClosePath(args.vg);
}
- if(blankModule->currentFrame != currentFrame) {
+ if (blankModule->currentFrame != currentFrame) {
currentFrame = blankModule->currentFrame;
- gifBuddy.displayGifFrame(args.vg,currentFrame);
+ gifBuddy.displayGifFrame(args.vg, currentFrame);
}
}
}
diff --git a/src/animatedGif.hpp b/src/animatedGif.hpp
@@ -24,7 +24,7 @@ typedef struct gif_result_t {
-STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *frames, std::vector<unsigned char*> &framePointers,int &frameDelay)
+STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *frames, std::vector<unsigned char*> &framePointers, int &frameDelay)
{
FILE *f;
stbi__context s;
@@ -44,7 +44,7 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
memset(&g, 0, sizeof(g));
memset(&head, 0, sizeof(head));
- printf("%i\n",g);
+ printf("%i\n", g);
*frames = 0;
@@ -58,8 +58,15 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
}
if (prev) prev->next = gr;
- gr->delay = g.delay;
- frameDelay=g.delay;
+ if (g.delay) {
+ gr->delay = g.delay;
+ frameDelay = g.delay;
+ }
+ else {
+ gr->delay = 4;
+ frameDelay = 4;
+ }
+
prev = gr;
gr = (gif_result*) stbi__malloc(sizeof(gif_result));
memset(gr, 0, sizeof(gif_result));
@@ -107,8 +114,11 @@ STBIDEF unsigned char *stbi_xload(char const *filename, int *x, int *y, int *fra
if (prev != &head) STBI_FREE(prev);
}
}
- printf("first frame address p:%i\n",framePointers[0]);
- printf("second frame address p:%i\n",framePointers[1]);
+ printf("framePointers.size() %i\n",framePointers.size());
+ if (framePointers.size()) {
+ printf("first frame address p:%i\n", framePointers[0]);
+ printf("second frame address p:%i\n", framePointers[1]);
+ }
}
else
{
@@ -138,7 +148,7 @@ struct AnimatedGifBuddy {
imageHandle = animatedGifCreateImage(ctx, filename, 0);
}
int getHandle() {
- printf("imageHandle:%i\n",imageHandle);
+ printf("imageHandle:%i\n", imageHandle);
return imageHandle;
}
int animatedGifCreateImage(NVGcontext* ctx, const char* filename, int imageFlags) {
@@ -149,14 +159,15 @@ struct AnimatedGifBuddy {
stbi_convert_iphone_png_to_rgb(1);
//img = stbi_load(filename, &w, &h, &n, 4);
framePointers = {};
- img = stbi_xload(filename, &w, &h, &frame, framePointers,frameDelay);
+ printf("framePointers.size BEFORE %i\n",framePointers.size());
+ img = stbi_xload(filename, &w, &h, &frame, framePointers, frameDelay);
printf(filename);
- printf("\nframe delay:%i\n",frameDelay);
+ printf("\nframe delay:%i\n", frameDelay);
printf("loaded %i frames\n", framePointers.size());
numFrames = (int) framePointers.size();
//printVector(framePointers);
- if (img == NULL) {
-// printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
+ if (img == NULL || numFrames == 0) {
+ printf("Failed to load %s - %s\n", filename, stbi_failure_reason());
return 0;
}
image = nvgCreateImageRGBA(ctx, w, h, imageFlags, img);
@@ -176,6 +187,6 @@ struct AnimatedGifBuddy {
return numFrames;
}
float getSecondsDelay() {
- return ((float) frameDelay)/100;
+ return ((float) frameDelay) / 100;
}
};
\ No newline at end of file