Skip to content

Commit

Permalink
fix a bug in ccv_array_new that forbid us to allocate memory that is …
Browse files Browse the repository at this point in the history
…bigger than 2G
  • Loading branch information
liuliu committed Apr 4, 2013
1 parent a098025 commit e7361af
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/ccv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ typedef struct {

void ccv_icf(ccv_dense_matrix_t* a, ccv_dense_matrix_t** b, int type);
ccv_icf_multiscale_classifier_cascade_t* __attribute__((warn_unused_result)) ccv_icf_classifier_cascade_new(ccv_array_t* posfiles, int posnum, ccv_array_t* bgfiles, int negnum, const char* dir, ccv_icf_new_param_t params);
void ccv_icf_classifier_cascade_soft(ccv_icf_multiscale_classifier_cascade_t* multiscale_cascade, ccv_array_t* posfiles, int posnum, ccv_array_t* bgfiles, int negnum, const char* dir, double accept);
ccv_array_t* __attribute__((warn_unused_result)) ccv_icf_detect_objects(ccv_dense_matrix_t* a, ccv_icf_multiscale_classifier_cascade_t** multiscale_cascade, int count, ccv_icf_param_t params);
ccv_icf_multiscale_classifier_cascade_t* __attribute__((warn_unused_result)) ccv_icf_read_classifier_cascade(const char* directory);
void ccv_icf_write_classifier_cascade(ccv_icf_multiscale_classifier_cascade_t* classifier, const char* directory);
Expand Down
21 changes: 9 additions & 12 deletions lib/ccv_icf.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,6 @@ static void _ccv_icf_write_classifier_cascade_state(ccv_icf_classifier_cascade_s
for (i = 0; i < state->positives->rnum + state->negatives->rnum; i++)
fprintf(w, "%u %u %u %la\n", (uint32_t)state->example_state[i].binary, (uint32_t)state->example_state[i].correct, state->example_state[i].index, state->example_state[i].weight);
fclose(w);
// making a copy as well
char copy[1024];
snprintf(copy, 1024, "%s/example_state.%d", directory, state->j);
w = fopen(copy, "w+");
for (i = 0; i < state->positives->rnum + state->negatives->rnum; i++)
fprintf(w, "%u %u %u %la\n", (uint32_t)state->example_state[i].binary, (uint32_t)state->example_state[i].correct, state->example_state[i].index, state->example_state[i].weight);
fclose(w);
state->x.example_state = 1;
}
if (!state->x.classifier)
Expand Down Expand Up @@ -557,7 +550,7 @@ ccv_icf_multiscale_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_arra
{
FLUSH(" - collect positives %d%% (%d / %d)", (z.j + 1) * 100 / posnum, z.j + 1, posnum);
double ratio = (double)(posnum - z.j) / posfiles->rnum;
for (k = 0; k < posfiles->rnum; k++)
for (k = 0; k < posfiles->rnum && z.j < posnum; k++)
{
ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(posfiles, k);
ccv_dense_matrix_t* image = 0;
Expand All @@ -573,6 +566,8 @@ ccv_icf_multiscale_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_arra
ccv_array_push(z.positives, feature);
ccv_matrix_free(feature);
++z.j;
if (z.j >= posnum)
break;
}
ccv_matrix_free(image);
}
Expand All @@ -587,14 +582,14 @@ ccv_icf_multiscale_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_arra
{
FLUSH(" - collect negatives %d%% (%d / %d)", (z.j + 1) * 100 / negnum, z.j + 1, negnum);
double ratio = (double)(negnum - z.j) / bgfiles->rnum;
for (k = 0; k < bgfiles->rnum; k++)
for (k = 0; k < bgfiles->rnum && z.j < negnum; k++)
{
ccv_file_info_t* file_info = (ccv_file_info_t*)ccv_array_get(bgfiles, k);
ccv_dense_matrix_t* image = 0;
ccv_read(file_info->filename, &image, CCV_IO_ANY_FILE | CCV_IO_GRAY);
if (image)
{
double max_scale_ratio = ccv_min((double)image->rows / z.size.height, (double)image->cols / z.size.height);
double max_scale_ratio = ccv_min((double)image->rows / z.size.height, (double)image->cols / z.size.width);
if (max_scale_ratio <= 0.5) // too small to be interesting
continue;
for (q = 0; q < ratio; q++)
Expand All @@ -618,8 +613,6 @@ ccv_icf_multiscale_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_arra
}
ccv_matrix_free(image);
}
if (z.j >= negnum)
break;
}
}
printf("\n");
Expand Down Expand Up @@ -690,6 +683,10 @@ ccv_icf_multiscale_classifier_cascade_t* ccv_icf_classifier_cascade_new(ccv_arra
return z.classifier;
}

void ccv_icf_classifier_cascade_soft(ccv_icf_multiscale_classifier_cascade_t* multiscale_cascade, ccv_array_t* posfiles, int posnum, ccv_array_t* bgfiles, int negnum, const char* dir, double accept)
{
}

ccv_icf_multiscale_classifier_cascade_t* ccv_icf_read_classifier_cascade(const char* directory)
{
char filename[1024];
Expand Down
2 changes: 1 addition & 1 deletion lib/ccv_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ ccv_array_t* ccv_array_new(int rsize, int rnum, uint64_t sig)
array->rnum = 0;
array->rsize = rsize;
array->size = ccv_max(rnum, 2 /* allocate memory for at least 2 items */);
array->data = ccmalloc(array->size * rsize);
array->data = ccmalloc((size_t)array->size * (size_t)rsize);
return array;
}

Expand Down

0 comments on commit e7361af

Please sign in to comment.